- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个文本文件,其中包含如下内容:
Hello, my name is Joe
What is your name?
My name is Jack.
That is good for you.
唯一的问题是我必须使用append方法将其加载到JTextArea中才能在JScrollPane中显示文本,如下所示:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
但是当我将文件读入文本区域时,文本区域显示如下内容:
Hello, my name is JoeWhat is your name?My name is Jack.That is good for you.
BufferedReader 从不将换行符 (\n) 读入 JTextArea。如何让读者添加文件中出现的空格和空行?如果有人可以提供帮助,我将不胜感激。谢谢!
最佳答案
所有 JTextComponent 都能够读取文本文件和写入文本文件,同时完全尊重当前操作系统的换行符,并且使用此功能通常是有利的。在您的情况下,您将使用 JTextArea 的 read(...) 方法来读取文件,同时充分理解文件系统的 native 换行符。像这样:
BufferedReader br = new BufferedReader(new FileReader(file));
textArea.read(br, null);
或者更完整的示例:
import java.io.*;
import javax.swing.*;
public class TextIntoTextArea {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private static void createAndShowGui() {
JFileChooser fileChooser = new JFileChooser();
int response = fileChooser.showOpenDialog(null);
if (response == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
final JTextArea textArea = new JTextArea(20, 40);
textArea.read(br, null); // here we read in the text file
JOptionPane.showMessageDialog(null, new JScrollPane(textArea));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
}
}
}
}
}
}
关于JavaIO : Reading text files as they are seen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9857688/
这个警告有什么可担心的吗?我读过它会导致不稳定的行为? 这是我正在尝试编译的示例,有人可以向我解释为什么作者将对象声明为类,然后 typedef 将其转换为结构吗?如果类(class)是POD,这样做
当用户打开页面时,我有一个简单的 AJAX 调用开始在后台加载图像: $('.image-container').load('/images/portfolio/'); 这会加载每个图像的副本,一个小
我在我的 Java/Maven 项目中使用 Jackson 注释。我有多个 Jackson 注释,但我最近添加的 @JsonAlias 没有看到。失败类的代码: ... import com.fast
在我进行大量元编程的库的深处,有一个包含以下代码的类: // Initialized in the constructor // Destroyed in IDisposable.Dispose pr
我有一个在 Chrome 中运行良好的代码: #menu ul { list-style-position: inside; list-style-type: none; display: block
我想在我的伪元素中检索内容的值。 script = "return window.getComputedStyle(document.querySelector('small.fa.text-mute
我制作了一个使用 CSS3 和 HTML 的新登录/注册模板,但在此之前我有一个工作表单,但非常基础。所以我决定制作另一个“表格”,它看起来应该是这样的: http://www.script-tuto
我正在尝试使用 java 的默认 pop3 实现,但无法让它读取标志的实际状态(我认为)。 这是(缩写的)代码: Store store = null; Folder folder = null; t
template void ComputeGenericDropCount(const function &func) { Wcf::ForEach([&](Wcf *wcf) {
我希望有一条线索可以帮助跟踪我希望为用户保留的值(value)。如果用户没有输入他们的名字,我想显示一个表单让他们输入他们的名字以用于查找。 我希望能够检查该名称是否在路径上。如果名称在路径上,则显示
在 Stack Overflow 上,个人资料页面列出了“上次查看”属性。这似乎并没有在每个页面 View 上更新(显然是出于性能原因)。您将如何在大流量网络应用程序中实现它?您会只在某些页面上更新吗
我只是想知道是否有一个可与 Ejabberd 一起使用的用于消息传递收据的 Strope 插件? 我想了解消息传递回执是如何工作的,Strope 有任何文档/插件吗? 最佳答案 实现Message D
我有一个包含主机和时间戳的映射(以及其他字段),并且希望获得一些最近见过的主机的“最后见面时间”。有没有比在主机字段上先进行术语刻面,然后对按降序排列的每个术语值进行范围查询(limit = 1)更好
我有一个文本文件,其中包含如下内容: Hello, my name is Joe What is your name? My name is Jack. That is good for you. 唯
我想显示用户最后一次在线的时间,比如 whatsapp使用 xmpp 和 Angular 我发出了一个 xmpp 请求,我得到了最后一次看到的用户,我想将其转换为时间 我得到了什么: 用户最后一次出现
确保 IMAP 客户端与其服务器同步的一种方法是利用 SEEN 标志(例如 Library for IMAP IDLE )。 我自己还没有使用过这个,但我想知道设置 SEEN 标志是否基本上将消息设置
我想使用 JS 和 PHP 实现像在 FB 组中这样的功能看到的帖子,我设法从滚动操作中获得看到的计数,但我需要知道用户是否只是向下滚动它或实际上花时间阅读它(某种滚动速度跟踪器或类似的东西) v
我有一个提醒菜单来向用户显示新内容。它是像 ebay 一样的 PHP 系统。 所以我有 sell 表: id seller customer product_name date 我想要的是为每个用户显
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我正在尝试使用 Python IMAP 库将电子邮件设置为已读。就像在这个例子中一样: Python, IMAP and GMail. Mark messages as SEEN 我选择一个 GMai
我是一名优秀的程序员,十分优秀!