日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java JFileChooser选择文件和保存文件

發布時間:2025/3/8 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java JFileChooser选择文件和保存文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

//文件過濾器
import
java.io.File;import javax.swing.filechooser.FileFilter;public class MyFilter extends FileFilter{private String[] filterString = null;public MyFilter(String[] filStrings){this.filterString = filStrings;}public boolean accept(File file){if(file.isDirectory()) return true;for(int i=0; i<filterString.length; ++i)if(file.getName().endsWith(filterString[i]))return true;/* 返回要顯示的文件類型 *//** File.isDirectory()測試此抽象路徑名表示的文件是否是一個目錄*/return false;}public String getDescription() {String ss = "";for(int i=0; i<filterString.length; ++i)ss += " *" + filterString[i];return("Txt Files(" + ss + ")"); //返回顯示文件類型的描述 } } //文件的選擇
          JFileChooser jfc = new JFileChooser();//設置文件的過濾器String[] filterString = {".cpp", ".c"};MyFilter filter = new MyFilter(filterString);//獲取jar包位置,設置JFileChooser當前路徑String jarFilePath = Main.class.getProtectionDomain().getCodeSource().getLocation().getFile();try {jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");} catch (UnsupportedEncodingException ex) {ex.printStackTrace();}jfc.setCurrentDirectory(new File(jarFilePath));jfc.setFileFilter(filter);jfc.showOpenDialog(null);File fl = jfc.getSelectedFile();if(fl != null){String code = "";try {BufferedReader br = new BufferedReader(new FileReader(fl));String newLine = null;boolean flag = true;while((newLine=br.readLine()) != null){}} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}

?

//文件的保存
            JFileChooser jfc = new JFileChooser();String[] filterString = {".txt"};//設置文件的過濾器MyFilter filter = new MyFilter(filterString);//獲取jar包位置,設置JFileChooser當前路徑String jarFilePath = LexicalAnalyzer.class.getProtectionDomain().getCodeSource().getLocation().getFile();try {jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}jfc.setCurrentDirectory(new File(jarFilePath));jfc.setFileFilter(filter);jfc.showSaveDialog(null);File fl = jfc.getSelectedFile();OutputStreamWriter osw;try {osw = new OutputStreamWriter(new FileOutputStream(fl));String text = textPane.getText();osw.write(text, 0, text.length());osw.flush();osw.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch block e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}

?

轉載于:https://www.cnblogs.com/hujunzheng/p/4399248.html

總結

以上是生活随笔為你收集整理的java JFileChooser选择文件和保存文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。