gpt4 book ai didi

java - 在从 JTextArea 获取文本的文档中保存文件时如何修复新行

转载 作者:行者123 更新时间:2023-11-29 06:56:04 26 4
gpt4 key购买 nike

import java.awt.BorderLayout;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Formatter;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FileDemonstrationJFileChooser extends JFrame {
private JTextArea outputArea;
private JScrollPane scrollPane;
JFileChooser fileChooser = new JFileChooser();

public FileDemonstrationJFileChooser() {
// TODO Auto-generated constructor stub
super("Testing class file");
outputArea = new JTextArea();
scrollPane = new JScrollPane(outputArea);
add(scrollPane, BorderLayout.CENTER);
setSize(400, 400);
setVisible(true);

analyzePath();

if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// save to file
String toSave = outputArea.getText();
try {
Formatter writer = new Formatter( new File(file+".txt"));
writer.format(toSave); //Problems here occurs
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(this,
"Message saved.(" + file.getName() + ")", "File Saved",
JOptionPane.INFORMATION_MESSAGE);

}
}

private File getFileOrDirectory() {

fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.CANCEL_OPTION) {
System.exit(1);
}
File fileName = fileChooser.getSelectedFile();
if ((fileName == null) || (fileName.getName().equals(""))) {
JOptionPane.showMessageDialog(this, "Invalid name", "Invalid name",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
return fileName;
}

private void analyzePath() {
File name = getFileOrDirectory();
if (name.exists()) {
outputArea.setText(String.format(
"%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s", name
.getName(), " exists", (name.isFile() ? "is a file"
: "is not a file"),
(name.isDirectory() ? "is a directory"
: "is not a directory"),
(name.isAbsolute() ? "is absolute path"
: "is not absolute path"), "Last modified: ", name
.lastModified(), "Length: ", name.length(),
"Path: ", name.getPath(), "Absolute path: ", name
.getAbsolutePath(), "Parent: ", name.getParent()));

if (name.isDirectory()) {
String[] directory = name.list();
outputArea.append("\n\nDirectory contents:\n");

for (String directoryName : directory) {
outputArea.append(directoryName + "\n");
}
}
} else {
JOptionPane.showMessageDialog(this, name + " does not exist.",
"Error", JOptionPane.ERROR_MESSAGE);
}

}
}

我的代码用JFileChooser 写入一个文件并保存在硬盘中。但是在写入文件的时候出现问题,没有取新行。它将整个文本区域的文本保存在一行中。怎么解决??

这里,当我保存文件时,它仅将其内容保存在一行中,而不是像在 JTextArea 中那样。也就是说,新行不会出现。它将所有内容保存在一行中。那我该如何解决呢??

最佳答案

Document 只会存储“\n”来表示换行符。根据您使用的文本组件,getText() 方法会将“\n”替换为适用于您的平台 (JTextPane) 的换行字符串或保留“\n”在文本中(JTextArea)。查看Text and New Lines获取更多信息。

不要使用格式化程序来编写文本。格式化程序不知道文本来自何处。

而是使用 JTextArea 提供的 write() 方法:

FileWriter writer = new FileWriter( new File(...) );
BufferedWriter bw = new BufferedWriter( writer );
textArea.write( bw );
bw.close();

write() 方法知道如何处理行尾字符串。

关于java - 在从 JTextArea 获取文本的文档中保存文件时如何修复新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33896148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com