基本Java IO,始终引发异常
我是Java新手,正在尝试编写一个有一个参数(文本文件路径)的程序。程序将找到文本文件并将其打印到屏幕上。最终,我将构建这个文件,以格式化给定的文本文件,然后将其打印到输出文件,但稍后我会到达那里 无论如何,我的程序总是抛出和IOException,我不知道为什么。给定参数C:\JavaUtility\input.txt,我在运行时收到“错误,无法读取文件”。我的代码在下面基本Java IO,始终引发异常,java,io,Java,Io,我是Java新手,正在尝试编写一个有一个参数(文本文件路径)的程序。程序将找到文本文件并将其打印到屏幕上。最终,我将构建这个文件,以格式化给定的文本文件,然后将其打印到输出文件,但稍后我会到达那里 无论如何,我的程序总是抛出和IOException,我不知道为什么。给定参数C:\JavaUtility\input.txt,我在运行时收到“错误,无法读取文件”。我的代码在下面 import java.io.*; public class utility { public static v
import java.io.*;
public class utility {
public static void main(String[] path){
try{
FileReader fr = new FileReader(path[0]);
BufferedReader textReader = new BufferedReader(fr);
String aLine;
int numberOfLines = 0;
while ((aLine = textReader.readLine()) != null) {
numberOfLines++;
}
String[] textData = new String[numberOfLines];
for (int i=0;i < numberOfLines; i++){
textData[i] = textReader.readLine();
}
System.out.println(textData);
return;
}
catch(IOException e){
System.out.println("Error, could not read file");
}
}
}
打包文本文件;
导入java.io.IOException;
公共类文件数据{
公共静态void main(字符串[]args)引发IOException{
字符串文件_name=args[0];
试一试{
ReadFile file=新的ReadFile(文件名);
字符串[]aryLines=file.OpenFile();
对于(int i=0;i
您位于文件的末尾。当您确定文件中的行数时,您已经读取到文件末尾,并且设置了EOF标志
。[编辑:正如下面的@EJP注释所述,BufferedReader
在文件末尾返回空读取值。但是,事实上,您的读取器不在您想要的位置,这一点仍然是正确的。]过去,我只是通过关闭和重新打开文件来解决这个问题。或者,查看数组列表
或简单的列表
。它们正在动态调整大小,因此您不需要提前知道文件中的行数。您位于文件的末尾。当您确定文件中的行数时,您已经读取到文件末尾,并且设置了EOF标志
。[编辑:正如下面的@EJP注释所述,BufferedReader
在文件末尾返回空读取值。但是,事实上,您的读取器不在您想要的位置,这一点仍然是正确的。]过去,我只是通过关闭和重新打开文件来解决这个问题。或者,查看数组列表
或简单的列表
。它们是动态调整大小的,所以您不需要提前知道文件中的行数。正如@mikeTheLiar提到的,您位于文件末尾BufferedReader
reference是文件处理程序,其内部光标指向文件中的当前位置。当您启动readLine()
方法时,指针读取字符,直到到达新行字符,并返回字符串。指针被设置到新位置。读取所有行后,readLine()
返回null
。之后,如果调用readLine()
它将抛出IOException。正如@EJP
在使用IO API时,最好的编码规则之一是始终检查EOF条件,就像在第一个循环中一样。在到达EOF之后,如果不重置光标,就不应该在同一引用上调用read方法-这可以通过调用reset()
method来完成
IMHO,在你的情况下,不需要第二个循环。您只能使用一个循环来实现该功能
import java.io.*;
import java.util.ArrayList;
public class utility {
public static void main(String[] path){
try{
FileReader fr = new FileReader(path[0]);
BufferedReader textReader = new BufferedReader(fr);
String aLine;
int numberOfLines = 0;
ArrayList readLines = new ArrayList();
while ((aLine = textReader.readLine()) != null) {
numberOfLines++;
readLines.add(aLine);
}
//Assuming you need array of lines, if not then you can print the lines directly in above loop
String[] textData = readLines.toArray();
System.out.println(textData);
return;
}
catch(IOException e){
System.out.println("Error, could not read file");
}
}
}
编辑
我尝试了你的代码-它工作正常,可以打印数组引用。正如注释中所建议的,问题在于源代码(由于安全或任何其他原因,文件可能不可读)-如果您可以打印异常消息并获得引发异常的确切行号,这将非常有用
除了IO异常外,还有几项观察结果:
您试图打开该文件两次<从OpenFile()
中调用code>readLines()方法。创建textReader
时,首先在OpenFile()
中打开代码文件序列。之后,您将调用readLines()
,当您创建file\u to\u read
时,它将再次尝试打开该文件
您应该尽量避免这种情况,并在流中调用int numberOfLines=readLines()
beforeFileReader fr=newfilereader(路径)代码>
同样,IMHO应该只有一个方法,并且您应该只在文件上迭代一次-从效率/性能和代码可维护性的角度来看。您可以按如下方式更改ReadFile
类:
package textfiles;
import java.io.*;
import java.util.ArrayList;
public class ReadFile {
private String path;
public ReadFile(String file_path){
path = file_path;
}
//You need not have separate file for counting lines in file
//Java provides dynamic sized arrays using ArrayList
//There is no need to count lines
public String[] OpenFile() throws IOException{
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
ArrayList fileLines = new ArrayList();
String readLine = textReader.readLine();
while(readLine != null){
fileLines.add(readLine);
readLine = textReader.readLine();
}
textReader.close();
return fileLines.toArray();
}
}
另一个小观察:在某些地方,java变量命名约定没有得到遵守OpenFile()
方法应该是OpenFile()
,file\u-to-u-read
应该是file-to-read
,正如@mikeTheLiar提到的,您在文件末尾BufferedReader
reference是文件处理程序,其内部光标指向文件中的当前位置。当您启动readLine()
方法时,指针读取字符,直到到达新行字符,并返回字符串。指针被设置到新位置。读取所有行后,readLine()
返回null
。之后,如果调用readLine()
它将抛出IOException。正如@EJP
在使用IO API时,最好的编码规则之一是始终检查EOF条件,就像在第一个循环中一样。在到达EOF之后,如果不重置光标,就不应该在同一引用上调用read方法-这可以通过调用reset()
method来完成
IMHO,在你的情况下,不需要第二个循环。您只能使用一个循环来实现该功能
import java.io.*;
import java.util.ArrayList;
public class utility {
public static void main(String[] path){
try{
FileReader fr = new FileReader(path[0]);
BufferedReader textReader = new BufferedReader(fr);
String aLine;
int numberOfLines = 0;
ArrayList readLines = new ArrayList();
while ((aLine = textReader.readLine()) != null) {
numberOfLines++;
readLines.add(aLine);
}
//Assuming you need array of lines, if not then you can print the lines directly in above loop
String[] textData = readLines.toArray();
System.out.println(textData);
return;
}
catch(IOException e){
System.out.println("Error, could not read file");
}
}
}
编辑
我尝试了你的代码-它工作正常,可以打印数组引用。正如注释中所建议的,问题在于源代码(由于安全或任何其他原因,文件可能不可读)-如果您可以打印异常消息并获得引发异常的确切行号,这将非常有用
除了IO异常外,还有几项观察结果:
package textfiles;
import java.io.*;
import java.util.ArrayList;
public class ReadFile {
private String path;
public ReadFile(String file_path){
path = file_path;
}
//You need not have separate file for counting lines in file
//Java provides dynamic sized arrays using ArrayList
//There is no need to count lines
public String[] OpenFile() throws IOException{
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
ArrayList fileLines = new ArrayList();
String readLine = textReader.readLine();
while(readLine != null){
fileLines.add(readLine);
readLine = textReader.readLine();
}
textReader.close();
return fileLines.toArray();
}
}