Home > Java > javaTutorial > body text

Select Java classes suitable for file reading and writing

王林
Release: 2024-02-19 15:18:07
Original
1187 people have browsed it

Select Java classes suitable for file reading and writing

Selecting the appropriate Java class for file reading and writing requires specific code examples

It is a very common requirement to read and write files in Java. Java provides many types and methods to meet different needs. In order to choose the appropriate Java class for file reading and writing, we need to choose based on specific needs. Below I will introduce several common Java classes, their application scenarios in file reading and writing operations, and provide corresponding code examples.

  1. FileInputStream and FileOutputStream

FileInputStream and FileOutputStream are the most basic and commonly used file reading and writing classes in the Java IO package. They are used to read and write byte streams and can read and write various types of files.

Sample code 1: Use FileInputStream to read file content

import java.io.*;

public class FileReaderExample {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("example.txt");
            int content;
            while ((content = fis.read()) != -1) {
                System.out.print((char) content);
            }
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

Sample code 2: Use FileOutputStream to write file content

import java.io.*;

public class FileWriterExample {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("example.txt");
            String content = "Hello, World!";
            fos.write(content.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login
  1. BufferedReader and BufferedWriter

BufferedReader and BufferedWriter are efficient reading and writing classes in the Java IO package. They can improve the efficiency of file reading and writing, and are especially suitable for reading and writing large files.

Sample code 3: Use BufferedReader to read file content line by line

import java.io.*;

public class BufferedReaderExample {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("example.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

Sample code 4: Use BufferedWriter to write file content

import java.io.*;

public class BufferedWriterExample {
    public static void main(String[] args) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"));
            String content = "Hello, World!";
            bw.write(content);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login
  1. Scanner and PrintWriter

Scanner and PrintWriter are stream classes in the Java.util package, which provide a more convenient way to read and write file contents.

Sample code 5: Use Scanner to read file content

import java.io.*;
import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(new File("example.txt"));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

Sample code 6: Use PrintWriter to write file content

import java.io.*;

public class PrintWriterExample {
    public static void main(String[] args) {
        try {
            PrintWriter writer = new PrintWriter("example.txt");
            String content = "Hello, World!";
            writer.println(content);
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

The above are several common Java classes in files Application scenarios in read and write operations and corresponding code examples. Selecting the appropriate Java class for file reading and writing based on specific needs can improve the efficiency and readability of the code. At the same time, you also need to pay attention to correctly handling and closing streams when operating files to avoid resource leaks and file corruption.

The above is the detailed content of Select Java classes suitable for file reading and writing. For more information, please follow other related articles on the PHP Chinese website!

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