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

What are the knowledge points of Java streams and files

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

Share

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

This article mainly explains "what are the knowledge points of Java streams and files". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's way of thinking to study and learn what are the knowledge points of Java streams and files.

1.Stream stream a. Definition:

The Java.io package contains almost all the classes needed to manipulate input and output. All of these streams represent input sources and output destinations.

Streams in the Java.io package support a variety of formats, such as basic types, objects, localized character sets, and so on.

A stream can be understood as a sequence of data. The input stream represents reading data from a source, and the output stream represents writing data to a destination.

Java provides powerful and flexible support for iPUBO, which makes it more widely used in file transfer and network programming.

b. Console input:

The console input of Java is done by System.in.

To get a character stream bound to the console, you can wrap the System.in in a BufferedReader object to create a character stream.

BufferedReader br = new BufferedReader (new InputStreamReader (System.in))

After the BufferedReader object is created, we can use the read () method to read a character from the console, or the readLine () method to read a string.

The following shows how to read multi-character input. This code reads the console's character input until the user enters Q:

Char c

BufferedReader br = new BufferedReader (new InputStreamReader (System.in))

System.out.println ("enter characters and press the'Q' key to exit.")

Do {

C = (char) br.read ()

System.out.println (c)

}

While (c! ='q')

If you want to read a string, use readLine ()

c. Console output:

As mentioned earlier, the output of the console is done by print () and println (). These methods are defined by the class PrintStream, where System.out is a reference to that class object.

PrintStream inherits the OutputStream class and implements the method write (). In this way, write () can also write operations using the round-trip console. However, this method of writing is not commonly used.

Public static void main (String args [])

{

Int b; b ='A'

System.out.write (b)

System.out.write ('\ n')

}

Read and write 2.File files:

According to the previous chapter, a stream is defined as a data sequence. The input stream is used to read data from the source, and the output stream is used to write data to the destination.

The two important flows in this chapter are FileInputStream and FileOutputStream.

A.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 ("C:/java/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 ("C:/java/hello")

InputStream out = new FileInputStream (f)

B.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 ("C:/java/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 ("C:/java/hello"); OutputStream f = new FileOutputStream (f)

For specific methods, please see: https://www.runoob.com/java/java-files-io.html

C.File Class (class):

Java file classes represent file names and directory pathnames in an abstract way. This class is mainly used for file and directory creation, file search and file deletion.

The File object represents the files and directories that actually exist on disk. Create a File object with the following constructor.

Create a new File instance by converting a given pathname string to an abstract pathname:

File (String pathname)

Create a new File instance based on the parent pathname string and the child pathname string:

File (String parent, String child)

Create a new instance of File by converting the given file: URI to an abstract pathname.

File (URI uri)

D.Java FileReader class:

The FileReader class inherits from the InputStreamReader class. This class reads data in the stream by character.

/ / create a new File given the FileReader from which the data is read:

FileReader (File file)

/ / create a new FileDescriptor given the FileReader from which the data is read.

FileReader (FileDescriptor fd)

/ / create a new FileReader given the file name from which the data is read.

FileReader (String fileName)

E.Java FileWriter class:

The FileWriter class inherits from the OutputStreamWriter class. This class writes data to the stream by character. You can create the objects you need through the following construction methods

Construct a FileWriter object when the File object is given:

FileWriter (File file)

Construct a File object when the FileWriter object is given.

FileWriter (File file, boolean append)

Thank you for your reading, these are the contents of "what are the knowledge points of Java streams and documents". After the study of this article, I believe you have a deeper understanding of what the knowledge points of Java streams and documents have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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