This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-11-20 18:19:12

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Java Program to Copy the contents of one file to another

Basically, I took two ideas I found and put them together to make a program that takes one file and then copies everything in it into another.

Code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class CopyFileExample {
    public static void main(String[] args){
        InputStream inStream = null;
        OutputStream outStream = null;
        Scanner s = new Scanner(System.in);
        
        try{
            File a = new File(s.nextLine());
            File b = new File(s.nextLine());
            File[] alist = a.listFiles();
            for(int i = 0; i < alist.length; i++){
                if(alist[i].isFile()){
                    inStream = new FileInputStream(a + "\\" + alist[i].getName());
                    outStream = new FileOutputStream(b + "\\" + alist[i].getName());
            
                    byte[] buffer = new byte[1024];
                    int length;
            
                    while((length = inStream.read(buffer)) > 0){
                        outStream.write(buffer, 0, length);
                    }
            
                    inStream.close();
                    outStream.close();
            
                    System.out.println((i+1) *100 / alist.length + "% complete");
                }
            }
            System.out.println("Complete.");
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#2 2012-11-20 19:54:32

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Java Program to Copy the contents of one file to another

If you translate that into javaScript, I swear I'll understand.  tongue


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#3 2012-11-20 20:00:48

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Java Program to Copy the contents of one file to another

The first bit is importing the libraries, that I can understand. The rest just goes over my head. I must brush up on my Java  tongue


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#4 2012-11-21 08:17:43

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Java Program to Copy the contents of one file to another

Code:

import java.io.File; //all the imports
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class CopyFileExample { //the class decleration
    public static void main(String[] args){ //the main method 
        InputStream inStream = null; //The file reading stream. It reads the contents of a file
        OutputStream outStream = null; //the file writing stream. it appends to a file
        Scanner s = new Scanner(System.in); //The object used to get user input
        
        try{ //This is used to catch any errors that may occur during file reading/writing
            File a = new File(s.nextLine()); //This object represents the folder whose contents will be read. The input prompted by s.nextLine() should be a path to a folder.
            File b = new File(s.nextLine());//This object represents the folder who should have the previously read contents written into it.
            File[] alist = a.listFiles(); //This array gets all the files from "a" using the call a.listFiles().
            for(int i = 0; i < alist.length; i++){  //This starts the loop that will go through the "alist" array and copy them.
                if(alist[i].isFile()){ //The alist[i].isFile() call makes sure that the file currently being copied is, infact, a file.
                    inStream = new FileInputStream(a + "\\" + alist[i].getName()); //Sets up the InputStream to start reading files. 
                    outStream = new FileOutputStream(b + "\\" + alist[i].getName()); //Sets up the OutputStream to start writting files.
            
                    byte[] buffer = new byte[1024]; //Honestly I'm a bit stupified about what this does. :/
                    int length;
            
                    while((length = inStream.read(buffer)) > 0){
                        outStream.write(buffer, 0, length);
                    }
            
                    inStream.close(); //This closes the InputStream. This call is required
                    outStream.close(); //This closes the OutputStream. For ANYTHING to be written you MUST close the stream
            
                    System.out.println((i+1) *100 / alist.length + "% complete"); //This updates the user on how many of the files have been copied.
                }//Ends the if
            }//ends the for
            System.out.println("Complete."); //Updates the user, telling them that the process is complete
        }catch(IOException e){ //If an error is caught by the try block, running gets sent here.
            e.printStackTrace(); //Outputs the error
        }//ends catch
    }//ends main
} //ends the class

There is the complete annotations. Sorry I don't know what the buffer does. XD I don't have too much experience with i/o in java.

Last edited by poopo (2012-11-21 08:35:24)


http://i45.tinypic.com/28rnqki.jpg

Offline

 

Board footer