Java file stream Copy file (read one byte array one at a time)

Source: Internet
Author: User

Package Fileoutputstream;import Java.io.fileinputstream;import Java.io.fileoutputstream;public class CopyFileDemo { public static void Main (string[] args) throws Exception {//Encapsulated data source FileInputStream FIS = new FileInputStream ("Fos.txt");//fo S.txt file must exist fileoutputstream fos = new FileOutputStream ("Write.txt");//write.txt file can not exist first//copy data () byte[] bys = new byte[ 1024];int Len=0;while ((Len=fis.read (bys))!=-1) {fos.write (Bys,0,len);//write one byte array at a time}//release resources fos.close (); Fis.close ();}}


Read one byte at a time (character), or go to an array of bytes at a time (character array)

Package Filereader;import Java.io.filereader;public class FileReader11 {public static void main (string[] args) throws Exc eption {////mode one: reads one byte at a time//filereader reader = new FileReader ("Fos.txt");////int by = 0;//while ((By=reader.read ())!=-1) {////system.out.println (by),//system.out.print ((char) by);//}//reader.close ();//Mode two: One byte array at a time FileReader reader2 = new FileReader ("Fos.txt"); char[] ch = new char[5];//byte[] bys = new Byte[1024];int Len=0;while ((Len=reader2.read (CH))! =-1) {System.out.println (new String (Ch,0,len));} Reader2.close ();}}


The final version displays the read file in the console:

Package Fileoutputstream;import Java.io.fileinputstream;public class FileOutputStream4 {public static void main (String [] args) throws Exception {FileInputStream fis = new FileInputStream ("Fos.txt");//Final code//array length is generally 1024 or 1024 of the integer times byte[] bys = new Byte[1024];int Len=0;while ((Len=fis.read (bys))!=-1) {System.out.println (new String (Bys,0,len));//new String is to convert the word count to a string}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java file stream Copy file (read one byte array one at a time)

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.