Home > Java > Mencopy File .txt

Mencopy File .txt

September 12, 2012 Leave a comment Go to comments

Berikut ini merupakan code Java untuk melakukan copy isi file

package io_copyfile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
/**
 *
 * @author Taeyeon
 */
public class Main {

    
    public static void main(String[] args) {
       File source = new File("C:/Input.txt");
       File destination = new File("C:/Output.txt");
       
       FileInputStream fis = null;
       FileOutputStream fos = null;
       
       try{
           fis = new FileInputStream(source);
           fos = new FileOutputStream(destination);
           
           int read;
           byte[] buffer = new byte[4209];
           
           while((read=fis.read(buffer)) != -1)
           {
               fos.write(buffer, 0, read);
           }
       }
       catch(IOException ex)
       {
           ex.printStackTrace();
       }
       finally
       {
           try{
               if(fos != null)
               {
                   fos.close();
               }
               if(fis !=null)
               {
                  fis.close();  
               }
              }
           catch(IOException ex)
           {
               ex.printStackTrace();
           }
       }
    }

}

Semoga bermanfaat.

Referensi : Dari berbagai sumber.

P.S.

File Input.txt sudah ada terlebih dahulu

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: