java_savedialog應用及逐行讀取JTe…

public void SaveScript() throws IOException
 {
  File file = null;

 //save file dialog
  static JFileChooser saveChooser = new JFileChooser();
  int result = saveChooser.showSaveDialog(buttonScript);
  if(result ==JFileChooser.APPROVE_OPTION)
  {
   saveChooser.addChoosableFileFilter(null);
   file = saveChooser.getSelectedFile();
   String filePath = file.getAbsolutePath();
   filePath = filePath.toString();
   

   //get the content from the scriptText which input with someone
   inputValue = scriptText.getText();
   String[] inputValues = inputValue.split(" ");
   StringBuffer inputValuesn = new StringBuffer();
   for (String x: inputValues){
    inputValuesn.append(x + " ");
   }
   outPut(filePath,inputValuesn.toString());

   }
  }


 //the action of savescript
 public void actionPerformed(ActionEvent e) {
  if (e.getSource() == buttonScript){
   try {
    SaveScript();
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
  }
 } 
 public void outPut(String path, String s) {
     File file = new File(path);
     try {
       PrintWriter out = new PrintWriter(file);
       out.write(s);
       out.flush();
       System.out.println("OK...");
       out.close();
     } catch (Exception e) {
       e.printStackTrace();
     }
 } 

發佈了76 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章