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

Example analysis of IO operation byte stream and character stream in Java

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you the "IO operation in Java byte stream and character stream example analysis", the content is easy to understand, clear, hope to help you solve the doubt, the following let the editor lead you to study and learn "Java operation byte stream and character flow example analysis" this article.

IO operation

Byte stream

The java.io.InputStream input stream is mainly used to read the contents of the file.

The java.io.OutputStream output stream is mainly used to write content bytes to the file.

FileInputStream

This stream is used to read data from a file, and its objects can be created with the keyword new.

There are several construction methods that can be used to create objects.

You can use a file name of a string type to create an input stream object to read the file

InputStream f = new FileInputStream ("D:/hello")

You can also use a file object to create an input stream object to read the file. We first have to use the File () method to create a file object:

File f = new File ("D:/hello"); InputStream in = new FileInputStream (f); FileOutputStream

This class is used to create a file and write data to the file.

If the stream does not exist before opening the file for output, the stream creates the file.

There are two constructors that can be used to create FileOutputStream objects.

Use a file name of type string to create an output stream object:

OutputStream f = new FileOutputStream ("D:/hello")

You can also use a file object to create an output stream to write to the file. We first have to use the File () method to create a file object:

File f = new File ("D:/hello"); OutputStream fOut = new FileOutputStream (f); byte stream read and write case

Read the contents of the D:/hello.txt file and input it into the file D:/test.txt

Public class Mk {public static void main (String [] args) throws IOException {File file=new File ("D://hello.txt"); File file1= new File ("D://test.txt"); InputStream is=new FileInputStream (file); OutputStream out=new FileOutputStream (file1); / / define the byte array to temporarily store data byte [] buf = new byte [1024] / / read the contents of Is and save them to buf is.read (buf); / / write the contents of buf to out out.write (buf); is.close (); out.close ();}} character stream FileReader

Write to file instance

Public class A {public static void main (String [] args) throws IOException {FileWriter fw = new FileWriter ("user.txt", true); fw.write ("Hello China 1"); fw.write ("Hello China 2"); fw.write ("Hello China 3"); fw.close ();}}

FileWriter

Read file instance

I changed the content here.

Br.ready () determines whether the line flow is empty

Br.readLine () outputs one line, and then automatically points the cursor to the next line.

Public class A {public static void main (String [] args) throws IOException {BufferedReader br = new BufferedReader (new FileReader ("user.txt")); while (br.ready ()) {System.out.println (br.readLine ());} br.close ();}

The difference between byte stream and character stream

The use of byte stream and character stream is very similar, so what is the difference apart from the operation code?

The byte stream itself does not use the buffer (memory) when operating, and operates directly with the file itself, while the character stream uses the byte stream to the buffer when manipulating the file, even if the resource (close method) is not closed, the file can be output, but if the character stream does not use the close method, it will not output anything, indicating that the character stream uses a buffer. And you can use the flush method to force the buffer to be flushed, so that the content can be output without close

So is it better to use byte stream or character stream in development?

Files are saved or transferred in bytes on all hard drives, including pictures are also done in bytes, and characters are formed only in memory, so bytes are used the most. If you want the java program to implement a copy function, you should choose the byte stream to operate (perhaps copy the picture), and use the method of reading and writing at the same time (saving memory).

The above is all the contents of the article "example Analysis of IO Operation Byte flow and character flow in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report