Doubt in java programming-2.

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.

Replies

  • Manish Goyal
    Manish Goyal
    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();
    }
    }
  • Morningdot Hablu
    Morningdot Hablu
    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.

You are reading an archived discussion.

Related Posts

hi, i wana know about the "best simulator" for PLC ladder programming... Plz help me to find this.....
can any one suggest me some projects on networking based on ccna
Gallery for funny avatars. I'm quite sure most of the CEans here can use these avatars for their own profile on CrazyEngineers.
hi CE EEEians can i knw our dept some important core companies in our country?? 😕
hello! can anyone please provide me with the PWM control code for 8051 microcontroller. thanks in advance.