Results 1 to 8 of 8
  1. #1
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default Modifying text file

    Hi all,

    I try to modify a text file appending a new line before and after each comma, and then save to a different file. However, there always happens an additional newline+comma at the end of the output file.

    Input:
    dabdsad,asdskdj
    sds abc
    adsa,dsfdff

    Output:
    dabdsad
    , :):):)
    asdskdj
    sds abc
    adsa
    , :):):)
    dsfdff
    , :eek::eek::eek:


    I appreciate if somebody finds the bug. Here is the program.


    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.util.Scanner;


    class Modifier2 {

    public static void main(String[] args) {

    FileOutputStream out = null;
    out = new FileOutputStream("out.txt");

    Scanner scan = new Scanner(new BufferedReader(new FileReader("in.txt"))).useDelimiter(",");

    while (scan.hasNext()) {
    StringBuffer buffer = new StringBuffer(scan.next());
    String modified= buffer.append("\r\n,\r\n").toString();
    byte[] modifiedBytes= modified.getBytes();

    out.write(modifiedBytes);

    }
    }
    }


    Thanks.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    you append a newline+comma+newline to each line. that's why it adds it to the end of the last line as well. if you don't add this after the last line, then your problem is solved. so if you can figure out how to not append when you have read the last line, you are good.

  3. #3
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default

    To you, what causes that problem, because I scan only comma. How the end of last line behave as comma?

  4. #4
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default

    and, how do I detect end of file while scanning?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    just how many fora have you posted this same question in??

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Quote Originally Posted by right2001 View Post
    and, how do I detect end of file while scanning?
    i assumed you already knew how, since you do it in another place in your code.

  7. #7
    Synapes's Avatar
    Synapes is offline Member
    Join Date
    Apr 2009
    Location
    United States
    Posts
    1
    Rep Power
    0

    Default new around here, want to say hello.

    Hi everyone.


    Hope everyone is doing well today

  8. #8
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    Why dont you simply try a different kind of loop? A do while loop?
    Tshegofatso Manakana
    a.k.a Untouchable

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. Modifying text file
    By right2001 in forum New To Java
    Replies: 7
    Last Post: 04-08-2009, 01:18 AM
  3. Modifying JSP using IntelliJ IDEA 6.0.5
    By ddeokarb in forum IntelliJ IDEA
    Replies: 0
    Last Post: 11-28-2008, 01:26 PM
  4. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  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
  •