Results 1 to 7 of 7
  1. #1
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default writing in file read from another file..

    I am facing problem to write in file..
    At first i read from file successfully.Now I want to write them on another file..It is working,,But not approprietly...
    It is working only for 1st line.Not others...

    I think problem is here..
    ((strLine=br.readLine())!=null)


    input file::
    UJJAL DHAR
    TAKI

    BUT OUTPUT FILE:
    UJJAL DHAR


    At first line there is null..what should i do for write all strings...

  2. #2
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Red face Code is here

    import java.io.*;
    class FileReadwrite
    {
    public static void main(String args[])
    {
    try{
    // Open the file that is the first
    // command line parameter

    FileInputStream fstream = new FileInputStream("textfile.txt"); // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;




    PrintWriter pwr = new PrintWriter(new FileWriter("java2s.txt"));

    //Read File Line By Line
    while ((strLine=br.readLine())!=null) {

    // Print the content on the console


    pwr.print(strLine);
    pwr.close();

    /*
    StreamTokenizer stok = new StreamTokenizer(new FileReader("java2s.txt"));
    int tok = stok.nextToken();
    while (tok != StreamTokenizer.TT_EOF) {
    System.out.println(stok.sval);
    tok = stok.nextToken();
    }*/

    }
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }
    }

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    U show the code logic.
    Ramya:cool:

  4. #4
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Red face Code is here

    import java.io.*;
    class FileReadwrite
    {
    public static void main(String args[])
    {
    try{
    // Open the file that is the first
    // command line parameter

    FileInputStream fstream = new FileInputStream("textfile.txt"); // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;




    PrintWriter pwr = new PrintWriter(new FileWriter("java2s.txt"));

    //Read File Line By Line
    while ((strLine=br.readLine())!=null) {

    // Print the content on the console


    pwr.print(strLine);
    pwr.close();

    /*
    StreamTokenizer stok = new StreamTokenizer(new FileReader("java2s.txt"));
    int tok = stok.nextToken();
    while (tok != StreamTokenizer.TT_EOF) {
    System.out.println(stok.sval);
    tok = stok.nextToken();
    }*/

    }
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }
    }

  5. #5
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    I am sorry...I have read from file 'testfile.txt'
    and want to write them on another file 'Java2s.txt'.

    in my code....

    //Read File Line By Line
    while ((strLine=br.readLine())!=null) {

    // Print the content on the console


    pwr.print(strLine);
    pwr.close();
    }


    1st line is written on new file..but not the others.
    How I could write the all contents in new file?
    help me please..

  6. #6
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    I am sorry...I have read from file 'testfile.txt'
    and want to write them on another file 'Java2s.txt'.

    in my code....

    //Read File Line By Line
    while ((strLine=br.readLine())!=null) {

    // Print the content on the console


    pwr.print(strLine);
    pwr.close();
    }


    1st line is written on new file..but not the others.
    How I could write the all contents in new file?
    help me please..

  7. #7
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  2. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  3. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  4. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  5. Replies: 9
    Last Post: 07-10-2008, 04:52 PM

Posting Permissions

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