 |
 |
 |
11) Le système d’E/S de Java |
|
 |
|
Texte original |
 |
Traducteur :
Armel Fortun |
|
 |
|
 |
 |
 |
 |
 |
 |
|
 |
|
 |
 |
 |
Exercises
|
 |
Exercices
|
 |
 |
 |
Solutions to selected exercises
can be found in the electronic document The Thinking in Java Annotated
Solution Guide, available for a small fee from
www.BruceEckel.com.
|
 |
Les solutions aux exercices choisis peuvent être trouvées
dans le document électronique The Thinking in Java Annotated Solution Guide, disponible
pour un faible coût sur www.BruceEckel.com.
|
 |
 |
 |
- Open a text file so that
you can read the file one line at a time. Read each line as a String and
place that String object into a LinkedList. Print all of the lines
in the LinkedList in reverse
order.
- Modify
Exercise 1 so that the name of the file you read is provided as a command-line
argument.
- Modify
Exercise 2 to also open a text file so you can write text into it. Write the
lines in the ArrayList, along with line numbers (do not attempt to use
the “LineNumber” classes), out to the
file.
- Modify
Exercise 2 to force all the lines in the ArrayList to upper case and send
the results to
System.out.
- Modify
Exercise 2 to take additional command-line arguments of words to find in the
file. Print any lines in which the words
match.
- Modify
DirList.java so that the FilenameFilter actually opens each file
and accepts the file based on whether any of the trailing arguments on the
command line exist in that
file.
- Create a class
called SortedDirList with a constructor that takes file path information
and builds a sorted directory list from the files at that path. Create two
overloaded list( ) methods that will either produce the whole list
or a subset of the list based on an argument. Add a size( ) method
that takes a file name and produces the size of that
file.
- Modify
WordCount.java so that it produces an alphabetic sort instead, using the
tool from Chapter
9.
- Modify
WordCount.java so that it uses a class containing a String and a
count value to store each different word, and a Set of these objects to
maintain the list of
words.
- Modify
IOStreamDemo.java so that it uses LineNumberInputStream to keep
track of the line count. Note that it’s much easier to just keep track
programmatically.
- Starting
with section 4 of IOStreamDemo.java, write a program that compares the
performance of writing to a file when using buffered and unbuffered
I/O.
- Modify section
5 of IOStreamDemo.java to eliminate the spaces in the line produced by
the first call to in5br.readLine( ). Do this using a while
loop and
readChar( ).
- Repair
the program CADState.java as described in the
text.
- In
Blips.java, copy the file and rename it to BlipCheck.java and
rename the class Blip2 to BlipCheck (making it public and
removing the public scope from the class Blips in the process). Remove
the //! marks in the file and execute the program including the offending
lines. Next, comment out the default constructor for BlipCheck. Run it
and explain why it works. Note that after compiling, you must execute the
program with “java Blips” because the main( )
method is still in class
Blips.
- In
Blip3.java, comment out the two lines after the phrases “You must
do this:” and run the program. Explain the result and why it differs from
when the two lines are in the
program.
- (Intermediate)
In Chapter 8, locate the GreenhouseControls.java example, which consists
of three files. In GreenhouseControls.java, the Restart( )
inner class has a hard-coded set of events. Change the program so that it reads
the events and their relative times from a text file. (Challenging: Use a design
patterns factory method to build the events—see Thinking in
Patterns with Java, downloadable at
www.BruceEckel.com.)
|
 |
