1 2 Previous Next 26 Replies Latest reply: May 22, 2007 10:23 AM by 807606 RSS

    FAST file reading

    807606
      Hello,
      i need to read large text files, but method i wrote appears to be very slow and cannot be used for larger files.
      i have something like this:
      BufferedReader br = new BufferedReader(new FileReader(path));
      String line = "";
      StringBuffer sb = new StringBuffer();
      
      while( line = br.readline()   != null )
      {
      sb.append(line);
      }
      br.close();
      I am sure there is faster way to read. Thank you for help.
        • 1. Re: FAST file reading
          807606
          Do you actually want the entire file in memory like that?

          For large enough files you will simply run out of memory.
          • 2. Re: FAST file reading
            807606
            I dont need to read SO large files. I need to read like 100 kb max. But my method needs 15 secnds to read 55kb file.
            • 3. Re: FAST file reading
              807606
              I dont need to read SO large files. I need to read
              like 100 kb max. But my method needs 15 secnds to
              read 55kb file.
              Well you didn't answer the other question. Do you actually need it all at once? Or not?
              • 4. Re: FAST file reading
                807606
                > But my method needs 15 secnds to read 55kb file.

                That's not normal.

                ~
                • 5. Re: FAST file reading
                  807606
                  I need to read it to the TextPane
                  • 6. Re: FAST file reading
                    807606
                    > But my method needs 15 secnds to read 55kb
                    file.


                    That's not normal.

                    ~
                    We don't know how long his lines are....
                    • 7. Re: FAST file reading
                      807606
                      > We don't know how long his lines are....

                      That's a fair cop. Thanks for taking my assumption and beating me about the face and shoulders with it.

                      :o)

                      ~
                      • 8. Re: FAST file reading
                        807606
                        Here's the old JTextAreaPrintStream for those extra long lines...
                        import java.io.OutputStream;
                        import java.io.PrintStream;
                        import javax.swing.*;
                        
                        public class JTextAreaPrintStream extends PrintStream {
                            
                            public JTextAreaPrintStream(JTextArea textArea){
                                super(new JTextAreaOutputStream(textArea));
                            }
                        
                            private static class JTextAreaOutputStream extends OutputStream {
                                private JTextArea textArea;
                        
                                private JTextAreaOutputStream(JTextArea textArea) {
                                    this.textArea = textArea;
                                }
                        
                                public void write(int i){
                                    textArea.append(new String(new char[] {(char) i}));
                                }
                        
                                public void write(byte[] b){
                                    textArea.append(new String(b));
                                }
                        
                                public void write(byte[] b, int offset, int len){
                                    textArea.append(new String(b, offset, len));
                                }
                            }  
                            
                            public static void main(String[] args) {
                                doDemo();
                            }
                            
                            static void doDemo() {
                                JFrame f = new JFrame("JTextAreaPrintStreamDemo");
                                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        
                                JTextArea display = new JTextArea();
                                display.setEditable(false);
                        
                                JTextAreaPrintStream newOut = new JTextAreaPrintStream(display);
                                System.setOut(newOut);
                        
                                f.add(new JScrollPane(display));
                                f.setSize(300, 300);
                                f.setVisible(true);
                        
                                System.out.println("How's this?");
                                for (int i = 0; i < 5; System.out.println(i++ + 1));
                            }
                        }
                        ~
                        • 9. Re: FAST file reading
                          807606
                          I thing that much slowdown comes from creating new instance of string each time a line is read. There must be some way to avoid that.

                          (When value of string variable is changed, new instance of string is created)
                          • 10. Re: FAST file reading
                            807606
                            > I thing that much slowdown comes from creating new
                            instance of string each time a line is read.

                            Nah. A 55k file isn't going to be that big a deal unless you have no RAM.

                            ~
                            • 11. Re: FAST file reading
                              807606
                              Yeah... I was thinking that shorter lines are more of a problem here.

                              More iterations = slower.

                              Especially if you could grab a larger buffer.

                              I would read as large a chunks as you can. 4kb at least.
                              • 12. Re: FAST file reading
                                807606
                                I am getting a little bit fonfused. So what is the fastest way to read text file to a JTextPane?
                                • 13. Re: FAST file reading
                                  807606
                                  > Yeah... I was thinking that shorter lines are more of a problem here.

                                  Okay, now I'm curious. I'm going to write a test...
                                  • 14. Re: FAST file reading
                                    807606
                                    And what is problem with lines? There must be some way to read it char by char or by blocks or something.
                                    1 2 Previous Next