TextArea

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saytri
    New Member
    • Dec 2007
    • 35

    TextArea

    Is there a way how i can display the contents of a binary file in a textArea?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by saytri
    Is there a way how i can display the contents of a binary file in a textArea?
    A Texarea displays text. Do you want to display the binary representation( ones and zeros) of the file?

    Comment

    • saytri
      New Member
      • Dec 2007
      • 35

      #3
      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

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by saytri
        .. 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 ..
        ? ?

        Comment

        • saytri
          New Member
          • Dec 2007
          • 35

          #5
          When 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

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by saytri
            When 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.
            I know. So what do you want to do since you you know this as well.

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              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

              • saytri
                New Member
                • Dec 2007
                • 35

                #8
                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

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  You are still leaving us guessing. Are you trying to write a hex editor?

                  Comment

                  • saytri
                    New Member
                    • Dec 2007
                    • 35

                    #10
                    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

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by saytri
                      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.
                      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

                      • saytri
                        New Member
                        • Dec 2007
                        • 35

                        #12
                        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

                        • BigDaddyLH
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1216

                          #13
                          Originally posted by saytri
                          but 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]
                          I've edited out irrelevant lines. Why the text area displays "players.da t" should be clear now.

                          Comment

                          • saytri
                            New Member
                            • Dec 2007
                            • 35

                            #14
                            Ok 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.

                            Comment

                            • BigDaddyLH
                              Recognized Expert Top Contributor
                              • Dec 2007
                              • 1216

                              #15
                              Originally posted by saytri
                              Ok 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.
                              Did you understand my advice in reply #11:
                              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

                              Working...