can not write streams to file ?

howdy~

i did some exercises on file I/O, but weird that i can only read but not write. here is my code below:


import java.io.*;


class TestFile_001
{
      public static void main(String[] args) throws Exception
      {
            copy();
      }

      public static void copy() throws IOException
      {
            File fIn = new File("TestFileStreamRead.txt");
            File fOut = new File("TestFileStreamWrite.txt");

            FileReader fr = new FileReader(fIn);
            FileWriter fw = new FileWriter(fOut);

            int n;
            while(true)
            {
                  n = fr.read();
                  if (n != -1)
                  {
                        fw.write(n);
                  }
                  else
                  {
                        break;
                  }
            }
      }
}

TestFileStreamWrite.txt is always empty after running the code, which is supposed to have same content as TestFileStreamRead.txt, i’m sticked here. any solution pls ?

Try fOut.close() and/or fOut.flush(). Also you might want to dump to the screen whats actually being copied across just to double check whether something is actually being copied.

Kev

PS. Could someone move this to a more appropriate forum.