Direct printing of strings in Java string

Every time I do something like

BufferedReader br = new BufferedReader(new FileReader(f));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();

      

It will not be displayed one by one. Instead, it will lag for 2-3 seconds and then show everything at once. I have tried using sleep methods etc. How can I make it so that it takes time and goes through each one, and not just lag behind and spit it all out all the time?

+3


source to share


3 answers


Try flushing the outlet.

System.out.flush();

      



After every System.out.println

+4


source


This is probably due to the fact that your file does not have carriage return characters at the end of each line. Thus, it treats the entire file as one line.



+2


source


Try sending strings to a list of arrays. See if the arralist contains each line. Then try iterating the list of arrays onto the website chunk by block.

BufferedReader br = new BufferedReader(new FileReader(f));
ArrayList<String> list = new ArrayList<String>();

        String line;
        while ((line = br.readLine()) != null) {
            list.add(line); 
        }
        br.close();

       for(String one_line: list){
        //send lines to website: 
        //sendLine(one_line);
        }

      

0


source







All Articles