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.