Is there a way how i can display the contents of a binary file in a textArea?
TextArea
Collapse
X
-
No, i want to display the data not the one's and zero. Thats why i am having problems. Because since binary files are not in a human readable mode, i want that when they are displayed on the textArea they are human readable (not with the zeros). Is there a way how i could display them in this way?
Thanks.Comment
-
I know. So what do you want to do since you you know this as well.Originally posted by saytriWhen you open a binary file like in a text editor, you can't read data, because its not readable. It contains jumbled up text and characters. Its not like a textfile where you can edit or read the data contained.Comment
-
You question is the equivalent of asking "how long is a piece of string"? Only you know the format of the file. Unless you don't, either!Comment
-
The problem is that i don't know how to call a binary file, to be displayed in a textArea. I know how to display a textfile in a textArea, but since binary files contain unreadable data, i can't display the contents of a binary file like i display a textfile. When i tried to display the contents of the binary file like i display them when they are stored in a textfile, it displays jumbled up characters (the way binary file stores them). I want to display the contents in a textfile in a readable mode.
Sorry guys if i'm not making myself clear. Hope that this would make it clearer. Thanks a lot.Comment
-
-
No, its just i am doing like a quiz. And my tutor told me that instead of saving the scores in a textfile i should store them in a binary file since binary files are more secure. And i want to display these scores into a JTextArea. I'm still a beginner and so i'm not just of an expert in java. Hope that this would give a more clearer idea of what i'm doing. Thanks a lot, and sorry for the trouble.Comment
-
Take the code you used to write data to a file and reverse it. Now you've recovered the data structure and you can display it as you like.Originally posted by saytriNo, its just i am doing like a quiz. And my tutor told me that instead of saving the scores in a textfile i should store them in a binary file since binary files are more secure. And i want to display these scores into a JTextArea. I'm still a beginner and so i'm not just of an expert in java. Hope that this would give a more clearer idea of what i'm doing. Thanks a lot, and sorry for the trouble.Comment
-
Ok thanks a lot. I tried doing this, but instead of displaying the contents of the binary file (i.e the scores) it just displays "players.da t". What am i doing wrong? Thanks a lot. I really appreciate all your help.
Code:import java.awt.BorderLayout; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import javax.swing.JDialog; import javax.swing.JTextArea; import java.awt.*; import javax.swing.*; public class Scores { private JTextArea textArea; String[] scores = null; static final String dataFile1 = "players.dat"; public Scores() { super(); try{ JDialog dialog = new JDialog(); dialog.getContentPane().setLayout(new BorderLayout(10, 10)); Test4Rx imagePanel = new Test4Rx("scores.gif"); dialog.add(imagePanel, "North"); dialog.add(new Test4Rx("scores.gif")); File aFile = new File( "players.dat" ); // create an output stream to the file FileInputStream aFileInStream = new FileInputStream ( aFile ); // create a data output stream to the file output stream DataInputStream aDataInStream = new DataInputStream ( aFileInStream ); textArea = new JTextArea(); String string = dataFile1; textArea.append(string + "\n"); textArea.setBackground(Color.orange); textArea.setFont(new Font("Times New Roman", Font.BOLD, 14)); textArea.setEditable(false); textArea.setForeground(Color.red); dialog.getContentPane().add(BorderLayout.CENTER, textArea); dialog.setTitle("Scores"); dialog.setSize(350, 350); dialog.setVisible(true); }catch (java.io.FileNotFoundException f) { JOptionPane.showMessageDialog(null, "File not found."); }catch (Exception e){ e.printStackTrace(); } } public static void main(String[] args){ new Scores(); } }Comment
-
I've edited out irrelevant lines. Why the text area displays "players.da t" should be clear now.Originally posted by saytribut instead of displaying the contents of the binary file (i.e the scores) it just displays "players.da t".
[CODE=Java]
dataFile1 = "players.da t";
...
String string = dataFile1;
textArea.append (string + "\n");
[/CODE]Comment
-
Did you understand my advice in reply #11:Originally posted by saytriOk i understood that(why its displaying players.dat. But how can i append the file, so it would display its contents. Because i can't figure out how to call the file to be displayed in the textArea. Thanks a lot for your help.
Take the code you used to write data to a file and reverse it. Now you've recovered the data structure and you can display it as you like.Comment
Comment