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;
}
}
`