5 Replies Latest reply on Oct 11, 2006 12:13 AM by 807607

    opening files and viewing the text

    807607
      I'm trying to write a method to open a .doc file and display the text to screen, the program runs but nothing appears on the screen. Is the code below the correct way or is it flawed.
      class Open implements ActionListener
           {
                public void actionPerformed(ActionEvent event) 
                {
                     try {
                          
                     JTextPane view = app.setScreen();
                                         
                   BufferedReader in = new BufferedReader(new FileReader("outfilename.doc"));
                   String str;
                   while ((str = in.readLine()) != null) {
                        
                   view.setText(str);     
                  
              }
              in.close();
          } catch (IOException e) {
          }
                }
           }
        • 1. Re: opening files and viewing the text
          807607
          I'm trying to write a method to open a .doc file and
          display the text to screen, the program runs but
          nothing appears on the screen. Is the code below the
          correct way or is it flawed.
          class Open implements ActionListener
               {
                    public void actionPerformed(ActionEvent event) 
                    {
                         try {
                              
                         JTextPane view = app.setScreen();
                                             
          BufferedReader in = new BufferedReader(new
          w FileReader("outfilename.doc"));
               String str;
               while ((str = in.readLine()) != null) {
                    
               view.setText(str);     
          
          in.close();
          atch (IOException e) {
          }
          
               }
          Hard to tell why you aren't getting any output without more of the program.

          One thing I see is that this classes replaces the text of JTextPane with the last string read from the file. If the last string were a blank line then you would have nothing to show.

          If you are assuming this is a reasonable size document then you might consider appending each line read into as StringBuffer then set the text of the JTextPane to the contents of the StringBuffer after the reaches then end.
          • 2. Re: opening files and viewing the text
            camickr
            In the future Swing related questions should be posted in a Swing forum.
            I'm trying to write a method to open a .doc file and display the text to screen,
            If you mean a Word .doc file then you can't do this. A .doc file has a special format that JTextPane know nothing about.

            If you have a text file with a .doc extension, then change the extension so you don't confuse everybody. Then you just use the read(...) method of JTextPane to load the file into the text pane.
            • 3. Re: opening files and viewing the text
              807607
              I conducted a small test. All the text is being read and stored into the String str, it is just not being written to the screen.
              • 4. Re: opening files and viewing the text
                807607
                If you mean a Word .doc file then you can't do this.
                A .doc file has a special format that JTextPane know
                nothing about.
                It is a Word.doc file is there no way i can print the text to screen. my program is reading in the text but not displaying it?
                • 5. Re: opening files and viewing the text
                  807607
                  This might help:

                  http://jakarta.apache.org/poi/