Package Com.hp.io;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
public class inputandoutputfile{
They're all pure hands. If the copy display can not be copied and do not bother to change the hand to the format is not good to see
public static void Main (String args[]) {
FileInputStream Fis=null;
FileOutputStream Fos=null;
try{
1. Create the input stream. The file that reads "f://finally wait for you. txt" disk
FIS = new FileInputStream ("f:\\ finally wait for you. txt");
2. Create an output stream object
FOS = new FileOutputStream ("f:\\ finally waits for you". txt ", true);
3. Create a transit array to hold each read content
byte [] words =new byte[255];//byte inside bytes, it is better to guarantee than you want to copy the source file byte more, to not be prompted array subscript out of bounds
4. Defines an int of type N, which represents the information to read the words size once
int n;
5. Through the loop implementation of the file read, a byte of a byte of reading, if read to 1 when the end of the file is no longer read
while ((N=fis.read (words))!=-1) {//6. Read the information in the file input stream until-1 ends
Fos.write (Words,0,n); 7. The file output stream is written to read the content length in the words array is 0-n
}
System.out.print ("File copy succeeded, view file");
}catch (Filenotfoundexcepton e) {//The first input file may not be found here to write an exception
E.prinntstacktrace (); Exceptions from small to uppercase
}catch (IOException e) {
E.printstacktrace ();
}finally{//8. Turn off Data flow
try{//Closed by first turn off
if (fos!=null)
Fos.close ();
if (fis!=null)
Fis.close ();
}catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Java IO file input/output stream copy File