0

I am a beginner in JAVA. This is my first question in this forum.I am developing a hex editor tool for a project. As a part of it, I have to make a small application which should open a text file and read contents of it and display it in an editor area. Then it should also generate hash value for the text in the text file using the cartographic algorithms like SHA-256 etc. I found a code in the internet which was so helpful. I am trying to reuse it. I got stuck up in displaying the contents of the text file into the editor. The code goes like this.

   public Test() throws IOException {

    // passes the number of array elements to the 
    // editor.

    byte[] ar;
    ar = new byte[16 * 16 * 100];
    Arrays.fill(ar, (byte) 0);


    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    ObjectOutputStream oos=new ObjectOutputStream(bos);


    win = new JFrame("Hex Editor");
    win.setSize(654, 473);

    JButton btnOpenFile = new JButton("Open File");
    btnOpenFile.setBounds(67, 38, 91, 23);
    win.getContentPane().add(btnOpenFile);
    btnOpenFile.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            Fileopener opener = new Fileopener();
            //returns the string value through getpause() method.
                            System.out.println(opener.getPause());
        }
    });

oos.writeObject("kirandfasnvcxnz.,mvnmc,xznvmcxzmnvcmxzcccbnxz cz hajk vc jbcvj xbnzvc sbj cvxz,bcxjnzbcvjhs avcjz cxmzncvxz ");
ar=bos.toByteArray();       

I defined the method for fileopener a another class file. which during execution is returning the contents of the text file. My question is how to how to pass a string value that is being read from a text file to this method. So that it displays in the editor.

oos.writeObject("kirandfasnvcxnz.,mvnmc,xznvmcxzmnvcmxzcccbnxz cz hajk vc jbcvj xbnzvc sbj cvxz,bcxjnzbcvjhs avcjz cxmzncvxz ");

The editor is displaying the contents with the above line of code. I tried to do it by calling a getter method which returns the string value of the text read. The method I used for this is

oos.writeObject("Fileopener.getPause()");

which is not displaying the desired content of the text file. Instead the editor is displaying the function inside the braces in the editor(I guess it has been read as string). Thanks for your help in advance. @ sgmorrison The following is the code of Fileopener() `

     package hexeditor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFileChooser;

public class Fileopener {

    static String pause;

    /**
     * 
     */
    public Fileopener() {
        super();
        // TODO Auto-generated constructor stub

        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(null);

        StringBuffer contents = new StringBuffer();

        BufferedReader inFile = null;

        if (returnVal == JFileChooser.APPROVE_OPTION) {

            File f = chooser.getSelectedFile();

            try {

                inFile = new BufferedReader(new FileReader(f));

                String text = null;

                while ((text = inFile.readLine()) != null) {

                    contents.append(text)

                    .append(System.getProperty(

                    "line.separator"));

                }

            }

            catch (FileNotFoundException e1) {

                e1.printStackTrace();

            } catch (IOException e1) {

                e1.printStackTrace();

            } finally {

                try {

                    if (inFile != null) {

                        inFile.close();

                    }

                } catch (IOException e1) {

                    e1.printStackTrace();

                }

            }

            // show file contents here
            pause = contents.toString();
            setPause(pause);
                   }

    }
         public static String getPause() {
        return pause;
    }

    public void setPause(String pause) {
        this.pause = pause;
    }

}

`

kirankar
  • 39
  • 1
  • 6

1 Answers1

0

If your code is exactly as written (oos.writeObject("Fileopener.getPause()");) then you are passing a String to writeObject with value "Fileopener.getPause()". You are not calling a method on fileopener, but creating a new String object that looks a bit like a method call. To see this is the case, try replacing the line with oos.writeObject("Fileopener.getNonExistentMethod()"); and see what happens.

To correct this issue, remove the quotes from your call:

oos.writeObject( Fileopener.getPause() );

UPDATE Having seen the code for Fileopener I now see what is likely to be another issue. Fileopener's pause field is declared as static, meaning that the same value of the property is available to all instances of the class, and is also available as a property of the class itself. getPause is also declared static, so the same rules apply.

In your comment you mention the following code:

    Fileopener opener = new Fileopener(); 
    System.out.println(opener.getPause());

Creating a new Fileopener causes pause to be set to include the file contents. Without calling new Fileopener(), pause never gets set, but you can still access it with getPause.

Since it only makes sense to read the value of pause once you've read the file, I'd recommend removing static from pause and getPause. You'll then have to change your troublesome code to:

    Fileopener opener = new Fileopener(); 
    oos.writeObject(opener.getPause());
sgmorrison
  • 948
  • 7
  • 11
  • Thanks for your reply. I tried both of your suggestions, first with `oos.writeObject(Fileopener.getPause());` without the quotes which did not display anything in the editor. I also tried `os.writeObject("Fileopener.getNonExistentMethod()");` in this case the result is the creation of new string object and the `Fileopener.getNonExistentMethod() ` is displayed in the editor area rather than displaying the text read from the text file. – kirankar Apr 08 '12 at 23:01
  • If `oos.writeObject(Fileopener.getPause());` caused no text to be displayed then `Fileopener.getPause()` is not returning any text to display. Try inspecting what is returned by this method. If you cannot get further, share the code for Fileopener so we can give more help. – sgmorrison Apr 09 '12 at 06:52
  • I tried it by including the System.out.println(Fileopener.getPause()); in the code which is returning null. When I tried to create an instance of Fileopener() like Fileopener opener = new Fileopener(); System.out.println(opener.getPause()); it is returning the text that has been read from the text file into the console. I included the code of Fileopener() in the question. – kirankar Apr 09 '12 at 11:06