【发布时间】:2012-08-07 04:15:31
【问题描述】:
我可以知道为什么我在上传 .txt 文件时无法读取下一行。 它发生在我尝试上传的所有 .txt 文件,然后是 system.out.println() 。 在我的文本文件中,它包含:cats,dogs,monkey(每个都在一行中).. 但输出的值是:
[Monkey][Monkey, null][Monkey, null, null][Monkey, null, null, null][Monkey, null, null, null, null][Monkey, null, null, null, null, null][Monkey, null, null, null, null, null, null]
在这方面需要帮助。 谢谢。
并想知道为什么我可以读取 .txt 文件而不是 .doc。也需要这方面的建议。
import java.awt.List;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.IIOException;
import javax.swing.JFileChooser;
public class searchforCapitalLetter {
public static void main(String[] args) throws IOException{
try {
// file chooser
JFileChooser chooser=new JFileChooser();
int returnVal = chooser.showOpenDialog(null);{
if(returnVal == JFileChooser.APPROVE_OPTION)
{ File f = chooser.getSelectedFile();}
FileInputStream fin = new FileInputStream(chooser.getSelectedFile());
DataInputStream din = new DataInputStream(fin);
BufferedReader br = new BufferedReader(new InputStreamReader(din));
ArrayList<String>list =new ArrayList<String> ();
if ((br.readLine()) != null) {
while (br.readLine() != " ") {
list.add(br.readLine());
System.out.print (list);
} br.close() ;
}//closes if statement
} // closes method dialog
} // closes try method
catch (FileNotFoundException e) {
e.printStackTrace();
} // closes catch method
} // closes method body
} // closes class method
【问题讨论】:
-
请不要包含注释代码。
-
另外,你应该真正养成一致缩进的习惯;它将为您节省大量调试时间。
-
您甚至没有添加
ActionListener以在选择文件时进行监听。 -
如果你想阅读文本,请不要使用DataInputStream,它比有用更混乱。