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)