Home > Java > javaTutorial > How does Java I/O stream perform string operations?

How does Java I/O stream perform string operations?

PHPz
Release: 2024-04-13 12:06:02
Original
1077 people have browsed it

Java I/O streams provide Reader and Writer classes to process string data. Reader is used to read text from a character input stream. Single characters can be read using the read() method. Writer is used to write text to a character output stream. Text can be written using the write() method.

Java I/O流如何进行字符串操作?

String operations in Java I/O streams

Java I/O streams provide powerful functions for processing characters String data. By using the Reader and Writer classes, you can conveniently read strings from a variety of sources and write to a variety of destinations.

Reader

Reader class is used to read text data from a character input stream. You can use the following methods to process strings:

// 从 FileInputStream 中读取字符串
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("input.txt"))) {
    StringBuilder sb = new StringBuilder();
    int c;
    while ((c = reader.read()) != -1) {
        sb.append((char) c);
    }
    String text = sb.toString();
} catch (IOException e) {
    e.printStackTrace();
}
Copy after login

Writer

Writer class is used to write text data to a character output stream. You can use the following methods to process strings:

// 向 FileWriter 中写入字符串
try (FileWriter writer = new FileWriter("output.txt")) {
    String text = "Hello, world!";
    writer.write(text);
    writer.flush();
} catch (IOException e) {
    e.printStackTrace();
}
Copy after login

Practical case: reading and writing strings from files

Consider a scenario: You have a Text file input.txt, which contains some text. You want to read the contents of the file and copy the contents into another text file output.txt.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

class FileCopy {
    public static void main(String[] args) {
        try {
            // 从 input.txt 中读取
            File inputFile = new File("input.txt");
            FileReader reader = new FileReader(inputFile);
            BufferedReader bufferedReader = new BufferedReader(reader);

            // 向 output.txt 中写入
            File outputFile = new File("output.txt");
            FileWriter writer = new FileWriter(outputFile);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                writer.write(line + "\n");
            }

            reader.close();
            writer.close();
            System.out.println("文件已复制完成。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

The above is the detailed content of How does Java I/O stream perform string operations?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template