Java I/O article 2 (buffer stream random stream array Stream Data Stream), java Article 2

Source: Internet
Author: User

Java I/O article 2 (buffer stream random stream array Stream Data Stream), java Article 2

1: Buffer stream BufferedReader BufferedWriter

Strong read/write capability, able to read and write a single row of data separately, can reduce the number of visits to the hard disk.

1/** 2 * The buffer stream enhances the readability of the price data, reduces the number of visits to the hard disk, and improves the efficiency. 3 **/4 public static void bufferedDemo () throws IOException {5 File fread = new File ("E: \ input.txt"); 6 File fwrite = new File ("E: \ output.txt"); 7 System. out. println ("file:" + "E: \ output.txt" + "readable" + fwrite. canWrite (); 8 BufferedReader bis = null; 9 BufferedWriter bos = null; 10 FileReader FCM = new FileReader (fread); 11 FileWriter fos = new FileWriter (fwrite); 12 bis = New BufferedReader (FCM); 13 bos = new BufferedWriter (fos); 14 // read file data, print display 15 int lineLen = 0; 16 String arrayStr [] = new String [20]; // create an array and store the read content 17 String temp; 18 while (temp = bis. readLine ())! = Null) {19 System. out. println (temp); 20 arrayStr [lineLen] = temp; 21 lineLen ++; 22} 23 System. out. println ("File Read completed"); 24 // output data 25 String s; 26 while (s = bis. readLine ())! = Null) {27 bos. write (s); 28 bos. newLine (); // insert linefeed 29} 30 System. out. println ("data output is complete. "); 31 bos. flush (); 32 bis. close (); 33 bos. close (); 34}

 

2: Random stream RandomAccessFile

A RandomAccessFile stream that can read data in a file or write data into a file.

1/** 2 * random stream readable and writable type RandomAccessFile 3 * @ throws IOException 4 **/5 public static void RandomAccessDemo () throws IOException {6 // use Random to write some data separately. 7 int data [] = {1, 2, 4, 5, 6, 7, 8, 9} is read }; 8 File fileName = new File ("E: \ RandomAccessFile.txt"); 9 if (! FileName. exists () 10 fileName. createNewFile (); 11 RandomAccessFile raf = new RandomAccessFile (fileName, "rw"); 12 for (int I = 0; I <data. length; I ++) {13 raf. writeInt (I); 14} 15 System. out. println ("Data Writing is complete. "); 16 // read data 17 for (int j = data. length-1; j> = 0; j --) {18 raf. seek (j * 4); 19 System. out. printf ("\ t % d", raf. readInt (); 20} 21 raf. close (); 22}

 

3: array stream byte array stream: ByteArrayInputStream ByteArrayOutputStream

Character array stream: BytearrayReader ByteArrayWriter

1/** 2 * array stream byte array stream (ByteArrayInputStream ByteArrayOutputStream) 3 * character array stream (BytearrayReader ByteArrayWriter) 4 **/5 public static void ByteArrayDemo () throws Exception {6 // byte stream 7 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 8 byte [] str = "Wish you a happy holiday ". getBytes (); 9 bos. write (str); // write data 10 // read data 11 ByteArrayInputStream bis = new ByteArrayInputStream (bos. toByteArray (); 12 byte [] B = new byte [bos. to ByteArray (). length]; 13 bis. read (B); 14 System. out. println ("written data:" + new String (B); 15 bis. close (); 16 bos. close (); 17 // bytes stream 18 CharArrayWriter caw = new CharArrayWriter (); 19 char [] str2 = "happy Mid-Autumn Festival! ". ToCharArray (); 20 caw. write (str2); 21 CharArrayReader car = new CharArrayReader (caw. toCharArray (); 22 char [] c = new char [caw. toCharArray (). length]; 23 car. read (c); 24 System. out. println ("read data:" + new String (c); 25 car. close (); 26 caw. close (); 27}

4: Data Stream DataInputStream DataOutputStream

It can easily read machine-independent java raw data without the need to tangle the size of bytes during reading.

/*** Data stream DataInputStream DataOutputStream can easily read original machine-independent java data **/public static void DataDemo () throws IOException {// create a file, use data streams to write different types of data and read different types of data File f = new File ("E: \ DataDemo.txt"); FileOutputStream fos = new FileOutputStream (f ); dataOutputStream dos = new DataOutputStream (fos); dos. writeInt (23); dos. writeDouble (1, 22.343435); dos. writeFloat (55.2343f); dos. writeBoolean (true); dos. writeChars ("happy Mid-Autumn Festival ! "); System. out. println (" the data has been read! "); // Read data FileInputStream FD = new FileInputStream (f); DataInputStream dis = new DataInputStream (FS); System. out. println (dis. readInt (); System. out. println (dis. readDouble (); System. out. println (dis. readFloat (); System. out. println (dis. readFloat (); System. out. println (dis. readBoolean (); char c = '\ 0'; while (c = dis. readChar ())! = '\ 0') {System. out. print (c);} dis. close (); dos. close ();}

 

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.