Holey_yan

[Java] TextWriter

May 22nd, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.EventQueue;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStreamWriter;
  9. import java.nio.charset.Charset;
  10. import javax.swing.JButton;
  11. import javax.swing.JEditorPane;
  12. import javax.swing.JFileChooser;
  13. import javax.swing.JFrame;
  14. import javax.swing.JOptionPane;
  15.  
  16. public class TextWriter {
  17.  
  18.     private JFrame frame;
  19.  
  20.     /**
  21.      * Launch the application.
  22.      */
  23.     public static void main(String[] args) {
  24.         EventQueue.invokeLater(new Runnable() {
  25.             public void run() {
  26.                 try {
  27.                     TextWriter window = new TextWriter();
  28.                     window.frame.setVisible(true);
  29.                 } catch (Exception e) {
  30.                     e.printStackTrace();
  31.                 }
  32.             }
  33.         });
  34.     }
  35.  
  36.     /**
  37.      * Create the application.
  38.      */
  39.     public TextWriter() {
  40.         initialize();
  41.     }
  42.  
  43.     /**
  44.      * Initialize the contents of the frame.
  45.      */
  46.     private void initialize() {
  47.         frame = new JFrame();
  48.         frame.setTitle("TextWriter");
  49.         frame.setBounds(100, 100, 500, 350);
  50.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.         frame.getContentPane().setLayout(null);
  52.        
  53.         JEditorPane editorPane = new JEditorPane();
  54.         editorPane.setBounds(10, 10, 464, 258);
  55.         frame.getContentPane().add(editorPane);
  56.        
  57.         JButton button_Save = new JButton("Save");
  58.         button_Save.addActionListener(new ActionListener() {
  59.             public void actionPerformed(ActionEvent arg0) {
  60.                 // 建立 fcSaveAs (JFileChooser),用以設定儲存位置
  61.                 JFileChooser fcSaveAs = new JFileChooser();
  62.                 // 設定 fcSaveAs 視窗中的標題
  63.                 fcSaveAs.setDialogTitle("Save as ..");
  64.                 // 當按下 fcSaveAs 視窗中的 [確認] 按鈕
  65.                 if(fcSaveAs.showSaveDialog(fcSaveAs) == JFileChooser.APPROVE_OPTION) {
  66.                    
  67.                     if (WriteTextToFile(editorPane.getText(), fcSaveAs.getSelectedFile().getAbsolutePath(), "UTF-8", false)) {
  68.                         JOptionPane.showMessageDialog(null, "Write file successfully.\n", frame.getTitle(), JOptionPane.INFORMATION_MESSAGE );
  69.                     } else {
  70.                         JOptionPane.showMessageDialog(null, "Write file faild.\n", frame.getTitle(), JOptionPane.ERROR_MESSAGE);
  71.                     }
  72.                 }
  73.             }
  74.         });
  75.         button_Save.setBounds(10, 278, 87, 23);
  76.         frame.getContentPane().add(button_Save);
  77.     }
  78.    
  79.     /**
  80.      * 寫入文字檔案至指定路徑
  81.      *
  82.      * @param fileContext   寫入檔案之文字內容
  83.      * @param filePath      寫入檔案之絕對路徑或相對路徑
  84.      * @param encoding      寫入檔案之編碼格式
  85.      * @param isAppend      是否將文字內容添加至檔案尾端
  86.      * @return              回傳寫入文字檔案結果
  87.      */
  88.     private boolean WriteTextToFile(String fileContext, String filePath, String fileEncoding, boolean isAppend) {
  89.         // 先建立檔案
  90.         // fcSaveAs.getSelectedFile().getAbsolutePath() 可取得 fcSaveAs 中的絕對路徑
  91.         File file = new File(filePath);
  92.         try {
  93.             // 開始寫入檔案
  94.             BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, isAppend), Charset.forName(fileEncoding)));
  95.             bufWriter.write(fileContext);
  96.             bufWriter.close();
  97.             return true;
  98.         } catch (IOException e) {
  99.             // TODO Auto-generated catch block
  100.             e.printStackTrace();
  101.             return false;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment