Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sort out the IO streams in JAVA (character streams and byte streams)

2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Io streams in java fall into two categories, characters and bytes:

The parent class of OutputStream and InputStream byte streams, abstract. OutputStream has two interfaces that provide implementation, closable and flushable. The parent class of Writer and Reader character streams, abstract.

In fact, in the operation of the stream, the underlying file is read and written by the byte stream, because even if it is the object of the character stream, it finally uses the byte stream to read and write. Manipulate the byte subclasses FileOutputStream and FileInputStream of the file.

Remember, what is written out and read in here are bytes. Class useByteStream {/ * use file output byte stream * * / public static void testFileOutputStream () {OutputStream out = null; try {File f = new File (".\\ log\\ test.txt"); / / out = new FileOutputStream (f) Out = new FileOutputStream (fjre true); / / record to the file String str = "Hello WorldSometries!"; byte b [] = str.getBytes (); out.write (b); out.close () } catch (FileNotFoundException e) {} catch (IOException e) {}} / * use file to input byte stream * / public static void testFileInputStream () {InputStream out = null Try {File f = new File (".\\ log\\ test.txt"); out = new FileInputStream (f); String str = "Hello Worldworkers employees!"; byte b [] = new byte [1000]; int len = out.read (b) System.out.println (new String (bline 0, len)); out.close ();} catch (FileNotFoundException e) {} catch (IOException e) {} The character subclasses FileWriter and FileReaderclass useCharStream {/ * use the file character output stream * / public static void testFileWriter () {Writer w = null; try {File f = new File (".\\ log\\ test2.txt") W = new FileWriter (fjue true); / / append method w.write ("hello world\ r\ n"); w.close ();} catch (FileNotFoundException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();}} / * input stream using file characters * / public static void testFileReader () {Reader w = null Try {File f = new File (".\\ log\\ test2.txt"); w = new FileReader (f); char c [] = new char [1024]; w.read (c); System.out.println (c) W.close ();} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} Two conversion classes, OutputStreamWriter, are responsible for converting the write character stream to a byte stream, and InputStreamReader, which is responsible for reading the byte stream to the character stream.

The immediate parent of FileWriter is OutputStreamWriter, not Writer.

The immediate parent of FileReader is InputStreamReader, not Reader.

Therefore, what is eventually written to and read from the file is a byte stream.

The above is based on file stream operations, and then on memory operation flows, which should be rarely used if you just write business code.

Memory operation stream

ByteArrayInputStream\ ByteArrayOutputStream. Class useMemoryStream {/ * use memory to manipulate streams, byte * / public static void testByteArray () {String str = "Hello world"; ByteArrayInputStream bis = null; ByteArrayOutputStream bos = null; bis = new ByteArrayInputStream (str.getBytes ()); bos = new ByteArrayOutputStream (); int temp = 0 While ((temp=bis.read ())! =-1) {char c = (char) temp; bos.write (Character.toUpperCase (c));} String newStr = bos.toString (); try {bis.close () Bos.close ();} catch (IOException e) {e.printStackTrace ();} System.out.println (newStr);}}; in addition, the pipeline flow enables communication between two threads.

PipedInputStream and PipedOutputStream.

PipedOutputStream establishes a connection with PipedInputStream through the connect method, and then the PipedInputStream.read method reads class Send implements Runnable {private PipedOutputStream pos = null; public Send () {this.pos = new PipedOutputStream ();} public void run () {String str = "Hello worldviews!" Try {try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace () } this.pos.write (str.getBytes ()); System.out.println ("thread:" + Thread.currentThread (). GetId () + ", Send string:" + str);} catch (IOException e) {e.printStackTrace () } try {this.pos.close ();} catch (IOException e) {e.printStackTrace ();}} public PipedOutputStream getPos () {return this.pos;}} Class Receive implements Runnable {private PipedInputStream pis = null; public Receive () {this.pis = new PipedInputStream ();} public void run () {byte b [] = new byte [1024]; int len = 0; try {len = this.pis.read (b) / / blocking mode} catch (IOException e) {e.printStackTrace ();} try {pis.close () } catch (IOException e) {e.printStackTrace ();} System.out.println ("thread:" + Thread.currentThread (). GetId () + ", receive:" + new String);} public PipedInputStream getPis () {return this.pis;}} Class pipedTest {public void pipedStream () {Send s = new Send (); Receive r = new Receive (); try {s.getPos () .connect (r.getPis ());} catch (IOException e) {e.printStackTrace () } new Thread (r) .start (); new Thread (s) .start ();}

All of the above are cacheless. To improve performance in general scenarios, it is best to use cached character streams: BufferedReader and BufferedWriter.

BufferedReader

Input can only be accepted as a character stream, not as a byte stream. So InputStreamReader is sometimes used to convert byte streams to character streams. And BufferedWriter.

Class useBuffer {public static void testBufferReader () {BufferedReader buf = null; / / the InputStreamReader class of the byte flow character stream is used here, because BufferedReader can only receive the character stream buf = new BufferedReader (new InputStreamReader (System.in)); String str = null; try {str = buf.readLine () } catch (IOException e) {e.printStackTrace ();} System.out.println ("output is:" + str);} public static void testBufferWriter () {File f = new File (".\\ log\\ test2.txt") Try {/ / default buffer size 8K is available via new BufferedWriter (new FileWriter (f), 1024); specified size is 1K BufferedWriter out = new BufferedWriter (new FileWriter (f)); out.write ("123321123355555", 0,10); out.write ("\ r\ n"); out.close () } catch (IOException e) {e.printStackTrace ();}

SCanner class, enter the data class.

The method is similar to the BufferedReader class, and it is easy to validate the data type.

Class useScan {public static void testScan () {Scanner scan = new Scanner (System.in); / / enter as the ending symbol, otherwise the default is the space scan.useDelimiter ("\ r\ n"); if (scan.hasNextInt () = = true) {int str = scan.nextInt (); System.out.println ("int" + str) } else {String str = scan.next (); System.out.println ("string" + str);}

Scan.hasNext supports regular expressions. For example, hasNext ("^\ d {4} -\ d {2} -\ d {2} $") is the regular expression of the date format yyyy-MM-dd, via next ("^\ d {4} -\ d {2} -\ d {2} $").

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report