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