private void saveFile(){ FileDialog fd = new FileDialog(this,"Save File"); //设置文件后缀 fd.setFile("untitled.txt"); //设置为保存模式 fd.setMode(FileDialog.SAVE); fd.setVisible(true); //获取文件名 String fileName = fd.getFile(); //获取对话框当前目录 String dir = fd.getDirectory(); //根据目录名文件名创建一个文件 File newFile = new File(dir + File.separator + fileName); PrintWriter pw = null; try{ pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(newFile))); String str = editArea.getText(); pw.println(str); pw.flush(); }catch(IOException e){ e.printStackTrace(); }finally{ pw.close(); } } //打开文件目录或文件 private void openDirOrFile(String absolutePath){ File file = new File(absolutePath); //判断文件是否存在 if(!(file.exists())){ editArea.setText("The file does not exist!"); }else if(file.isDirectory()){ //判断是否是同一个目录 editArea.setText(null); showDir(file); }else if (file.isFile()){ //判断是否是同一个文件 try{ FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String str = null; editArea.setText(null); while((str = br.readLine())!= null){ editArea.append(str + "\t\n"); } }catch(IOException e){ e.printStackTrace(); } } } //浏览目录,建立树形图 private void showDir(File directory){ File[] files = directory.listFiles(); int len = files.length; for(int i = 0; i < len; i++){ if(files[i].isDirectory()) { for(int j = 0; j < this.level; j++) { editArea.append(" "); } editArea.append("|-- " + files[i].getName() + " (Folder)\r\n"); this.level++; showDir(files[i]); this.level--; } else if(files[i].isFile()) { for(int j = 0; j < this.level; j++) { editArea.append(" "); } editArea.append("|-- " + files[i].getAbsolutePath() + "\r\n"); } } }