Results 1 to 2 of 2
  1. #1
    LightAngels is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Default Java, read text from file and break it into lines

    I'm writing a code that read a text from a file and then sort it into lines of max specific width.

    Example: a text that contains "aaaa bbbb cccc dddd"

    specified width is 16

    so the output should be

    aaaa bbbb cccc //width is only 14, if dddd is added, it would be longer than 16.

    dddd

    My approach: read the text and assign it to a string
    Java Code:
    Scanner input_OUT = new Scanner(new File("abc"));
    
    PrintStream output = new PrintStream("abc");
    .
    .
    
    String s = "";
    String str = "";    
     while (input_OUT.hasNextLine()) {
        s = input_OUT.nextLine();
        str = s +" "+ s; //This will keep adding onto str until hasNextLine is false
     }
    
    String s = "";
    
    while(input_OUT.hasNext()) { // get words if it still have
    
                if (s.length() + input_OUT.next().length() > width) { 
                    s = str.substring(0,s.length());
                    output.println(s);
                    str = str.substring(s.length()+1,str.length());
                    s = "";
                }
                else {
                    s += input_OUT.next();
                }
    
            }
    Although it compiles. But the file doesn't show any output. I think my code is not right. I know there is options for stringbuild, string split, array. But i'm now allowed to do that.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,945
    Rep Power
    4

    Default Re: Java, read text from file and break it into lines

    First, print out to the console to make certain your program works as expected. If it does the odds are you are not flushing the output buffer.
    You can create an instance of PrintStream with the autoflush option set to true. That may solve your problems. Or just do an output.close()
    when you are done writing to the file.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. How do i read multiple lines from a file into a string?
    By Pinky4Free in forum Advanced Java
    Replies: 9
    Last Post: 11-04-2012, 09:06 PM
  2. Replies: 4
    Last Post: 11-03-2010, 06:17 PM
  3. How can we read text file in java 1.4
    By javasaravanan in forum Advanced Java
    Replies: 4
    Last Post: 11-30-2009, 03:28 PM
  4. How to break a text file into block.
    By kinnylaw in forum New To Java
    Replies: 1
    Last Post: 11-28-2009, 08:49 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •