Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 8 Java I/O Fundamentals

D:\> java Copy Copy.java DuplicateCopy.java D:\> fc Copy.java DuplicateCopyjava

Comparing files Copy.java and DuplicateCopy.java FC: no differences encountered

Yes, it worked correctly. What if you give it a source file name that does not exist?

D:\> java Copy Cpy.java DuplicateCopyjava

Cannot open the file Cpy.java (The system cannot find the file specified)

You typed Cpy.java instead of Copy.java and the program terminates with a readable error message, as expected. Here’s how this program works. In the try-with-resources statement, you opened srcFile for reading and

dstFile for writing. You wanted to use buffered I/O, so you passed FileReader and FileWriter references to

BufferedReader and BufferedWriter, respectively.

try (BufferedReader inputFile = new BufferedReader(new FileReader(srcFile)); BufferedWriter outputFile = new BufferedWriter(new FileWriter(dstFile)))

You’re using the try-with-resources statement, and the close() method for BufferedWriter will first call the flush() method before closing the stream.

When you’re using buffered I/O in your programs, it’s a good idea to call the flush() method explicitly in places where you want to ensure that all pending characters or data is flushed (i.e., written to the underlying file).

“Tokenizing” Text

In the last two examples (Listings 8-4 and 8-5), you just read or wrote to text files. However, in real-world programs, you may want to perform some processing when reading or writing files. For example, you may want to look out for certain patterns, search for some specific strings, replace one sequence of characters with another sequence of

characters, filter out specific words, format the output in a certain way, etc. You can use existing APIs such as regular expressions (covered in Chapter 7), Scanner, etc. for such purposes.

For illustration, consider that you want to list all the words in a given text file and eliminate all unnecessary whitespaces, punctuation characters, etc. Also, you need to print the resulting words in alphabetical order. To solve this problem, you can use a Scanner and pass the regular expression that you want to match or delimit (see Listing 8-6).

Listing 8-6.  Tokenize.java

import java.io.FileNotFoundException; import java.io.FileReader;

import java.util.Scanner; import java.util.Set; import java.util.TreeSet;

//read the input file and convert it into "tokens" of words;

//convert the words to same case (lower case), remove duplicates, and print the words

235

Chapter 8 Java I/O Fundamentals

class Tokenize {

public static void main(String []args) {

//read the input file if(args.length != 1) {

System.err.println("pass the name of the file to be read as an argument"); System.exit(−1);

}

String fileName = args[0];

//use a TreeSet<String> which will automatically sort the words

//in alphabetical order

Set<String> words = new TreeSet<>();

try ( Scanner tokenizingScanner = new Scanner(new FileReader(fileName)) ) {

//set the delimiter for text as non-words (special characters,

//white-spaces, etc), meaning that all words other than punctuation

//characters, and white-spaces will be returned tokenizingScanner.useDelimiter("\\W"); while(tokenizingScanner.hasNext()) {

String word = tokenizingScanner.next();

if(!word.equals("")) { // process only non-empty strings // convert to lowercase and then add to the set words.add(word.toLowerCase());

}

}

//now words are in alphabetical order without duplicates,

//print the words separating them with tabs

for(String word : words) { System.out.print(word + '\t');

}

} catch (FileNotFoundException fnfe) {

System.err.println("Cannot read the input file - pass a valid file name");

 

 

}

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Let’s see if it works:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D:\> type limerick.txt

 

 

 

 

 

 

There was a young lady of Niger

 

 

 

 

 

Who smiled as she rode on a tiger.

 

 

 

 

 

They returned from the ride

 

 

 

 

 

 

With the lady inside

 

 

 

 

 

 

 

And a smile on the face of the tiger.

 

 

 

 

 

 

 

 

 

 

 

 

 

D:\> java Tokenize limerick.txt

 

 

 

 

 

a

and

as

face

from

inside lady

niger

of

on returned

ride

rode

she

smile

smiled the

there they tiger

was

who

with

young

 

 

 

 

 

 

 

 

 

Yes, it does work correctly. Now let’s see what this program does. The program first opens the file using a FileReader and passes it to the Scanner object. The program sets the delimiter for Scanner with

useDelimiter("\\W"); the “\W” matches for non-words, so any non-word characters will become delimiters.

236

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]