//通过swing控件读取文件
private static void chooseFile() {
JFileChooser jfc = new JFileChooser();// 初始化文件选择器
FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT","txt");// 设置文件过滤,TXT为提示用户选择文件类型,txt为文件过滤后缀
jfc.setFileFilter(filter);// 将文件过滤加载到选择器中
int returnVal = jfc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
// 获得打开的文件
File file = jfc.getSelectedFile();
File out = new File(file.getParent() + "\jiagkun.txt");
try {
InputStreamReader in = new InputStreamReader(
new FileInputStream(file), "UTF-8");
BufferedReader buff = new BufferedReader(in);
BufferedWriter write = new BufferedWriter(new FileWriter(out));
String text = null;
while ((text = buff.readLine()) != null) {
write.write(text + "
");
}
buff.close();
write.close();
} catch (FileNotFoundException e1) {
} catch (UnsupportedEncodingException e1) {
} catch (IOException e1) {
}
}