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 ("Error 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:
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.
它尝试复制文本文件,但似乎有一些错误。
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:
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.