gpt4 book ai didi

Java 使用文件选择器读取 csv 文件

转载 作者:行者123 更新时间:2023-12-01 12:56:51 25 4
gpt4 key购买 nike

我有我的代码

import java.io .*;
import java.util.Arrays;

public class Inputtheta {
public static void main (String [] arg) throws Exception{
BufferedReader CSVFile = new BufferedReader (new FileReader ("/Users/Carolina/Documents/IST/AMC/init.csv"));
String dataRow = CSVFile.readLine();


while (dataRow != null && !dataRow.isEmpty()){
String [] dataArray = dataRow.split (",");
double[] doubleArray =new double[dataArray.length];
int i=0;
while (i< dataArray.length ) {
doubleArray[i]= Double.parseDouble(dataArray[i]);
i++;
}
System.out.println(Arrays.toString(doubleArray));
dataRow = CSVFile . readLine ();
}
CSVFile.close ();
}

}

它从我直接询问的文件中读取,并将其数据保存到字符串中。我的问题是,除了直接放置该文件之外,如何包含文件选择器?我已经设法在 GUI 中打开该文件,但我想知道如何在上面的代码中实际使用该文件。

 public void actionPerformed(ActionEvent e) {

//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(Escolherficheiros.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());


}
}

最佳答案

[注意:尚未尝试编译或运行您的代码]

如果您只想弹出 JFC 而无需任何其他元素,则可以将 null 传递给 JFC.showOpenDialog()。由于您将在没有任何其他 swing/awt 元素的情况下启动它,因此它不会在 actionPerformed 方法中。

我只会使用类似的东西

private static File getFile(){
JFileChooser fc = new JFileChooser();
File file = null;
int returnVal = fc.showOpenDialog(null);

if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
}
return file;
}

并从您的主方法中调用它。

关于Java 使用文件选择器读取 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23812601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com