Home >Java >javaTutorial >File storage and access

File storage and access

王林
王林Original
2024-02-18 12:50:061058browse

File storage and access

Files file storage and reading, specific code examples are required

File storage and reading is one of the operations often involved in computer program development. Whether it is saving user data, reading configuration files, or storing program output, file access is essential. In the Java programming language, you can use the Files class to perform file access operations. This article will introduce you to the common methods of the Files class and provide specific code examples.

1. File Storage

  1. Creating Files

In Java, you can use the static method createFile() of the Files class to create a new file. The following code example shows how to create a text file named "example.txt" in the specified directory.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileStorageExample {
    public static void main(String[] args) {
        String filename = "example.txt";
        try {
            Path path = Paths.get(filename);
            Files.createFile(path);
            System.out.println("文件创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. Write files

Use the write() method of the Files class to write data to the file. The following code example shows how to write a piece of text to a specified file.

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileStorageExample {
    public static void main(String[] args) {
        String filename = "example.txt";
        String content = "Hello, World!";
        try {
            Path path = Paths.get(filename);
            Files.write(path, content.getBytes(StandardCharsets.UTF_8));
            System.out.println("数据写入文件成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. Append file content

If you need to append content to an existing file, you can use the second parameter of the write() method of the Files class to specify the file The writing mode is append. The following code example demonstrates how to append text to an existing file.

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileStorageExample {
    public static void main(String[] args) {
        String filename = "example.txt";
        String content = "追加的内容";
        try {
            Path path = Paths.get(filename);
            Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
            System.out.println("追加内容成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. File reading

  1. Reading file contents

Use the readAllLines() method of the Files class to read the contents of the entire file. And returned in the form of List. The following code example shows how to read the contents of a specified file.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class FileStorageExample {
    public static void main(String[] args) {
        String filename = "example.txt";
        try {
            Path path = Paths.get(filename);
            List<String> lines = Files.readAllLines(path);
            for (String line : lines) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. Read part of the file

Use the newBufferedReader() method of the Files class to create a BufferedReader object, and then read the file content line by line. The following code example shows how to read the first 5 lines of a specified file.

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileStorageExample {
    public static void main(String[] args) {
        String filename = "example.txt";
        try {
            Path path = Paths.get(filename);
            BufferedReader reader = Files.newBufferedReader(path);
            String line;
            int linesToRead = 5;
            while ((line = reader.readLine()) != null && linesToRead > 0) {
                System.out.println(line);
                linesToRead--;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The above code examples show common methods of the Files class, including operations such as creating files, writing files, appending file contents, and reading file contents. By learning and understanding the use of these methods, I believe you can perform file access operations more flexibly, thereby improving programming efficiency. I hope this article is helpful to you, thank you for reading.

The above is the detailed content of File storage and access. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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