Detecting a new line character

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blacksaibot
    New Member
    • Apr 2008
    • 1

    #1

    Detecting a new line character

    I am reading in words from a file using the following code:

    Code:
    scannedWord = scan.next(); 
    System.out.println("word: " + scannedWord);
    Then I am writing each word scanned into a file with the following code:

    Code:
    FileWriter outFile = new FileWriter(outputFile,true);
    outFile.write(scannedWord + " ");
    outFile.close();
    The problem is, that when it writes to the output file, it doesn't remember where spaces are or characer returns.. How do I detect a new line character so that the output file looks exactly like the input file? Thanks!!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Assuming that your scan variable is a Scanner, you use use the nextLine() method. This will get the whole line and, when printed out (or written), will preserve spacing.

    In order to get individual words out of this, you can use String.split().

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by blacksaibot
      I am reading in words from a file using the following code:

      Code:
      scannedWord = scan.next(); 
      System.out.println("word: " + scannedWord);
      Then I am writing each word scanned into a file with the following code:

      Code:
      FileWriter outFile = new FileWriter(outputFile,true);
      outFile.write(scannedWord + " ");
      outFile.close();
      The problem is, that when it writes to the output file, it doesn't remember where spaces are or characer returns.. How do I detect a new line character so that the output file looks exactly like the input file? Thanks!!
      Could you post an example input?!! to be saved in a file.....

      Comment

      Working...