Copy file of java file stream (read one byte array at a time), number of bytes of copy

Source: Internet
Author: User

Copy file of java file stream (read one byte array at a time), number of bytes of copy

Package fileoutputstream; import java. io. fileInputStream; import java. io. fileOutputStream; public class CopyFileDemo {public static void main (String [] args) throws Exception {// encapsulate the data source FileInputStream FD = new FileInputStream ("fos.txt "); // FileOutputStream fos = new FileOutputStream ("write.txt") must exist in the fos.txt file; // The write.txt file can not exist first // copy data () byte [] bys = new byte [1024]; int len = 0; while (len = Fi. read (bys ))! =-1) {fos. write (bys, 0, len); // write a byte array at a time} // release the resource fos. close (); FCM. close ();}}


Read one byte (character) at a time, or remove one byte array (character array) at a time)

Package filereader; import java. io. fileReader; public class FileReader11 {public static void main (String [] args) throws Exception {/// Method 1: read 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 (); // Method 2: Read a byte array 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 files on the console:

Package fileoutputstream; import java. io. fileInputStream; public class FileOutputStream4 {public static void main (String [] args) throws Exception {FileInputStream FD = new FileInputStream ("fos.txt "); // The latest generation of bytes // The length of the array is generally 1024 or an integer multiple of 1024 byte [] bys = new byte [1024]; int len = 0; while (len = FCM. read (bys ))! =-1) {System. out. println (new String (bys, 0, len); // new String converts the number of characters to a String }}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.