4

I am trying to read integers from a file, apply some operation on them and writing those resulting integers to another file.

// Input
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
Scanner s = new Scanner(br);

// Output
FileWriter fw = new FileWriter("out.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

int i;

while(s.hasNextInt())
{
    i = s.nextInt();
    pw.println(i+5);
}

I want to ask is it a good practice to wrap these input and output streams like this?
I am new to java and on internet, I saw lots of other ways of I/O in files. I want to stick to one approach so is above the best approach ?

manfcas
  • 1,933
  • 7
  • 28
  • 47
Happy Mittal
  • 3,667
  • 12
  • 44
  • 60

5 Answers5

5

- Well consider that you went shopping into a food mall, Now what you do usually, pick-up each item from the selves and then go to the billing counter then again go to the selves and back to billing counter ....?? Or Store all the item into a Cart then go to the billing counter.

- Its similar here in Java, Files deal with bytes, and Buffer deals with characters, so there is a conversion of bytes to characters and trust me it works well, there will not be any noticeable overhead.

So to Read the File:

File f = new File("Path");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

So to Write the File:

File f = new File("Path");
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);

And when you use Scanner there is no need to use BufferedReader

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
3

Keep in mind that the design of those classes is based on the Decorator design pattern. A good practice is to close all instances of java.io.Closeable in a finally block. For example:

    Reader r = null;
    Scanner s = null;
    try {
        r = new FileReader("test.txt");
        s = new Scanner(r);
        // Do your stuff here.
    } finally {
        if (r != null)
            r.close();
        if (s != null)
            s.close();
    }

or, if you are using Java 7 or higher:

    try (
            Reader r = new FileReader("test.txt");
            Scanner s = new Scanner(r)
            ) {
        // Do your stuff here.
    }
Graham
  • 7,431
  • 18
  • 59
  • 84
manfcas
  • 1,933
  • 7
  • 28
  • 47
2

you dont really need BuffredWriter when you are using PrintWriter to write character data, printwriter has a constructor which takes filewriter as an argument. and dont need a scanner to read from a file you could acheive it using bufferedreader itself.

FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
while((line=br.readLine())!=null){
//do read operations here
}

FileWriter fw = new FileWriter("out.txt");
PrintWriter pw = new PrintWriter(fw);
 pw.println("write some data to the file")
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • I have taken BufferedWriter to increase efficiency. I included Scanner so I could read integer. I am not sure how to do it without Scanner because BufferedReader has only read() or readLine() method. – Happy Mittal Nov 01 '12 at 08:30
  • It's true that scanner is unnecessary to simply read in the file. But, scanner allows you to at least parse the tokens, and this is necessary for most applications –  Nov 01 '12 at 08:36
2

Scanner does not need the BufferedReader. You can wrap it over the FileReader.

Scanner s = new Scanner(new FileReader("test.txt"));

While using the scanner its better to assume that the source contains various content. Its good to close the scanner after using it.

   while(s.hasNext()){

    if(s.hasNextInt())
      int i = s.nextInt();

    s.next();
    }
     s.close();
clinton
  • 612
  • 3
  • 6
  • But I read somewhere we should use BufferedReader to increase efficiency. Isn't that so ? – Happy Mittal Nov 01 '12 at 08:35
  • They have different functions. Scanner can be used to tokenize input based on primitive values or delimiter string. BufferedReader has an advantage over the FileReader where it can read lines of text at a time. – clinton Nov 01 '12 at 08:38
0

I usually do this:

String inputFileLocation = "Write it here";

BufferedReader br = new BufferedReader(new FileReader(new File(fileLocation)));

while((line=br.readLine())!=null){
//Scanner operations here
}

String outputFileLocation = "Here";
PrintWriter pr = new PrintWriter(new FileWriter(new File(outputFileLocation)));
Angel
  • 1