//如果按下确定按钮,则获得该文件。
if(flag==JFileChooser.APPROVE_OPTION)
{
//获得你输入要保存的文件
f=fc.getSelectedFile();
//获得文件名
fileName=fc.getName(f);
//也可以使用fileName=f.getName();
System.out.println(fileName);
//这里读取文件中的内容。
} 参考:public static List<String> readFile(File f) throws IOException {
if (f == null || !f.exists()) {
return null;
}
List<String> list = new ArrayList<String>();
String encoding = "UTF-16LE";
InputStreamReader read = new InputStreamReader(new FileInputStream(f),
encoding);
BufferedReader br = new BufferedReader(read);
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
read.close();
return list;
}
if(flag==JFileChooser.APPROVE_OPTION)
{
//获得你输入要保存的文件
f=fc.getSelectedFile();
//获得文件名
fileName=fc.getName(f);
//也可以使用fileName=f.getName();
System.out.println(fileName);
//这里读取文件中的内容。
} 参考:public static List<String> readFile(File f) throws IOException {
if (f == null || !f.exists()) {
return null;
}
List<String> list = new ArrayList<String>();
String encoding = "UTF-16LE";
InputStreamReader read = new InputStreamReader(new FileInputStream(f),
encoding);
BufferedReader br = new BufferedReader(read);
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
read.close();
return list;
}
//如果按下確定按鈕,則獲得該文件。
if(flag==JFileChooser.APPROVE_OPTION){
t.setText("");//把你的textarea設置成全局
//獲得該文件
f=fc.getSelectedFile();
System.out.println("open file----"+f.getName());
try {
InputStreamReader read = new InputStreamReader(
new FileInputStream(f));
BufferedReader br = new BufferedReader(read);
String line = null;
while ((line = br.readLine()) != null) {
t.append(line+"\n");
}
br.close();
read.close();
} catch (Exception e) {
// TODO: handle exception
}
}
File file = ...; // jfilechooser 返回的文件
try(FileReader reader = new FileReader(file)){
kit.read(reader,jta.getDocument(),0);
}catch(IOException|BadLocationException e){
...// process exception
}