Doubt in java programming.

Hello friend's,
Check out this program.
import java.io.*;
public class r
{
public static void main(String args[])throws IOException
{
try
{
FileReader fr=new FileReader("mohit.txt");
FileWriter fw=new FileWriter("monu.txt");
int s;
while((s=fr.read())!=-1)
{
fw.write(s);
}
}
catch(Exception e)
{
System.out.println("exception occured ");
}
}
}
What's wrong with this one..??

Replies

  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    You have to use fw.close(); after the while loop.
  • Morningdot Hablu
    Morningdot Hablu
    Yeh it's working.
    .
    Now i comes to the main problem what i faced.
    Check out this program ES
    import java.io.*;
    public class r
    {
    public static void main(String args[])throws IOException
    {
    try
    {
    FileInputStream fr=new FileInputStream("mohit.txt");
    FileOutputStream fw=new FileOutputStream("monu.txt");
    int s,d;
    s=fr.read();
    fw.write(s);
    }
    catch(Exception e)
    {
    System.out.println("exception occured ");
    }
    }
    }
    This program will be written using byte stream in this program i didn't close the streams and it's working but in the case of character stream we must have to close the stream's.
    Can you tell me the reason why..??
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    This is the information I found from a friend!

    In the FileReader operation the data is written onto Buffer it seems, and you can use flush() or close() for getting the file written!
  • Morningdot Hablu
    Morningdot Hablu
    Thanks ES !!
    I got the answer.
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    @Mohit:
    Read this point , we need to close the file stream as well.

    Always Close Streams ,Closing a stream when it's no longer needed is very important — so important that CopyBytes uses a finally block to guarantee that both streams will be closed even if an error occurs. This practice helps avoid serious resource leaks.

You are reading an archived discussion.

Related Posts

Hey , Post here how's your day???? And share your thoughts here....... such that you can feel relax........... write anything you want NO RESTRICTIONS WILL BE APPLIED HERE...... If angry...
HI Guys, I think the war between Amd and Intel is very long. Everyday many people meet who have different opinion about these processor manufacturer. Who use Intel they say...
Hi, guys I recently search details for i core processor made by Intel. I just got shocked when I know that core i3 and core i5 600 series processor are...
Hi,Guys. I have 3 hdd. On first I install windows 7 (main operating system), now on second hdd i install fedora 13. As i install Fedora 13 for learning and...
hello all,am currently in college studying to be an engineer,and am at the minute involved in a project on ocean energy,i am wondering if there is any entry level software...