java读取行和拆分为字符数组
我不明白为什么我的程序不能运行。它会编译,但不会打印。我的文件中有一个5个字符的单词。我需要从文件中读取行,然后将其拆分为字符数组,然后打印出来。谢谢
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
public class test {
public static void main(String[] args)
{
BufferedReader line = null;
char[] array = new char[7];
try{
line = new BufferedReader(new FileReader(args[0]));
String currentLine;
while((currentLine = line.readLine()) != null)
{
array = currentLine.toCharArray();
}
for(int i = 0; i < array.length; i++)
{
System.out.print(array[i]);
}
}//try
catch(IOException exception)
{
System.err.println(exception);
}//catch
finally
{
try
{
if(line != null)
line.close();
}//try
catch(IOException exception)
{
System.err.println("error!" + exception);
}//catch
}//finally
} // main
} // test
# 1 楼答案
while循环跳过除最后一行之外的每一行,因此最后一行可能是空的。要显示可能的每一行,请执行以下操作:
或者,如果你只有1行,你可以简单地使用:
# 2 楼答案
你的程序只打印最后一行
你必须循环打印
如果以上不是问题,请检查您的文件权限
检查您的系统可能是由于对文件的权限,程序无法读取文件