http://www.technicalpage.net/search/label/SQL

>> Copy the contents of a file to another file.


 private static void copyFileContents(String srcPath, String destPath) throws IOException {
   File srcfl = new File(srcPath);
   File destfl = new File(destPath);

       FileInputStream f_inputStream = null;
       FileOutputStream f_outputStream = null;
       try {
 
       f_inputStream = new FileInputStream(srcfl);
        f_outputStream = new FileOutputStream(destfl);

              byte[] buffer = new byte[1024];
              int readbytes;

           while ((readbytes = f_inputStream.read(buffer)) > 0) {
              f_outputStream.write(buffer, 0, readbytes);
              }
 
       } finally {
           f_inputStream.close();
           f_outputStream.close();
          }
  }

Example:
   String inputFile = "C:\\Project1\\Test.txt";
   String outpuFile = "C:\\Project2\\Test.doc";
Test.txt file contents is copied to Test.doc . If the target file does not exists, it will create that file and copy the contents.

No comments:

Post a Comment