Pages: 1
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.
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(); } } }
Offline
If you translate that into javaScript, I swear I'll understand.
Offline
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
Offline
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)
Offline
Pages: 1