我一直试图这样做一个星期,我用javafx和scenebuilder创建了一个java程序。我希望能够使用文本文件的值打开文本文件并生成表格。
到目前为止,我有:
我遇到的问题是将所选文件链接到我创建的数组,然后使用数组中的值创建动态表。我不知道从哪里开始,让这项工作。
工作文件选择器:https://gyazo.com/884b215545a7beebedfe7e9bfab064ed 场景构建器表:https://gyazo.com/5637d10c7191710d24c2e2d42c53675e
文字文件
john, smith, usa, California
sarah, parker, usa, Iowa
simon, davids, usa, Mississippi
文件开启者代码
public class SampleController {
private Button btn1;
public void Button1Action(ActionEvent event) {
{
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters()
.addAll(
new FileChooser.ExtensionFilter("TXT files (*.TXT)", "*.TXT"),
new FileChooser.ExtensionFilter("txt files (*.txt)", "*.txt"));
File file = fileChooser.showOpenDialog(null);
if (file != null) {
try {
Files.lines(file.toPath()).forEach(System.out::println);
} catch (IOException ex) {
Logger.getLogger(JavaFXReadTextFile.class.getName())
.log(Level.SEVERE, null, ex);
}
}
}
}{
}
文本文件数组
public class tableValue {
public static Scanner value;
{
try {
value = new Scanner(new File("data.txt"));
// code for number of lines start
File file =new File("data.txt");
if(file.exists()){
FileReader fr = new FileReader(file);
LineNumberReader lnr = new LineNumberReader(fr);
int linenumber = 0;
while (lnr.readLine() != null){
linenumber++;
}
lnr.close();
// code for number of lines end
String[][] valuezz = new String [linenumber][];
for (int i = 0; i < linenumber; i++) {
String line = value.nextLine();
String [] oneRowValue = line.split(",");
animalzz[i] = new String[oneRowValue.length];
for(int j=0; j<oneRowAnimals.length; j++) {
// Here you are storing values
valuezz[i][j] = oneRowValue[j];
}
}
System.out.println(valuezz[2][3]);
// Outputs Mississippi
}else{
System.out.println("File does not exists!");
}
}
catch(Exception e) {
System.out.println("could not find file");
}
}