Common Java class 4: Typical use of I/O streams

Source: Internet
Author: User
Tags object serialization
Package HTTP; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception;/** the read and write operations correspond to inputstream and outputstream * respectively, the latter is used for byte stream * filereader and filewrite, respectively. They correspond to fileinputstream and fileoutputstream * bufferedreader and bufferedwrite respectively, respectively, to bufferedinputstream and * bufferedoutputstream * This example implements character read/write operations Feredreaderdemo {public static string read (string filename) throws ioexception {stringbuilder STR = new stringbuilder (); bufferedreader in = new bufferedreader (New filereader (filename); string S; while (S = in. readline ())! = NULL) Str. append (S + '\ n'); In. close (); Return Str. tostring ();} public static void write (string filename, Boolean append) throws ioexception {bufferedwriter out = new bufferedwriter (New filewriter (filename, append); out. write ("I Am a Dahai! Java hello! "); Out. close ();} public static void main (string [] ARGs) {try {write ("file/test3.txt", false); system. out. println (read ("file/test3.txt");} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

 

Package HTTP; import Java. io. bytearrayinputstream; import Java. io. datainputstream; import Java. io. ioexception;/** datainputstream is used to read formatted data */public class datainputstreamandbytearrayinputstreamdemo {public static void main (string [] ARGs) throws ioexception {datainputstream in = new datainputstream (New bytearrayinputstream (filereaderandbufferedreaderdemo. read ("file/test3.txt "). getbytes (); While (in. A Vailable ()! = 0) system. Out. Print (char) in. readbyte ());}}

 

Package test; import HTTP. filereaderandbufferedreaderdemo; import Java. io. ioexception; import Java. io. stringreader;/** stringreader operates on the string */public class stringreaderdemo {public static void main (string [] ARGs) throws ioexception {stringreader in = new stringreader (filereaderandbufferedreaderdemo. read ("file/test3.txt"); int C; while (C = in. read ())! =-1) system. Out. Print (char) c );}}

 

Package test; import HTTP. filereaderandbufferedreaderdemo; import Java. io. ioexception; import Java. io. printwriter;/** corresponds to printstream * used to format the output to a file */public class printwriterdemo {public static void main (string [] ARGs) throws ioexception {// simplified creation method printwriter out = new printwriter ("file/test4.txt"); // You can also create it like this: out = new printer (New bufferedwriter (new // filewriter ("file/test4.txt"); // format the output to the text out. print Ln ('A'); Out. println (3); Out. println (3.5); Out. Print ("I love you! I Love You "); Out. Close (); // read the system. Out. println (filereaderandbufferedreaderdemo. Read (" file/test4.txt "));}}

 

Package test; import Java. io. filenotfoundexception; import Java. io. ioexception; import Java. io. randomaccessfile;/** randomaccessfile directly inherits objects and can carry out random input and output. It is similar to file operations in C language. * specify the method to open the file. When using this class, you must know the file layout, this class has a variety of methods to read and write the basic type and UTF-8 string *, can be located to a certain position of the file to read and write */public class randomaccessfiledemo {public static void main (string [] ARGs) throws filenotfoundexception {randomaccessfile out = new randomaccessfile ("file/test5 ", "RW"); try {out. writeint (1); Out. writedouble (3.3); Out. writechar ('A'); Out. writeutf ("Hello, Java! "); Out. close ();} catch (ioexception e) {e. printstacktrace ();} randomaccessfile in = new randomaccessfile ("file/test5", "R"); try {In. seek (4); system. out. println (in. readdouble (); In. seek (4 + 8 + 2); system. out. println (in. readutf (); In. close ();} catch (ioexception e) {e. printstacktrace ();}}}

 

Package test; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. io. serializable;/** objectinputstream (objectoutputstream) used for Object serialization * directly reads and writes an object. The object must be serializable */public class objectinputstreamdemo {public static void writeobject (string filena Me, object o, Boolean isappend) throws filenotfoundexception, ioexception {objectoutputstream out = new objectoutputstream (New fileoutputstream (filename, true); out. writeobject (o); out. close ();} public static object readobject (string filename) throws filenotfoundexception, ioexception, classnotfoundexception {objectinputstream in = new objectinputstream (New fileinputstream (filename); object o = in. rea Dobject (); In. close (); return O;} public static void main (string [] ARGs) {try {objectinputstreamdemo. writeobject ("file/object", new person (), false); (person) objectinputstreamdemo. readobject ("file/object ")). display ();} catch (ioexception e) {e. printstacktrace ();} catch (exception e) {e. printstacktrace () ;}} class person implements serializable {private string name = "liuhaifang"; private int sex = 0; Person (string name, int sex) {This. name = Name; this. sex = sex;} person () {} void display () {system. out. println ("My name is:" + name); string S = (sex = 0 )? "Male": "female"; system. Out. println (s );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.