[英]Get the value of a file from a jfilechooser
我有一個帶有Jbutton的Java應用程序,用於選擇本地文件,然后用不同類型的圖表表示該文件。 我想獲取JFileChooser返回的文件並進行處理(拆分,在某些列表中添加值...)
這是JButton actionlistener:
JButton theButton = new JButton("Choose the file to represent");
theButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
theFile = fileChooser.getSelectedFile();
ReadGCFile.getValue(theFile);
}
}
});
desktopPane.add(theButton);
theButton.setVisible(true);
theButton.setBounds(20, 100, 250, 20);
getContentPane().add(theButton);
在這里我要處理文件:
public static void readGCList(List<String> gcArrayList,
List<String> gcStringList, List<String> gcDateList)
throws NumberFormatException, IOException, ParseException {
File newFile = null;
String line = "";
String[] tokens = null;
FileReader fr = null;
BufferedReader bufReader = null;
try {
fr = new FileReader(GDRT.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();
}
我做了一個getValue方法,其中我不知道要放什么:
public static void getValue(File myFile) {
System.out.println(myFile.getName());
}
考慮這樣的方法:
public void processFile(File newFile) {
try {
getValue(newFile);
FileReader fr = new FileReader(newFile);
BufferedReader bufReader = new BufferedReader(fr);
Object line;
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]);
}
}
您可以在ActionListener中調用此命令:
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
theFile = fileChooser.getSelectedFile();
processFile(theFile);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.