栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将文本从jTextArea保存(即另存为)到新的.txt文件中

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将文本从jTextArea保存(即另存为)到新的.txt文件中

我将使用JTetArea自己的write方法,因为这将使写入文件变得容易,并将很好地处理所有换行。例如(并借用您的代码):

public class TextEditor extends Jframe {   int count = 2;   Jtextarea n = new Jtextarea();   final JFileChooser fc = new JFileChooser();   public void SaveAs() {      final JFileChooser SaveAs = new JFileChooser();      SaveAs.setApproveButtonText("Save");      int actionDialog = SaveAs.showOpenDialog(this);      if (actionDialog != JFileChooser.APPROVE_OPTION) {         return;      }      File fileName = new File(SaveAs.getSelectedFile() + ".txt");      BufferedWriter outFile = null;      try {         outFile = new BufferedWriter(new FileWriter(fileName));         n.write(outFile);   // *** here: ***      } catch (IOException ex) {         ex.printStackTrace();      } finally {         if (outFile != null) { try {    outFile.close(); } catch (IOException e) {    // one of the few times that I think that it's OK    // to leave this blank }         }      }   }}

您的代码中有一些错误。例如,这可行

import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.io.*;import javax.swing.*;import javax.swing.filechooser.FileNameExtensionFilter;@SuppressWarnings("serial")public class TextEditor extends Jframe {   int count = 2;   Jtextarea textarea = new Jtextarea(10, 30);   final JFileChooser fc = new JFileChooser();   public TextEditor() {      add(new JScrollPane(textarea));      add(new JPanel(){{add(new JButton(new AbstractAction("Save As") {         @Override         public void actionPerformed(ActionEvent arg0) { saveAs();         }      }));}}, BorderLayout.SOUTH);   }   public void saveAs() {      FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("Text File", "txt");      final JFileChooser saveAsFileChooser = new JFileChooser();      saveAsFileChooser.setApproveButtonText("Save");      saveAsFileChooser.setFileFilter(extensionFilter);      int actionDialog = saveAsFileChooser.showOpenDialog(this);      if (actionDialog != JFileChooser.APPROVE_OPTION) {         return;      }      // !! File fileName = new File(SaveAs.getSelectedFile() + ".txt");      File file = saveAsFileChooser.getSelectedFile();      if (!file.getName().endsWith(".txt")) {         file = new File(file.getAbsolutePath() + ".txt");      }      BufferedWriter outFile = null;      try {         outFile = new BufferedWriter(new FileWriter(file));         textarea.write(outFile);      } catch (IOException ex) {         ex.printStackTrace();      } finally {         if (outFile != null) { try {    outFile.close(); } catch (IOException e) {}         }      }   }   private static void createAndShowGui() {      TextEditor frame = new TextEditor();      frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);      frame.pack();      frame.setLocationByPlatform(true);      frame.setVisible(true);   }   public static void main(String[] args) {      SwingUtilities.invokeLater(new Runnable() {         public void run() { createAndShowGui();         }      });   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470292.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号