该程序从文本文件中获取文本并将其放入名为textFile的字符串中。 我遇到的问题是在这个程序执行后程序继续运行,我需要手动杀死它来停止程序。我不明白为什么会这样。任何帮助将不胜感激。
代码:
import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
//selecting the txt file gui
JFileChooser jFileChooser = new JFileChooser();
int result = jFileChooser.showOpenDialog(new JFrame());
java.io.File selectedFile = jFileChooser.getSelectedFile();
if (result == JFileChooser.APPROVE_OPTION) {
// System.out.println("Selected file: " + selectedFile.getAbsolutePath());
} else {
System.out.println("Error selecting file");
}
String textFile = "";
try {
//start filereader and buffered reader to read txt file that was selected
FileReader fileReader = new FileReader(selectedFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = bufferedReader.readLine();
while(line != null) {
textFile += line;
line = bufferedReader.readLine();
}
bufferedReader.close();
fileReader.close();
} catch (Exception error) {
System.out.println("Error selecting file\n");
error.printStackTrace();
}
System.out.println(textFile);
}
}