我正在尝试从 JfileChooser 中选择一个目录,然后遍历该目录,读取所有文件并将它们全部写入一个大 HTML 文件以便于查看。这是应该完成此操作的代码片段:
else if(arg0.getSource()==generate){
//Create HTML report in same directory that file(s) came from
try{
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnval = fileChooser.showSaveDialog(this);
if(returnval == JFileChooser.APPROVE_OPTION){
File fileName = new File( "./report.html" );
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
bw.write("<html>");
bw.write("<body>");
bw.write("<h1>Graded Assignment</h1>");
bw.write("<p>");
//for loop here that appends all the files in a directory
File directory = new File(fileChooser.getSelectedFile().getAbsolutePath());
for( File f : directory.listFiles() ){
FileInputStream fstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
bw.write(br.readLine() + "\n");
}
bw.write("</p>");
bw.write("</body>");
bw.write("</html>");
bw.close();
Desktop.getDesktop().browse(fileName.toURI());
}
}
catch(FileNotFoundException fnf){
fnf.printStackTrace();
}
catch(IOException io){
io.printStackTrace();
}
它将进入 for 循环,但不会在第一个文件中读取。我收到某种 AWT-EventQueue 错误。谁能重新创建这个并告诉我问题可能是什么?谢谢。