我有一个应用程序来创建数据的图形表示。 我在JFreeCharts方法上使用此方法来使用lists参数创建图表。 我需要处理从JFileChooser返回的文件。 这是我的班级:
public class ReadGCFile {
static File theFile;
static JFileChooser fileChooser = new JFileChooser();
static JButton theButton = new JButton("Choose the file to represent");
public static void readGCList(List<String> gcArrayList,
List<String> gcStringList, List<String> gcDateList)
throws NumberFormatException, IOException, ParseException {
String line = "";
String[] tokens = null;
FileReader fr = null;
BufferedReader bufReader = null;
theButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
theFile = fileChooser.getSelectedFile();
}
}
});
try {
fr = new FileReader(theFile);
bufReader = new BufferedReader(fr);
while ((line = bufReader.readLine()) != null) {
line = line.replace(",", ".");
tokens = line.split(";");
gcDateList.add(tokens[0]);
gcStringList.add(tokens[1]);
gcArrayList.add(tokens[2]);
gcArrayList.add(tokens[3]);
gcArrayList.add(tokens[4]);
}
} catch (FileNotFoundException es) {
System.out.println("The file was not found.");
} catch (NullPointerException e) {
System.out.println("No files were chosen !");
}
catch (IOException e) {
bufReader.close();
}
for (int i = 0; i < gcDateList.size(); i++) {
SimpleDateFormat currentFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
SimpleDateFormat convertedFormat = new SimpleDateFormat(
"dd MMM hh:mm:ss");
gcDateList.set(i, convertedFormat.format(currentFormat
.parse(gcDateList.get(i))));
}
}
}
如何从按钮的actionlistener中保留“theFile”的值。 在“try”块中,“theFile”为空。
答案 0 :(得分:1)
仅当您拥有文件时,即在 actionPerformed 方法中处理文件。
示例:
将所有处理逻辑放在一个方法中:
private static void processFile(File theFile, List<String> gcArrayList,
List<String> gcStringList, List<String> gcDateList){
String line = "";
String[] tokens = null;
FileReader fr = null;
BufferedReader bufReader = null;
try {
fr = new FileReader(theFile);
bufReader = new BufferedReader(fr);
while ((line = bufReader.readLine()) != null) {
line = line.replace(",", ".");
tokens = line.split(";");
gcDateList.add(tokens[0]);
gcStringList.add(tokens[1]);
gcArrayList.add(tokens[2]);
gcArrayList.add(tokens[3]);
gcArrayList.add(tokens[4]);
}
} catch (FileNotFoundException es) {
System.out.println("The file was not found.");
} catch (NullPointerException e) {
System.out.println("No files were chosen !");
}
catch (IOException e) {
bufReader.close();
}
for (int i = 0; i < gcDateList.size(); i++) {
SimpleDateFormat currentFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
SimpleDateFormat convertedFormat = new SimpleDateFormat(
"dd MMM hh:mm:ss");
gcDateList.set(i, convertedFormat.format(currentFormat
.parse(gcDateList.get(i))));
}
}
然后,从actionPerformed方法调用它:
theButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
theFile = fileChooser.getSelectedFile();
processFile(theFile,gcArrayList,gcStringList,gcDateList);
}
}
});
此外,根据您的需要,您可以考虑重置两个文件处理之间的列表。