0

So I'm trying to read a file into a fileinputstream, then read it byte by byte, converting them to Hexadecimals and outputting them in the JText area. The if = 15 doesn't matter at this point, thats just demonstrating when to new line but I didn't implement it yet. However, for whatever reason nothing will write into the JTextarea when I append it.

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


        }
  • http://stackoverflow.com/questions/14648635/append-a-string-value-from-thread-class-to-jtextarea – harshit Mar 11 '14 at 23:24
  • did you try debugging? – Jean-Paul Mar 11 '14 at 23:24
  • 2
    Why are you opening an output stream to the same file you are reading? This could corrupt your file... – MadProgrammer Mar 11 '14 at 23:32
  • 1
    One thing I see is `try { bin = new int[23123123]; String[] hexChars = new String[bin.length * 2];`. Where's the catch block? This is allocating 200MB+ on the heap, the heap size is typically smaller than this by default. It could be throwing OutOfMemoryError. – Radiodef Mar 11 '14 at 23:35
  • 1
    Are you calling this in the EDT? – wumpz Mar 11 '14 at 23:40
  • Ah, I assumed I would need an outputstream at some point. Although removing it I still couldn't get it to run. – user3316794 Mar 11 '14 at 23:47
  • Can you show us your catch block? @wumpz also brings up a good point that if this is long-running, nothing will appear to happen initially. Also verify that your file hasn't been erased after opening the output stream from before. In general, we need to see more of your code. The catch block, how this routine is called (inside an event?), etc... – Radiodef Mar 11 '14 at 23:50

1 Answers1

2

It's probably just me, but, these...

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

Seem weird to me. I don't know why you would be opening an OutputStream to a File which you've just opened an InputStream to...The int buffer seems overly large and the hexChars never need need to exceed 16 characters, not to mention the fact that you are completely ignore the int buffer anyway...

Your try-catch is a little awkward to. Generally you should be providing a finally block which is used to close any resources, like streams, that you openeded, or your could use the try-with-resource feature introduced in Java 7...

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

Your file reading process also seems a little awkward, for example, the following seems overtly complicated...

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

And could be reduced to...

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

But that's a nit pick ;)

You're not adding any new line characters to the text area nor are you taking into consideration the potential that the hexchars may contain a partial result when you finish reading the file.

Since you are reading each int from the file one by one, a better solution might be to just convert each int to a hex value and append it to the JTextArea (or maybe a StringBuilder depending on your requirements)

For example

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

}

There could be any number of reasons why it's not appearing in your JTextArea, you could have forgotten to add it to any visible window or you could be shadowing your variables...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hmm....I tried implementing the changes, and adding a static piece of text to ensure text was loading on the jtextarea at all, and it was. but the output isn't showing up still. So I'm not sure what the deal is.I'll have to mess around and try to figure it out – user3316794 Mar 12 '14 at 00:40
  • Unless you can provide an over all runnable example that demonstrates your issue, that's about as far as we can get you – MadProgrammer Mar 12 '14 at 00:42
  • well, this is my class I've got, and then just a runner main class that sets it to visible. – user3316794 Mar 13 '14 at 01:06
  • edit: god damn I'm dumb. so I just tried it with a docx file and it sent stuff back. so it just doesn't like .txt – user3316794 Mar 13 '14 at 01:08