[Java] 简易记事本 →→→→→进入此内容的聊天室

来自 , 2019-08-05, 写在 Java, 查看 111 次.
URL http://www.code666.cn/view/b197ffde
  1. private void saveFile(){
  2.                 FileDialog fd = new FileDialog(this,"Save File");
  3.                
  4.                 //设置文件后缀
  5.                 fd.setFile("untitled.txt");
  6.                
  7.                 //设置为保存模式
  8.                 fd.setMode(FileDialog.SAVE);
  9.                 fd.setVisible(true);
  10.                
  11.                 //获取文件名
  12.                 String fileName = fd.getFile();
  13.                
  14.                 //获取对话框当前目录
  15.                 String dir = fd.getDirectory();
  16.                
  17.                 //根据目录名文件名创建一个文件
  18.                 File newFile = new File(dir + File.separator + fileName);
  19.                 PrintWriter pw = null;
  20.                 try{
  21.                         pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(newFile)));
  22.                         String str = editArea.getText();
  23.                         pw.println(str);
  24.                         pw.flush();
  25.                 }catch(IOException e){
  26.                         e.printStackTrace();
  27.                 }finally{
  28.                         pw.close();
  29.                 }
  30.         }
  31.        
  32.         //打开文件目录或文件
  33.         private void openDirOrFile(String absolutePath){
  34.                 File file = new File(absolutePath);
  35.                
  36.                 //判断文件是否存在
  37.                 if(!(file.exists())){
  38.                         editArea.setText("The file does not exist!");
  39.                 }else if(file.isDirectory()){
  40.                         //判断是否是同一个目录
  41.                         editArea.setText(null);
  42.                         showDir(file);
  43.                 }else if (file.isFile()){
  44.                         //判断是否是同一个文件
  45.                         try{
  46.                                 FileInputStream fis = new FileInputStream(file);
  47.                                 BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  48.                                 String str = null;
  49.                                 editArea.setText(null);
  50.                                 while((str = br.readLine())!= null){
  51.                                         editArea.append(str + "\t\n");
  52.                                 }
  53.                         }catch(IOException e){
  54.                                 e.printStackTrace();   
  55.                         }
  56.                 }
  57.         }
  58.         //浏览目录,建立树形图
  59.         private void showDir(File directory){
  60.                 File[] files = directory.listFiles();
  61.                 int len = files.length;
  62.                 for(int i = 0; i < len; i++){
  63.                         if(files[i].isDirectory())
  64.                         {
  65.                                 for(int j = 0; j < this.level; j++)
  66.                                 {
  67.                                         editArea.append("    ");
  68.                                 }
  69.                                 editArea.append("|-- " + files[i].getName() + " (Folder)\r\n");
  70.                                 this.level++;
  71.                                 showDir(files[i]);
  72.                                 this.level--;
  73.                         }
  74.                         else if(files[i].isFile())
  75.                         {
  76.                                 for(int j = 0; j < this.level; j++)
  77.                                 {
  78.                                         editArea.append("    ");
  79.                                 }
  80.                                 editArea.append("|-- " + files[i].getAbsolutePath() + "\r\n");
  81.                         }
  82.                 }
  83.         }
  84.        

回复 "简易记事本"

这儿你可以回复上面这条便签

captcha