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

    Here is my program. I try to append some whitespace after each word, and copy in a different file. However, it only writes last word and the whitespace in the new file. Anybody has a comment?

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


    class ReadFile {

    public static void main(String[] args) {
    /*
    System.out.println("enter the source");
    InputStreamReader source = new InputStreamReader(System.in);
    */
    Scanner s = null;
    try {
    s = new Scanner(new BufferedReader(new FileReader("d:in.txt")));
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    while (s.hasNext()) {
    StringBuffer sb = new StringBuffer(s.next());
    String a= sb.append("\t \r\n\t /*heey*/").toString();
    byte[] b= a.getBytes();
    writeFile(b);

    }
    }

    public static void writeFile (byte[] z){

    FileOutputStream out = null;


    try {
    out = new FileOutputStream("dut.txt");
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    try {
    out.write(z);

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }}
    Add to right2001's Reputation

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

    Default

    I guess, since outputstream is created again and again, FileOutPutStream should be declared before scanning. That may prevent from processing only the last word. Is that right? Does anybody have an idea about how to handle that?

    Best,

    Murat.

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    439
    Rep Power
    6

    Default

    Can you please tell me what is the objective of your program?
    Why i am asking this is,i was not able to understand your code.
    Mak
    (Living @ Virtual World)

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

    Default

    I try to create a parsing test suite by adding white spaces after each space, new line, ',', and '='. New version is below. There is still one problem. It adds the delimiter at the end of file on each iteration.

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


    class ReadFile {

    public static void main(String[] args) {
    /*
    System.out.println("enter the source");
    InputStreamReader source = new InputStreamReader(System.in);
    */
    FileOutputStream out1 = null;

    try {
    out1 = new FileOutputStream("d:out1.txt");
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Scanner s = null;
    try {
    s = new Scanner(new BufferedReader(new FileReader("d:in.txt")));
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    while (s.hasNext()) {
    StringBuffer sb1 = new StringBuffer(s.next());
    String a= sb1.append("\t \r\n\t /*heey*/\r\n").toString();
    byte[] b= a.getBytes();
    try {
    out1.write(b);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    FileOutputStream out2 = null;

    try {
    out2 = new FileOutputStream("d:out2.txt");
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Scanner s2 = null;
    try {
    s2 = new Scanner(new BufferedReader(new FileReader("d:out1.txt"))).useDelimiter(",");
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    while (s2.hasNext()) {
    StringBuffer sb2 = new StringBuffer(s2.next());

    String c= sb2.append("\t \r\n\t /*heey*/\r\n ,\r\n").toString();
    byte[] d= c.getBytes();
    try {
    out2.write(d);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    FileOutputStream out3 = null;

    try {
    out3 = new FileOutputStream("d:out3.txt");
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Scanner s3 = null;
    try {
    s3 = new Scanner(new BufferedReader(new FileReader("d:out2.txt"))).useDelimiter("=");
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    while (s3.hasNext()) {
    StringBuffer sb3 = new StringBuffer(s3.next());
    String e= sb3.append("\t \r\n\t /*heey*/\r\n =\r\n").toString();
    byte[] f= e.getBytes();
    try {
    out3.write(f);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }

    }
    }

  5. #5
    faisalcmpm is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    Yes u are right.
    Your FileOutputStream should be initialised only once and it should be before
    while (s.hasNext()) .. line

  6. #6
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    439
    Rep Power
    6

    Default

    As told by faisalcmpm,FileOutputstrem should be initialized only once.

    Hence,Use a simple way to solve the problem instead of complex one like this.
    Mak
    (Living @ Virtual World)

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

    Default

    Yeah, I solved the problem, but at this time there appear additional characters at the end of outputfile. Can anybody look at the code and find the bug? It is supposed to just scan the input file, and whenever finds a whitespace, comma or equal sign, should append a sequence of character pattern,and then write to the outputfile. Here is the code:

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


    class ReadFile {

    public static void main(String[] args) {
    /*
    System.out.println("enter the source");
    InputStreamReader source = new InputStreamReader(System.in);
    */
    FileOutputStream out1 = null;

    try {
    out1 = new FileOutputStream("d:out1.txt");
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Scanner s = null;
    try {
    s = new Scanner(new BufferedReader(new FileReader("d:in.txt")));
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    while (s.hasNext()) {
    StringBuffer sb1 = new StringBuffer(s.next());
    String a= sb1.append("\t \r\n\t /*heey*/\r\n").toString();
    byte[] b= a.getBytes();
    try {
    out1.write(b);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    FileOutputStream out2 = null;

    try {
    out2 = new FileOutputStream("d:out2.txt");
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Scanner s2 = null;
    try {
    s2 = new Scanner(new BufferedReader(new FileReader("d:out1.txt"))).useDelimiter(",");
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    while (s2.hasNext()) {
    StringBuffer sb2 = new StringBuffer(s2.next());

    String c= sb2.append("\t \r\n\t /*heey*/\r\n ,\r\n").toString();
    byte[] d= c.getBytes();
    try {
    out2.write(d);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    FileOutputStream out3 = null;

    try {
    out3 = new FileOutputStream("d:out3.txt");
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Scanner s3 = null;
    try {
    s3 = new Scanner(new BufferedReader(new FileReader("d:out2.txt"))).useDelimiter("=");
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    while (s3.hasNext()) {
    StringBuffer sb3 = new StringBuffer(s3.next());
    String e= sb3.append("\t \r\n\t /*heey*/\r\n =\r\n").toString();
    byte[] f= e.getBytes();
    try {
    out3.write(f);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }

    }
    }

    Here is the sample input/output.

    input

    dabdsad asdskdj
    sds=abc
    adsa,dsfdff

    output

    dabdsad
    /*heey*/
    asdskdj
    /*heey*/
    sds
    /*heey*/
    =
    abc
    /*heey*/
    adsa
    /*heey*/
    ,
    dsfdff
    /*heey*/

    /*heey*/
    ,

    /*heey*/
    =


    Additionally, is there a way to avoid creating multiple files in order to get final output?

    Cheers,

    Murat.

  8. #8
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    I did not feel like peddling through your code, but I do have a few suggestions to help you towards getting an answer.

    Learn about java's code conventions. Using a convention will help you and others understand your code.
    Since I can't post a link, try searching google for java code conventions

    Try to maintain the formatting when you post by using the
    [ code ][ /code ] tags without the spaces.

    Also try to use descriptive variable names.

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 JSP using IntelliJ IDEA 6.0.5
    By ddeokarb in forum IntelliJ IDEA
    Replies: 0
    Last Post: 11-28-2008, 01:26 PM
  3. 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
  4. Replies: 1
    Last Post: 03-27-2008, 07:40 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
  •