[英]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. 我是Java的新手,尝试学习I / O语法。 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 ""
. 因为
output
始终为""
,所以将新行的counter
编号写入saveFile。 I guess it soposed to copy one file to another, but there should be save.write(temp + "\\n");
我猜它可能会将一个文件复制到另一个文件,但是应该有
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. 这是Java I / O教程的链接。 If you're trying to get to grips with Java I/O this is the best starting point.
如果您想使用Java I / O,那么这是最好的起点。
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 首先,您的类名FileReader与java.io.FileReader发生冲突,因此您对
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 实际上是在尝试实例化您的类FileReader的实例(对于该实例,没有构造函数采用单个字符串),而不是实例化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 以及此openFR声明为FileReader类型(您的FileReader不是java.io.FileReader),因此对
open = new BufferedReader(openFR);
also fails because BufferedReader expects a java.io.FileReader object. 也会失败,因为BufferedReader需要一个java.io.FileReader对象。
Your constructor for FileReader doesn't declare the types of the arguments: 您的FileReader构造函数未声明参数的类型:
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. 它尝试通过从openFile中读取所有行并将它们写入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).
然后,它为openFile中的每一行(减去第一行)将空白行写入saveFile。 You are also never doing anything with the counter value, so what is the point in counting it?
您也永远不会对计数器值做任何事情,那么对它进行计数有什么意义呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.