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

Analysis of byte stream and character stream examples of IO operation in JAVA

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the JAVA operation of IO byte stream and character stream instance analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this JAVA operation of IO byte stream and character flow instance analysis article will have a harvest, let's take a look.

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 FileWriter

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 ();}}

FileReader

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) and operates directly with the file itself, while the character stream uses the buffer when operating.

When a byte stream operates on a file, the file can be output even if the resource (close method) is not closed, 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 can use the flush method to force the buffer to be flushed. Only then can the content 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).

This is the end of the article on "byte stream and character flow example analysis of IO operations in JAVA". Thank you for reading! I believe you all have some knowledge of "byte stream and character flow instance analysis of IO operation in JAVA". If you want to learn more, you are 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