How to read from a file when I used PrintWriter, BufferedWriter and FileWriter for writing?

I have a method that writes some data to a file. I use PrintWriter , BufferedWriter and FileWriter as shown below

 public void writeToFile(String FileName){ PrintWriter pw = null; try { pw = new PrintWriter(new BufferedWriter(new FileWriter(FileName))); for(Cars car : list){ pw.println(car.getType()); pw.println(car.getMaxSpeed()); pw.println(car.getOwner()); pw.println(); pw.flush(); } pw.close(); } catch(IOException ex){ System.err.println(ex); } } 

Now, how can I read this data from a file? I tried using InputStreamReader , BufferedReader and FileInputStream , but my NetBeans shows me an error message

  public void readFromFile() throws IOException { InputStreamReader fr = null; try { fr = new InputStreamReader(new BufferedReader(new FileInputStream(new FileReader("c:\\cars.txt")))); System.out.println(fr.read()); } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { fr.close(); } } 

What is wrong with this method?

+9
source share
3 answers
 BufferedReader in = new BufferedReader(new FileReader("file.in")); BufferedWriter out = new BufferedWriter(new FileWriter("file.out")); String line = in.readLine(); // <-- read whole line StringTokenizer tk = new StringTokenizer(line); int a = Integer.parseInt(tk.nextToken()); // <-- read single word on line and parse to int out.write(""+a); out.flush(); 
+15
source

There are several problems in the code:

1) InputStreamReader takes InputStream as an argument, not a Reader. See http://docs.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html .

2) FileInputStream does not accept the Reader argument as an argument (it takes a file, FileDescriptor or String). See: http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html

3) BufferedReader usually reads the line File by line. The read () method reads only one character.

A possible solution could be:

 fr = new BufferedReader(new InputStreamReader(new FileInputStream(new File("c:\\cars.txt")))); String line = ""; while((line = fr.readLine()) != null) { System.out.println(line); } 

Btw: It would be easier for others to help you if you provide an accurate error message or even better StackTrace.

+3
source

Simple error: it is not possible to resolve the FileInputStream constructor (java.io.FileReader), the required constructor does not exist in the API.

Your original code was:

 new PrintWriter(new BufferedWriter(new FileWriter(FileName))); 

so you need to read

 new PrintReader(new BufferedReader(new FileReader(FileName))); 

but PrintReader is not needed (does not exist), so all you need is:

 new BufferedReader(new FileReader(FileName)) 

PrinterWriter prints formatted representations of objects in the text output stream, but when reading text is always formatted, so PrinterReader does not exist.

You write line by line, so read line by line :) Example:

 public void readFromFile() throws IOException { BufferedReader bufferedReader = null; try { String sCurrentLine; bufferedReader = new BufferedReader(new FileReader("c:\\cars.txt")); while ((sCurrentLine = bufferedReader.readLine()) != null) { System.out.println(sCurrentLine); } } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { bufferedReader.close(); } } 

or better (JDK7)

 void readFromFile() throws IOException { Path path = Paths.get("c:\\cars.txt"); try (BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset())){ String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } } } 
0
source

All Articles