簡體   English   中英

為什么不寫我的JTextArea

[英]Why isn't this writing to my JTextArea

所以我試圖將文件讀入fileinputstream,然后逐字節讀取,將它們轉換為十六進制並在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打開到你剛剛打開一個InputStreamFile ...... int緩沖區似乎過大而且hexChars永遠不需要超過16字符,更不用說無論如何你完全忽略了int緩沖區......

你的try-catch有點尷尬。 通常,您應該提供一個finally塊,用於關閉您打開的任何資源,如流,或者您可以使用Java 7中引入的try-with-resource功能...

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 ,您可能忘記將其添加到任何可見窗口中,或者您可能會隱藏變量...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM