我需要在使用JFileChooser
打开txt文件后执行以下操作任何人都可以指导对要求的理解吗?我已经实现了代码t从文件中读取字符,但不确定我是否接近要求。
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(JFileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
char[] buf = new char[32];
int numRead = 0;
int index = 1;
try {
while ((numRead = reader.read(buf)) != -1) {
System.out.println(numRead);
String readData = String.valueOf(buf, 0, numRead);
if (numRead == 32) {
fileData.append(" " + index + " " + readData);
index++;
} else {
fileData.append(" * " + readData);
index = 0;
}
buf = new char[32];
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
reader.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(fileData);
}
}
//Handle save button action.
}