JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter =new FileNameExtensionFilter ("Text/Java files","txt","java");
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
BufferedReader in = new BufferedReader(new FileReader("filename"));
String str;
ArrayList<String> list = new ArrayList<>();
while ((str = in.readLine()) != null) {
list.add(str);
}
String[] listArray =list.toArray(new String[list.size()]);
for (int b=0; b<listArray.length;b++) {
String[] Arra= str.split(" ");
jTextfield1.setText(Arra.get(0));
jTextfield2.setText(Arra.get(0));
答案 0 :(得分:2)
首先,将bufferedReader更改为:
BufferedReader in = new BufferedReader(new FileReader(filename));//remove quotes around filename
您试图从名为FileReader
的文件创建filename
,而不是从文件名本身创建<{p}}
Arra
是一个数组,但您将其用作列表。
请改为:
if(Arra.length > 1){
jTextfield1.setText(Arra[0]);
jTextfield2.setText(Arra[1]);
}
if语句很重要,因为Arra
可能没有索引0或1