Doubt in java programming-2.

Morningdot Hablu

Morningdot Hablu

@morningdot-6Xuj4M Oct 21, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Aug 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();
    }
    }
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Aug 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.