Good practice in Java File I/O

12,367

Solution 1

- 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

Solution 2

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.
    }

Solution 3

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")

Solution 4

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();
Share:
12,367

Related videos on Youtube

Happy Mittal
Author by

Happy Mittal

Updated on October 15, 2022

Comments

  • Happy Mittal
    Happy Mittal over 1 year

    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 ?

    • Mostafa Zeinali
      Mostafa Zeinali almost 11 years
      How big is the file??