- Ouvrez un fichier de manière à pouvoir lire le fichier une ligne à la fois.
Lisez chaque ligne comme un String et placez cet objet String
dans un . Affichez toutes les lignes dans le LinkedList en ordre
inverse.
- Modifiez l'exercice 1 pour que le nom du fichier à lire soit entré comme
argument de ligne de commande.
- Modifiez l'exercice 2 pour ouvrir aussi un fichier texte dans lequel vous
pourrez écrire. Écrivez les lignes dans l'ArrayList, avec les numéros de ligne (ne
pas essayer d'utiliser la classe « LineNumber »), vers le fichier.
- Modifiez l'exercice 2 pour imposer les majuscules à toutes les lignes de
l'ArrayList et envoyer les résultats à System.out.
- Modifiez l'exercice 2 pour qu'il prenne un argument de ligne de commande
supplémentaire des mots à trouver dans le fichier. Afficher toutes les lignes dans lesquelles on
trouve le mot.
- Modifier DirList.java pour que le
FilenameFilter ouvre en fait chaque fichier et reçoive le fichier sur la base de
n'importe quel argument tiré de la ligne de commande existant dans ce fichier.
- Créez une classe nommée SortedDirList avec un constructeur
qui prend l'information de chemin de fichier et construit une liste triée du répertoire à partir
des fichiers du répertoire. Créez deux méthodes list() surchargées avec un
constructeur qui présenterons l'une ou l'autre la liste complète ou un sous-ensemble de la liste
basée sur un argument. Ajoutez une méthode size() qui prendra un nom de fichier et
présentera la taille de ce fichier.
- Modifiez WordCount.java pour qu'il produise à la place un
classement alphabétique, utilisant l'outil du Chapitre 9.
- Modifiez WordCount.java pour qu'il utilise une classe
contenant un String et une valeur de numérotation pour stocker chaque différent
mot, et un Set de ces objets pour maintenir la liste de ces mots.
- Modifiez IOStreamDemo.java afin qu'il utilise
LineNumberInputStream pour garder trace du compte de ligne. Notez qu'il est plus
facile de garder seulement les traces de manière programmatique.
- En commençant avec la partie 4 de IOStreamDemo.java,
écrivez un programme qui compare les performances à l'écriture d'un fichier en utilisant une E/S
mise en mémoire tampon et l'autre non.
- Modifiez la partie 5 d'IOStreamDemo.java pour éliminer les
espaces dans la ligne produite par le premier appel à in5br.readLine(). Faites
cela en employant une boucle while et readChar().
- Réparez le programme CADState.java comme décrit dans le
texte.
- Dans Blips.java, copiez le fichier et renommez le en
BlipCheck.java et renommez la classe Blip2 en
BlipCheck (rendez la public et enlevez la portée publique de la
classe Blips dans le processus). Enlevez les marques //! dans le
fichier et lancez le programme incluant les mauvaises lignes. Ensuite, enlevez les commentaires du
constructeur par défaut de BlipCheck. Lancez le et expliquez pourquoi il
fonctionne. Notez qu'après la compilation, vous devrez exécuter le programme avec
« java Blips » parce que la méthode main() est encore dans
Blips.
- Dans Blip3.java, enlevez les commentaires des deux lignes
après les phrases « Vous devez faire ceci : » et lancez le programme. Expliquez le
résultat et pourquoi il diffère de quand les deux lignes sont dans le programme.
- (Intermédiaire) Dans le chapitre 8, repérez l'exemple
GreenhouseControls.java, qui se compose de trois fichiers. Dans
GreenhouseControls.java, la classe interne Restart() contient un
jeu d'événements codé durement. Changez le programme pour qu'il lise les événements et leurs heures
relatives depuis un fichier texte. (Défi : Utilisez une méthode de fabrique d'un design
pattern pour construire les événements — voir Thinking in Patterns with Java,
télechargeable à www.BruceEckel.com.)
|
 |
 |
 |
[57]
Design Patterns, Erich Gamma et al., Addison-Wesley
1995.
|
 |
[57] Design
Patterns, Erich Gamma et al., Addison-Wesley 1995.
|
 |
 |
 |
[58]
XML is another way to solve the problem of moving data across different
computing platforms, and does not depend on having Java on all platforms.
However, Java tools exist that support XML.
|
 |
[58] XML est un autre
moyen de résoudre le problème de déplacer les donnée entre différentes plates-formes informatiques,
et ne dépend pas du fait d'avoir Java sur toutes les plate-formes. Cependant, des outils Java
existent qui supportent XML.
|
 |
 |
 |
[59]
Chapter 13 shows an even more convenient solution for this: a GUI program with a
scrolling text area.
[ Previous Chapter ]
[ Short TOC ]
[ Table of Contents ]
[ Index ]
[ Next Chapter ]
Last Update:04/24/2000
|
 |
[59] Le chapitre 13
montre une solution même plus commode pour cela : un programme GUI avec une zone de texte avec
ascenseur.
|
 |
 |
 |
 |
 |
 |
 |
 |
|
 |
 |
 |
|
 |