CrazyEngineers
  • Doubt in java programming-2.

    Updated: Oct 21, 2024
    Views: 972
    hello friends,
    I write a program to copy a text file in java.
    But i didn't get the text in serial way.
    Check out this
    import java.io.*;
    public class r
    {
    public static void main(String args[])throws IOException
    {
    FileInputStream f=new FileInputStream("monu.txt");
    InputStreamReader isr=new InputStreamReader(f);
    BufferedReader b=new BufferedReader(isr);
    char s;
    int t;
    FileOutputStream ff=new FileOutputStream("kumar.txt");
    OutputStreamWriter osr=new OutputStreamWriter(ff);
    BufferedWriter br=new BufferedWriter(osr);
    do
    {
    s=(char) b.read();
    if((t=b.read())!=1)
    br.write(s);
    }
    while((t=b.read())!=-1);
    br.close();
    }
    }
    any help will be appreciated.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberAug 28, 2010

    Check this program .i have tried this by using both do while and while loop

    using while loop output is perfect but using do while there is one extra character added

    since in do while which is exit controlled loop first statement is executed then condition is checked therefore an extra character is added but in case of while loop which is entry controlled loop the ouptut is correct
    import java.io.*;
    public class T
    {
    public static void main(String args[])throws IOException
    {
    FileInputStream f=new FileInputStream("monu.txt");
    InputStreamReader isr=new InputStreamReader(f);
    BufferedReader b=new BufferedReader(isr);
    int t;
    FileOutputStream ff=new FileOutputStream("kumar.txt");
    OutputStreamWriter osr=new OutputStreamWriter(ff);
    BufferedWriter br=new BufferedWriter(osr);
    /*while((t=b.read())!=-1)
    {
    br.write((char)t);
    }*/
    
    do
    {
    t=b.read();
    br.write((char)t);
    }
    while(t!=-1);
    br.close();
    b.close();
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberAug 28, 2010

    Thanks goyal.
    But it work fine in window but not in ubuntu.
    It will show a character encoding error when i want to open it with gpedit.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register