Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.EventQueue;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.nio.charset.Charset;
- import javax.swing.JButton;
- import javax.swing.JEditorPane;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class TextWriter {
- private JFrame frame;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- TextWriter window = new TextWriter();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public TextWriter() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setTitle("TextWriter");
- frame.setBounds(100, 100, 500, 350);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- JEditorPane editorPane = new JEditorPane();
- editorPane.setBounds(10, 10, 464, 258);
- frame.getContentPane().add(editorPane);
- JButton button_Save = new JButton("Save");
- button_Save.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- // 建立 fcSaveAs (JFileChooser),用以設定儲存位置
- JFileChooser fcSaveAs = new JFileChooser();
- // 設定 fcSaveAs 視窗中的標題
- fcSaveAs.setDialogTitle("Save as ..");
- // 當按下 fcSaveAs 視窗中的 [確認] 按鈕
- if(fcSaveAs.showSaveDialog(fcSaveAs) == JFileChooser.APPROVE_OPTION) {
- if (WriteTextToFile(editorPane.getText(), fcSaveAs.getSelectedFile().getAbsolutePath(), "UTF-8", false)) {
- JOptionPane.showMessageDialog(null, "Write file successfully.\n", frame.getTitle(), JOptionPane.INFORMATION_MESSAGE );
- } else {
- JOptionPane.showMessageDialog(null, "Write file faild.\n", frame.getTitle(), JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- });
- button_Save.setBounds(10, 278, 87, 23);
- frame.getContentPane().add(button_Save);
- }
- /**
- * 寫入文字檔案至指定路徑
- *
- * @param fileContext 寫入檔案之文字內容
- * @param filePath 寫入檔案之絕對路徑或相對路徑
- * @param encoding 寫入檔案之編碼格式
- * @param isAppend 是否將文字內容添加至檔案尾端
- * @return 回傳寫入文字檔案結果
- */
- private boolean WriteTextToFile(String fileContext, String filePath, String fileEncoding, boolean isAppend) {
- // 先建立檔案
- // fcSaveAs.getSelectedFile().getAbsolutePath() 可取得 fcSaveAs 中的絕對路徑
- File file = new File(filePath);
- try {
- // 開始寫入檔案
- BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, isAppend), Charset.forName(fileEncoding)));
- bufWriter.write(fileContext);
- bufWriter.close();
- return true;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment