Home Java javaTutorial What are the basic knowledge points of Java streams and files?

What are the basic knowledge points of Java streams and files?

Apr 27, 2023 pm 02:34 PM
java

1.Stream

a.Definition:

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

Streams in the Java.io package support many formats, such as basic types, objects, localized character sets, etc.

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

Java provides powerful and flexible support for I/O, making it more widely used in file transfer and network programming.

b.Console input:

Console input for Java is done by System.in.

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

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

Once 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 will continue to read the character input of the console until the user enters q:

char c;

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

System.out.println("Enter characters, press '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 before, console output is completed by print() and println(). These methods are defined by the class PrintStream, and System.out is a reference to the object of this class.

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

public static void main(String args[])

{

int b; b = 'A';

System.out.write(b);

System.out.write('\n');

}

2.File file reading and writing:

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

The two important streams 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 using the keyword new.

There are various constructor methods used to create objects.

You can use a string file name 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 target file does not exist before the stream opens the file for output, the stream creates the file.

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

Create an output stream object using a string file name:

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, see: https://www.runoob.com/java/java-files-io.html

c.File Class:

The Java File class represents file names and directory pathnames in an abstract way. This class is mainly used for creating files and directories, searching for files, and deleting files.

File objects represent files and directories that actually exist on disk. Create a File object through the following constructor.

Creates a new File instance by converting the given pathname string into 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);

Creates a new File instance by converting the given file: URI into an abstract pathname.

File(URI uri);

d.Java FileReader class:

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

//Create a new FileReader given a File to read data from:

FileReader(File file);

//Create a new FileReader given a FileDescriptor to read data from.

FileReader(FileDescriptor fd);

//Create a new FileReader given the name of a file to read data from.

FileReader(String fileName);

e.Java FileWriter class:

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

Constructs a FileWriter object given a File object:

FileWriter(File file);

Constructs a FileWriter object given a File object.

FileWriter(File file, boolean append);

The above is the detailed content of What are the basic knowledge points of Java streams and files?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles