Java IO file input/output stream copy File

Source: Internet
Author: User
Tags file copy

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.