Results 1 to 10 of 10
  1. #1
    priyankatxs is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Reading and Writing the contents of a file to another file

    Hi,

    My requirement is i have a file which has many records. I have to filter the records that has a text "AGR" and write only those records to a different file.

    Can anyone please help me out URGENT?

    Thanks,
    Priyanka.
    Last edited by priyankatxs; 10-19-2009 at 10:16 PM.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Show some code. What have you tried already and where are you stuck? Nobody here is going to write your code for you.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    We don't need to know that your program is urgent to you,
    Your post is not more important than others.
    What have you done so far?

  4. #4
    priyankatxs is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    I have written a code to read and write the contents to another output file. But i am not sure how to search a particular text in a record and write that record alone.

    import java.io.*;

    public class rFile {
    public static void main(String[] args) throws IOException {
    File inputFile = new File("/Users/t/t.txt");
    File outputFile = new File("/Users/t/t2.txt");

    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);
    int c;

    while ((c = in.read()) != -1)
    out.write(c);

    in.close();
    out.close();
    }
    }

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) What do you mean by record? Do you mean line?
    2.)Wrap that FileReader with a BufferedReader and use the readLine method to read line by line.
    3.) Use a PrintWriter to write the new file.
    4.) flush after you are done writting and close in finally blocks.

  6. #6
    priyankatxs is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Yes record is a line. I can read line by line. But in a line i have a text say "ABC" if i have a line with that text then i have to write that lines alone to the new file.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read the API specs for the String.contains method.
    Implement all the suggestions I suggested above.

  8. #8
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    r035198x rox
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Dieter View Post
    r035198x rox
    Trying to make fun of my user name are you?
    I'll alert the plebeians and get the cars out of the way.
    Been a while since we had a defenestration so there should be a good audience.

  10. #10
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    haha :p i can't wait!
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

Similar Threads

  1. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  2. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 11:44 AM
  3. Replies: 6
    Last Post: 12-12-2008, 04:35 PM
  4. Replies: 9
    Last Post: 07-10-2008, 05:52 PM
  5. Reading file contents (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 10:00 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
  •