0

All I want to do is display the entire contents of a txt file. How would I go about doing this? I'm assuming that I will set the text of a JLabel to be a string that contains the entire file, but how do I get the entire file into a string? Also, does the txt file go in the src folder in Eclipse?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

4 Answers4

4

This code to display the selected file contents in you Jtext area

      static void readin(String fn, JTextComponent pane) 
              {
             try 
              {
               FileReader fr = new FileReader(fn);
               pane.read(fr, null);
               fr.close();
              }
                 catch (IOException e) 
                 {
                  System.err.println(e);
                 }
              }

To choose the file

         String cwd = System.getProperty("user.dir");
         final JFileChooser jfc = new JFileChooser(cwd);

            JButton filebutton = new JButton("Choose");
            filebutton.addActionListener(new ActionListener() 
            {
            public void actionPerformed(ActionEvent e) 
            {
                if (jfc.showOpenDialog(frame) !=JFileChooser.APPROVE_OPTION)

                        return;
                  File f = jfc.getSelectedFile();



            readin(f.toString(), textpane);

                  SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        frame.setCursor(Cursor.
                            getPredefinedCursor(
                            Cursor.DEFAULT_CURSOR));

                    }
                });
            }
        });
Murali
  • 774
  • 6
  • 12
  • The `fr.close()` statement should be in a finally block. And how is that part which sets the cursor of the frame relevant to the question ? – Robin Dec 02 '12 at 07:57
  • @Robin as this question related to getting the file name it is right and my answer is right. – Murali Dec 02 '12 at 08:10
  • 1
    @Murali Robins suggestions isn't about discusion about whatever – mKorbel Dec 02 '12 at 08:34
3

All I want to do is display the entire contents of a txt file. How would I go about doing this? I'm assuming that I will set the text of a JLabel to be a string that contains the entire file but how do I get the entire file into a string?

You would be better of using a JTextArea to do this. You can also look at the read() method.

does the txt file go in the src folder in Eclipse?

Nope. You can read files from any where. The tutorial on "Reading, Writing, and Creating Files" would be a good place to start

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sujay
  • 6,753
  • 2
  • 30
  • 49
2
  • create text file in your project's working folder
  • read your text file line by line
  • store the line contents in stringBuilder variable
  • then append next line contents to stringBuilder variable
  • then assign the content of your StringBuilder variable to the JLabel's text property

But it is not good idea to store whole file's data into JLabel, use JTextArea or any other text containers.

Read your file like this:

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
       line = br.readLine();
    }
    String everything = sb.toString();
} finally {
    br.close();
}

now assign value of everything to JLabel or JTextArea

JLabel1.text=everything;
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • 1
    And how will the "\n" help in case of a `JLabel` ? And I wasn't aware that `JLabel` had a protected `text` field (and it seems the javadoc isn't aware of this either) – Robin Dec 02 '12 at 07:54
1
  1. Use java.io to open a file stream.
  2. Read content from file by lines or bytes.
  3. Append content to StringBuilder or StringBuffer
  4. Set StringBuilder or StringBuffer to JLable.text.

But I recommend using JTextArea..

You don't need to put this file in src folder.

bhuang3
  • 3,493
  • 2
  • 17
  • 17