Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Tạo File Tạm Trong Java: Hướng Dẫn Thực Hành

Tạo File Tạm

Có những lúc chúng ta cần tạo file tạm (temporary files) ngay lập tức để lưu trữ một số thông tin và xóa chúng sau đó.

Trong Java, chúng ta có thể dùng phương thức Files.createTempFile() để tạo file tạm.

Sử Dụng Files.createTempFile(prefix, suffix)

Ví dụ sau dùng Files.createTempFile(prefix, suffix) để tạo một file tạm.

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

public class CreateTempFile {
    public static void main(String[] args) {
        try {
            // Tạo một file tạm
            Path tempFile = Files.createTempFile("temp-", ".txt");
            System.out.println("Temp file : " + tempFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Đầu ra:

Temp file : /var/folders/nyckvw0000gr/T/temp-2129139085984899264.txt

Ở đây, "temp-" là prefix (tiền tố) và ".txt" là suffix (hậu tố).

Lưu ý: Mặc định Java tạo file tạm trong thư mục tạm (temporary directory). Chúng ta có thể lấy thư mục tạm bằng System.getProperty("java.io.tmpdir").

Nếu suffix là null, file tạm được tạo với phần mở rộng .tmp.

Ví dụ:

Path tempFile = Files.createTempFile("prefix-", null);
System.out.println("Temp file : " + tempFile);
// Temp file : /var/folders/nyckvw0000gr/T/prefix-17184288103181464441.tmp

Cũng nếu suffix không được cung cấp, file tạm được tạo không có phần mở rộng:

Path tempFile = Files.createTempFile(null, "");
System.out.println("Temp file : " + tempFile);
// Temp file : /var/folders/nyckvw0000gr/T/1874152090427250275

Tạo File Tạm Trong Thư Mục Chỉ Định

Thay vì để Java chọn thư mục, chúng ta có thể nói cho nó biết nơi tạo file tạm.

Ví dụ:

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

public class CreateTempFile {
    public static void main(String[] args) {
        try {
            Path path = Paths.get("target/tmp/");
            // Tạo file tạm trong thư mục chỉ định.
            Path tempFile = Files.createTempFile(path, null, ".log");
            System.out.println("Temp file : " + tempFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Lưu ý: Thư mục chỉ định phải tồn tại để file tạm được ghi vào đó.

Tạo File Tạm Và Ghi Vào Nó

Đoạn mã sau tạo một file tạm và sau đó ghi một số văn bản vào nó:

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 CreateTempFile {
    public static void main(String[] args) {
        try {
            Path path = Paths.get("target/tmp/");
            // Tạo file tạm trong thư mục chỉ định.
            Path tempFile = Files.createTempFile(path, null, ".log");
            System.out.println("Temp file : " + tempFile);
            // Ghi một dòng
            Files.write(tempFile, "Hello From Temp File\n".getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • Java ghi file (Java write to files)
  • Java tạo file (Java create file)
  • Java đọc file (Java read files)