gpt4 book ai didi

java - 为什么这不是写到我的 JTextArea

转载 作者:行者123 更新时间:2023-12-02 06:05:21 25 4
gpt4 key购买 nike

所以我试图将文件读入文件输入流,然后逐字节读取它,将它们转换为十六进制并将它们输出到 JText 区域。 if = 15 此时并不重要,这只是演示何时换行,但我还没有实现它。但是,无论出于何种原因,当我追加 JTextarea 时,都不会写入任何内容。

try {
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(file);
bin = new int[23123123];
String[] hexChars = new String[bin.length * 2];
int hexcount = 0;
boolean done=false;
while(!done)
{
int next = in.read();
if(next == -1)
{
done=true;
}
else
{ hexChars[hexcount] = Integer.toHexString(next);
if (hexcount == 15)
{
for (int i = 0; i < 16; i++) {
textArea.append(hexChars[i]);
}
hexcount = 0;

} else
{
hexcount++;
}
count++;

}
}
filelbl.setText("Opened: " + file.getName() + "." );
in.close();
out.close();


}

最佳答案

这可能只是我的问题,但是,这些......

FileOutputStream out = new FileOutputStream(file);
bin = new int[23123123];
String[] hexChars = new String[bin.length * 2];

我觉得很奇怪。我不知道为什么您要打开一个 OutputStream 到一个 File,而您刚刚打开了一个 InputStream 到...int 缓冲区似乎太大,并且 hexChars 永远不需要超过 16 个字符,更不用说您完全忽略 int 无论如何缓冲...

你的try-catch有点尴尬。一般来说,您应该提供一个finally block ,用于关闭您打开的任何资源,例如流,或者您可以使用try-with-resource中引入的try-with-resource功能Java 7...

try (FileInputStream in = new FileInputStream(file)) {...

你的文件读取过程似乎也有点尴尬,例如,以下看起来过于复杂......

while (!done) {
int next = in.read();
if (next == -1) {

并且可以简化为...

int next = -1;
while ((next = in.read()) != -1) {...

但这是一个挑剔;)

您没有向文本区域添加任何新行字符,也没有考虑在读完文件后 hexchars 可能包含部分结果的可能性。

由于您正在从文件中一一读取每个 int,更好的解决方案可能是将每个 int 转换为 hex值并将其附加到JTextArea(或者可能是StringBuilder,具体取决于您的要求)

例如

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class HexViewer {

public static void main(String[] args) {
new HexViewer();
}

public HexViewer() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JTextArea ta = new JTextArea(10, 16);
readFile(new File("a file"), ta);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(ta));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public void readFile(File file, JTextArea textArea) {

try (FileInputStream in = new FileInputStream(file)) {

int colCount = 0;
int next = -1;
while ((next = in.read()) != -1) {

textArea.append(Integer.toHexString(next) + " ");
colCount++;
if (colCount > 15) {
textArea.append("\n");
colCount = 0;
}
}

} catch (IOException exp) {
exp.printStackTrace();
}
}

}

可能有多种原因导致它没有出现在您的 JTextArea 中,您可能忘记将其添加到任何可见窗口中,或者您可能会隐藏您的变量...

关于java - 为什么这不是写到我的 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22338802/

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