IO Package The five most important classes and an interface
File/outputstream/inputstream ( byte stream )/writer/reader ( character streams )
an interface: Serializable
?
File class:
BYTE stream:
OutputStream with the InputStream is abstract class
OutputStream is the entire IO the package. Maximum class of byte output stream
FileOutputStream is a subclass that often uses the entire subclass to instantiate by polymorphic OutputStream
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvz29vzgh1ywp1bg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>
?
?
?
Packageiotest;
?
Importjava.io.File;
Importjava.io.FileNotFoundException;
Importjava.io.IOException;
Importjava.io.OutputStream;
Importjava.io.FileOutputStream;
Importjava.io.InputStream;
Importjava.io.FileInputStream;
?
Publicclass OutputStreamDemo1 {
public static void Main (String args[]) throws exception{
File f =newfile ("D:" +file.separator+ "JavaSrc" +file.separator+ "Write_file.txt");
OutputStream O=null;
O=new FileOutputStream (f);
String s= "Hello world!";
byte b[]=s.getbytes ();
O.write (b);
O.close ();
Change file Append file contents
OutputStream O1=null;
O1=new FileOutputStream (f,true);
O1.write ("\ r \ n". GetBytes ()); To add a line break
O1.write (b);
O1.close ();
?
Read and print files
01 defines a byte array with a length of 1024
InputStream In1=null;
In1=new FileInputStream (f);
byte b1[]=new byte[1024];
int Len=in1.read (B1);
In1.close ();????????????????
Print (B1,len);
?
02 Defining a byte array length for file length
InputStream In2=null;
In2=new FileInputStream (f);
BYTE b2[]=new byte[(int) f.length ()];
In2.read (B2);
In2.close ();
Print (B2);
?
?? ??? ?//03 has file length read -by-byte
?????? ?????? ? InputStream In3=null;
In3=new FileInputStream (f);
BYTE b3[]=new byte[(int) f.length ()];
for (int i=0;i<b3.length;i++) {
B3[i]= (Byte) in3.read ();
}
In3.close ();
Print ("The third Read Method:");
Print (B3);
?????
04 Unknown file length Read
InputStream In4=null;
In4=new FileInputStream (f);
byte b4[]=new byte[1024];
int i=0;
int temp=0;
while ((Temp=in4.read ())!=-1) {
b4[i]= (byte) temp;
i++;
}
In4.close ();
Print ("The fourth Read method:");
print (B4);
}
?
public static void print (Byte b[],int len) {
System.out.println (New String (B,0,len));
}
public static void print (Byte b[]) {
System.out.println (New String (b));
}
public static void print (String b) {
System.out.println (b);
}
?
}
IO packet stream of Java learning notes