[英]How to process a file returned from JFileChooser in Java
我有一個應用程序來創建數據的圖形表示。 我在JFreeCharts方法上使用此方法,以使用列表參數創建圖表。 我需要處理從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))));
}
}
}
如何保留按鈕的動作偵聽器中“ theFile”的值。 在“ try”塊中,“ theFile”為空。
僅在有文件時(即在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);
}
}
});
另外,根據您的需要,您可以考慮重置兩個文件處理之間的列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.