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

Application Analysis of IO instance in java

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of IO instance application analysis in java, the content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. I believe everyone will gain something after reading this article on IO instance application analysis in java. Let's take a look.

I. the concept of IO

I Input/ O is the abbreviation for input and output Output, which means that the computer schedules to write and write the data in each storage (including memory and external storage).

Java uses "stream" to abstractly represent such a write-out function, encapsulated into a "class", all in the java.io package.

Various "stream" classes and interfaces are provided under the java.io package to obtain different kinds of data and to input or output data through standard methods.

1. What is input?

A program that reads data from memory is called input Input.

two。 What output (Output)

The program writes data into memory, which is called output Output.

II. Classification of streams

According to the operating data units, it is divided into: byte stream (8 bit), character stream (16 bit)

According to the flow direction of the data stream, it can be divided into input stream and output stream.

According to the role of the flow is divided into: node flow, processing flow

IO flow system

1. InputStream (byte stream)

Example:

Public static void main (String [] args) {iprt ();} public static void ipst () {InputStream inputStream = null; try {inputStream = new FileInputStream ("C:\\ 1.txt"); int i; while ((I = inputStream.read ())! =-1) {System.out.print ((char) I) } catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {try {if (inputStream! = null) {inputStream.close () } catch (IOException e) {e.printStackTrace ();}

Description: use InputStream to read file data into memory.

2. OutputStream (byte stream)

Example:

Public class ImageCopy {public static void main (String [] args) {try (InputStream inputStream = new FileInputStream ("D:\\ KDA.jpg"); OutputStream outputStream = new FileOutputStream ("E:\\ aaa\\ KDA.jpg") {byte [] bytes = new byte [1024]; int I While ((I = inputStream.read (bytes))! =-1) {outputStream.write (bytes,0,i);}} catch (IOException e) {e.printStackTrace ();}

Description: use the combination of input stream and output stream to achieve the function of picture replication.

3. Reader (character stream)

Example:

Public static void iprt () {Reader reader = null; try {reader = new FileReader ("C:\\ 1.txt"); int I; while ((I = reader.read ())! =-1) {System.out.print ((char) I);} catch (FileNotFoundException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();} finally {try {if (reader! = null) {reader.close ();}} catch (IOException e) {e.printStackTrace () }}}

Description: use Reader (character stream) to read data from a file.

4. Writer (character stream) public static void iprt () {Reader reader = null; Writer writer = null; try {reader = new FileReader ("C:\\ Users\\ 52425\\ Desktop\\ 1.txt"); writer = new FileWriter ("C:\\ 2.txt"); int I While ((I = reader.read ())! =-1) {writer.write (I);}} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {try {writer.close () Reader.close ();} catch (IOException e) {e.printStackTrace ();}

Description: use character stream to achieve file copy function.

This is the end of the article on "Application Analysis of IO examples in java". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Application Analysis of IO examples 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

Internet Technology

Wechat

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

12
Report