从 jfilechooser 获取文件的值
Get the value of a file from a jfilechooser
我有一个带有 Jbutton 的 Java 应用程序,用于选择本地文件,然后用不同类型的图表表示该文件。
我想获取 JFileChooser 返回的文件并对其进行处理(拆分,在某些列表中添加值...)
这是 JButton 动作侦听器:
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);
}
我有一个带有 Jbutton 的 Java 应用程序,用于选择本地文件,然后用不同类型的图表表示该文件。 我想获取 JFileChooser 返回的文件并对其进行处理(拆分,在某些列表中添加值...)
这是 JButton 动作侦听器:
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);
}