我在读取 java 中的文本文件并将其分配给数组时遇到问题。该程序正在运行,但我得到空值。我尝试将代码更改为最简单的代码,就像您在下面看到的一样。因为这个应该真正循环遍历文本文件。但我这样做是为了让我很容易看出问题出在哪里。但问题是我不知道为什么它仍然输出null。
该文件肯定在我指定的目录中,因为当我使用此方法检查它时,内置方法存在返回 true:
if(ofile.exists()==true){
System.out.println("exist");
}else{
System.out.println("not exist");
}
请我需要帮助,确定这背后的错误,为什么它返回 null。
public static void main(String args[]){
String[] eq=new String[50];
String[]ea=new String[50];
String[]eb=new String[50];
String[] ec=new String[50];
String[]ed=new String[50];
char[] ans=new char[50];
String strtemp;
File ofile= new File("J:\\questions.txt");
BufferedInputStream bis= null;
FileInputStream fis= null;
DataInputStream dis=null;
int ii=0;
int score=0;
System.out.println(eq[1] + "\n" + ea[1] + "\n" + eb[1] + "\n" + ec[1] + "\n" + ed[1] + "\n" + "\n" + "Answer: ");
String strans=x.nextLine();
char y=strans.charAt(0);
if(y==ans[1]){
score++;
}else{
}
try{
fis=new FileInputStream(ofile);
bis=new BufferedInputStream(fis);
dis=new DataInputStream(bis);
while(dis.available()!=0){
eq[1]=dis.readLine(); ea[1]=dis.readLine();
eb[1]=dis.readLine(); ec[1]=dis.readLine();
ed[1]=dis.readLine(); strtemp=dis.readLine();
ans[1]=strtemp.charAt(0);
}
bis.close();
dis.close();
fis.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}