Copy files-read and write files with FileOutputStream and FileInputStream

Source: Internet
Author: User

Write a file
File File=new files ("Demo1.txt");//abstract a 1.txt document, processing file name or path
Constructs an instance object of two FileOutputStream, first defines the procedure comparison specification of a file class files, the general
FileOutputStream fos1=new FileOutputStream (file,false);//true indicates that if a file already exists, the program is appended to the file one time without executing it. Otherwise, each execution will overwrite the result of the last execution of the file.
Bufferedoutputstream bos=new Bufferedoutputstream (FOS1);//package fos1 as a file output stream with buffering function
FileOutputStream fos2=new FileOutputStream ("Demo2.txt");//Construct a FileOutputStream object with a string, string is the file name, This construction method can also have two parameters, Ibid.
DataOutputStream dos=new DataOutputStream (FOS2);//dataoutputstream provides the ability to write to arbitrary objects
String s1= "How is it?";
String s2= "fine.thanks!";
Dos.writebytes (s1+ "" +s2);
Fos1.write (S1.getbytes ());//write the string S1 as a byte stream in a 1.txt file
Fos2.write (S2.getbytes ());//Writes the string S2 in byte form to the 2.txt file
Fos1.write (S2.getbytes ());



Read the contents of the file
FileInputStream fis=new fileinputstream (file);
Byte[] b=new byte[fis.available ()];//creates a byte array with an array length equal to the number of bytes obtained in file
while (Fis.read (b)!=-1) {
System.out.println (New String (b));//print out what is read from file
}

 Public Static voidMain (string[] args) {File file=NewFile ("E:\\12332.txt"); Try{FileOutputStream fos=Newfileoutputstream (file); Bufferedoutputstream Bos=NewBufferedoutputstream (FOS); DataOutputStream dos=NewDataOutputStream (BOS); FileInputStream FIS=NewFileInputStream (NewFile ("E:\\12223.txt")); byte[] B =New byte[1024]; intc = 0;  while((C=fis.read (b))!=-1) {Dos.write (b,0, c);;            } fis.close ();            Dos.close ();            Bos.close ();            Fos.close (); System.out.println ("Run to this"); } Catch(Exception e) {e.printstacktrace (); }                                            }    

FileInputStream reading a file with a byte stream

FileOutputStream reading a file with a byte stream

Copy files-read and write files with FileOutputStream and FileInputStream

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.