Question

I am trying to write a program that just reads and write an unbuffered steam, and reads and writes a buffered stream. Following the example on the java docs, I've got this for my buffered stream, which works fine.

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyCharacters {
    public static void main(String[] args) throws IOException {

        FileReader inputStream = null;
        FileWriter outputStream = null;

        try {
            inputStream = new FileReader("unbufferedread.txt");
            outputStream = new FileWriter("unbufferedwrite.txt");

            int c;
            while ((c = inputStream.read()) != -1) {
                outputStream.write(c);
            }
        // Add finally block incase of errors.
        // Display error if input file is not found.
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}

However the Java docs say "Here's how you might modify the constructor invocations in the CopyCharacters example to use buffered I/O:".

inputStream = new BufferedReader(new FileReader("bufferedread.txt"));
outputStream = new BufferedWriter(new FileWriter("bufferedwrite.txt"));

My question is how to implement it. Is it possible to add it all to one class? When I try to add it, I get an error saying:

"Cannot find symbol - class BufferedReader"

Any help would be great. Thanks.

Était-ce utile?

La solution

You have to import the java.io.BufferedReader and java.io.BufferedWriter classes. Based on the code you posted, you aren't doing that. So just add the two lines:

import java.io.BufferedReader;
import java.io.BufferedWriter;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top