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

这篇博客讨论了如何在Java中创建一个文本编辑器,并实现将用户输入的内容保存为.txt文件的功能。通过使用JFileChooser对话框,允许用户选择保存文件的位置和名称。在保存过程中,对可能的异常进行了处理,确保文件能够正确写入并关闭。示例代码中修复了一些错误,并展示了如何使用JTextArea的write方法直接写入文件,以简化代码并处理行结束符。
摘要由CSDN通过智能技术生成

I am busy trying to make a word processor as one of my project and I need the text entered into the jTextArea to be saved as a .txt file with a name and location that the user chooses. note "fc" is the name of i file chooser i have already declared.

public class TextEditor extends javax.swing.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);

File fileName = new File(SaveAs.getSelectedFile() + ".txt");

try {

if (fileName == null) {

return;

}

BufferedWriter outFile = new BufferedWriter(new FileWriter(fileName));

outFile.write(n.getText()); //put in textfile

outFile.close();

} catch (IOException ex) {

}

}

解决方案

I would use the JTetArea's own write method as this will allow easy writing to file and will handle all line feeds nicely. For example (and to borrow your code):

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

}

}

}

}

}

You've got some bugs in your code. For e.g. this works,

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();

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值