Chapter 10: Using I/O

**2. Even though console input and output is text-based, why does Java still use byte streams for this purpose?**
 **3. Show how to open a file for reading bytes.**
Here is one way to open a file for byte input:

```
FileInputStream fin = new FileInputStream("test");

 **4. Show how to open a file for reading characters.**
Here is one way to open a file for reading characters:

```
FileReader fr = new FileReader("test");

 **5. Show how to open a file for random-access I/O.**
Here is one way to open a file for random access:

```
randfile = new RandomAccessFile("test", "rw");

 **6. How do you convert a numeric string such as "123.23" into its binary equivalent?**
 **7. Write a program that copies a text file. In the process, have it convert all spaces into hyphens.'Use the byte stream file classes. Use the traditional approach to closing a file by explicitly**
**calling close( ).**

```
/\* Copy a text file, substituting hyphens for spaces.

This version uses byte streams.

To use this program, specify the name

of the source file and the destination file.

For example,

java Hyphen source target

\*/

<nowiki>\*</nowiki>/

import java.io.\*;

class Hyphen {

public static void main(String args[])

{

int i;

FileInputStream fin = null;

FileOutputStream fout = null;

// First make sure that both files have been specified.

if(args.length !=2 ) {

System.out.println("Usage: Hyphen From To");

return;

}

// Copy file and substitute hyphens.

try {

fin = new FileInputStream(args[0]);

fout = new FileOutputStream(args[1]);

do {

i = fin.read();

// convert space to a hyphen

if((char)i == ' ') i = '-';

if(i != -1) fout.write(i);

} while(i != -1);

} catch(IOException exc) {

System.out.println("I/O Error: " + exc);

} finally {

try {

if(fin != null) fin.close();

} catch(IOException exc) {

System.out.println("Error closing input file.");

}

try {

if(fin != null) fout.close();

} catch(IOException exc) {

System.out.println("Error closing output file.");

}

}

}

}

 **8. Rewrite the program in question 7 so that it uses the character stream classes. This time, use the try-with-resources statement to automatically close the file.**

```
/\* Copy a text file, substituting hyphens for spaces.

This version uses character streams.

To use this program, specify the name

of the source file and the destination file.

For example,

java Hyphen2 source target

\*/

<nowiki>\*</nowiki>/

import java.io.\*;

class Hyphen2 {

public static void main(String args[])

throws IOException

{

int i;

// First make sure that both files have been specified.

if(args.length !=2 ) {

System.out.println("Usage: CopyFile From To");

return;

}

// Copy file and substitute hyphens.

// Use the try-with-resources statement.

try (FileReader fin = new FileReader(args[0]);

FileWriter fout = new FileWriter(args[1]))

{

do {

i = fin.read();

// convert space to a hyphen

if((char)i == ' ') i = '-';

if(i != -1) fout.write(i);

} while(i != -1);

} catch(IOException exc) {

System.out.println("I/O Error: " + exc);

}

}

}

 9. What type of stream is System.in?
 **10. What does the read( ) method of InputStream return when the end of the stream is reached?**
 **11. What type of stream is used to read binary data?**
 **12. Reader and Writer are at the top of the \_\_\_\_\_\_\_\_\_\_\_\_ class hierarchies.**
 **13. The try-with-resources statement is used for \_\_\_\_\_\_\_\_\_\_\_ \_\_\_\_\_\_\_\_\_\_\_\_ \_\_\_\_\_\_\_\_\_\_\_\_.**
 '*14. If you are using the traditional method of closing a file, then closing a file within a finally'***block is generally a good approach. True or False?**
True