To read a text file:
1 Private Static voidIoDemo1 () {2 Try {3FileReader FileReader =NewFileReader ("c:\\users\\yan\\desktop\\ Code specification (2). txt");4 intAA;5AA =Filereader.read ();6 while(Aa!=-1) {//convert to char type, otherwise a bunch of numbers7System.out.print ((Char) AA);8AA =Filereader.read ();9 }Ten filereader.close (); One}Catch(FileNotFoundException e) { A e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); the } -}
To copy a binary file:
1 Private Static voidIoDemo2 () {2 Try {3FileInputStream FR =NewFileInputStream ("d:/test.xlsx");//cannot use FileReader (character stream)4FileOutputStream FW =NewFileOutputStream ("D:/test1.xlsx");5 intRead =Fr.read ();6 while(Read!=-1) {7 Fw.write (read);8Read=Fr.read ();9 }TenFw.flush ();//no drink in the cup does not mean the drink has been finished, this method is to empty the drink in the straw One fw.close (); A fr.close (); -}Catch(FileNotFoundException e) { - e.printstacktrace (); the}Catch(IOException e) { - e.printstacktrace (); - } -}
To copy a binary file using a buffered stream:
1 Private Static voidIoDemo3 () {2 Try {3Bufferedinputstream bis =NewBufferedinputstream (NewFileInputStream ("D:/test.xlsx"));4Bufferedoutputstream BOS =NewBufferedoutputstream (NewFileOutputStream ("C:\\users\\yan\\desktop\\test.xlsx"));5 byte[] buffer =New byte[2048];6 intRead =bis.read (buffer);7 while(Read!=-1)8 {9Bos.write (buffer, 0, read);TenRead =bis.read (buffer); One } A Bos.flush (); - bos.close (); - bis.close (); the}Catch(FileNotFoundException e) { - e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); + } -}
Data Flow Passing Data:
1 Private Static voidIoDemo4 () {2 Try {3 4Bytearrayoutputstream BAOs =NewBytearrayoutputstream ();5DataOutputStream dos =NewDataOutputStream (BAOs);6 Longsrc = 1234454544;7 Dos.writelong (SRC);8 9 byte[] bytes =Baos.tobytearray ();TenBytearrayinputstream Bais =Newbytearrayinputstream (bytes); OneDataInputStream dis =NewDataInputStream (Bais); A Longrecieve; -recieve =Dis.readlong (); - theSystem.out.println ("Recieve:" +recieve); - -}Catch(IOException e) { - e.printstacktrace (); + } -}
PrintStream:
Reference
Objectstream:
1 Private Static voidIoDemo5 () {2ObjectOutputStream Oos =NULL;3ObjectInputStream Ois =NULL;4Student s =NewStudent ("Goku", 007, 95.5f);5Student S1 =NULL;6 7 Try {8FileOutputStream fos =NewFileOutputStream ("C:\\users\\yan\\desktop/ccc.txt");9Oos =NewObjectOutputStream (FOS);Ten Oos.writeobject (s); One AOIS =NewObjectInputStream (NewFileInputStream ("C:\\users\\yan\\desktop/ccc.txt")); -S1 =(Student) ois.readobject (); - theSystem.out.println ("Name:" +s1.getname ()); -System.out.println ("School Number:" +S1.getid ()); -System.out.println ("Score:" +S1.getscore ()); - fos.close (); +}Catch(Exception e) { - e.printstacktrace (); +}finally { A Try { at oos.close (); - ois.close (); -System.exit (-1); -}Catch(Exception e) { -System.exit (-1); - } in } - } to //The purpose of implementing serializable is to save objects in a file for transmission + classStudentImplementsSerializable { - Private Static Final LongSerialversionuid = -111977186014986048l; the PrivateString name; * Private intID; $ Private transient floatScore;//transient meaning is fleeting, and the role is to deserializePanax Notoginseng - PublicStudent (String name,intIdfloatscore) { the Super(); + This. Name =name; A This. ID =ID; the This. score =score; + } - $ PublicString GetName () { $ returnname; - } - the Public voidsetName (String name) { - This. Name =name;Wuyi } the - Public intgetId () { Wu returnID; - } About $ Public voidSetId (intID) { - This. ID =ID; - } - A Public floatGetscore () { + returnscore; the } - $ Public voidSetScore (floatscore) { the This. score =score; the } the}
Java Basics-Common IO stream usage examples