Can someone tell me what this Java class does? I/O related
I am relatively new to Java and trying to learn the I/O syntax. Could someone give me a general overview of what this code does? Thanks!!
import java.io.*;
public class FileReader { private String openFile=""; private String saveFile=""; FileReader(openFile, saveFile) { this.openFile=openFile; this.saveFile=saveFile; } public String process(){ System.out.println(this.openFile); System.out.println(this.saveFile); BufferedReader open=null; FileReader openFR=null; FileWriter save=null; int counter=0; String output=""; if(openFile.equals("")){ return "No open file specifified\n"; } if(this.saveFile.equals("")){ return "No save file specified\n"; } try { openFR = new FileReader(this.openFile); open = new BufferedReader(openFR); } catch (FileNotFoundException e) { return ("Open file no longer exists\n"); } try { save = new FileWriter(saveFile); } catch (IOException e){ return ("Error saving the file\n"); } try{ String temp = open.readLine(); while(temp != null){ temp = open.readLine(); counter++; save.write(output + "\n"); } } catch (IOException e){ e.getStackTrace(); return ("Er开发者_Go百科ror reading open file"); } try { save.flush(); } catch (IOException e) { e.printStackTrace(); return ("Error writing save file"); } return "Operation completed successfully"; } }
This is a nice example of how not to code!
Some of the issues:
- Doesn't close the streams!
This could lead to problems due to locked files and/or uncomplete written files - Doesn't use Exceptions or return states to indicate errors.
If you want to know if the operation succeded, you'll have to compare the returned Strings.. If anyone changes the String the depending app won't run anymore. - member variables only set in constructor should be final
So they can't be accidentially assigned. - local variables should be declared when needed
declaring them at the start of the method is a relict of older languages - ambiguous Exceptions aren't exposed
If an exception occures, you'll never know what it was, you will just see "Error reading open file"
Writes the counter
number of new lines to the saveFile, because output
is always ""
. I guess it soposed to copy one file to another, but there should be save.write(temp + "\n");
to do so.
It opens two files, then (if both files exists and can be opened), reads lines from the first file and writes empty lines into the second file in an endless loop until it gets to the end of the first file.
It does not close any of the files btw.
It tries to copy a text file but seems to have some bugs.
Here is a link to the Java I/O tutorial. If you're trying to get to grips with Java I/O this is the best starting point.
The code you've pasted attempts to copy the source file to a new destination file one line at a time but it contains bugs which will prevent this from happening, namely:
- The first line of the file is read and immediately discarded.
- Each line read is never assigned to "output" and is therefore never written to the destination file. The only thing that is written is "\n".
For me, this code does absolutely nothing except throw a lot of compile time errors.
First of all your class name FileReader clashes with java.io.FileReader, so your call to
new FileReader(this.openFile);
is actually trying to instantiate an instance of YOUR class FileReader (for which there is no constructor that takes a single string) rather than java.io.FileReader
As well as this openFR is declared to be of type FileReader (your FileReader not java.io.FileReader) so the call to
open = new BufferedReader(openFR);
also fails because BufferedReader expects a java.io.FileReader object.
Your constructor for FileReader doesn't declare the types of the arguments:
FileReader(openFile, saveFile) {
this.openFile=openFile;
this.saveFile=saveFile;
}
should be:
FileReader(String openFile, String saveFile) {
this.openFile = openFile;
this.saveFile = saveFile;
}
So on to what the program does once these errors are fixed:
It tries to copy a file by reading all lines from openFile and writing them to saveFile.
What it actually does is:
Reads the first line from the file and discards it. Enters a loop where it continues to read lines and increment a counter (which is also wrong since it never includes the first line in the count, unless the aim is to ignore the first line???). It then writes a blank line to the saveFile for every line in the openFile (minus the first line). You are also never doing anything with the counter value, so what is the point in counting it?
精彩评论