Giới thiệu java.io.tmpdir

Trong bài này, chúng ta sẽ tìm hiểu về thuộc tính hệ thống java.io.tmpdir. Thuộc tính hệ thống java.io.tmpdir chỉ ra thư mục tạm thời được sử dụng bởi Máy ảo Java (JVM) để tạo và lưu trữ các tập tin tạm thời.
Giá trị mặc định thường là “/tmp“, hoặc “/var/tmp” trên các nền tảng Linux. Trên các hệ thống Microsoft Windows, thuộc tính java.io.tmpdir thường là “C:\Users\gpcoder\AppData\Local\Temp\“.
Trong quá trình thực hiện ứng dụng của bạn, bạn có lấy giá trị của thuộc tính hệ thống java.io.tmpdir:

System.out.println(System.getProperty("java.io.tmpdir"));

1. Thay đổi giá trị mặc định của thuộc tính java.io.tmpdir

Trong trường hợp bạn muốn thay đổi thuộc tính hệ thống java.io.tmpdir, bạn có thể sử dụng tham số -Djava.io.tmpdir và chỉ định thư mục tạm của chính bạn.

Ví dụ:

java -Djava.io.tmpdir=/home/user/Temp

Bằng cách này, bạn thay đổi giá trị của thuộc tính hệ thống java.io.tmpdir, trong quá trình khởi tạo Máy ảo Java (JVM). Nếu không, bạn có thể sử dụng đoạn code dưới đây để thay đổi giá trị của thuộc tính hệ thống java.io.tmpdir:

System.setProperty ("java.io.tmpdir", "/home/user/Temp");

2. Tạo file tạm

Java cung cấp hai phương thức static để tạo file tạm thời qua lớp File:

public static File createTempFile(String prefix, String suffix, File directory);
public static File createTempFile(String prefix, String suffix);

Trong đó:

  • prefix: prefix của tên file tạm
  • suffix: phần mở rộng của file
  • directory: thư mục chứa file tạm. Nếu không xác định thì JVM sẽ lấy giá trị thư mục tạm mặc định của hệ thống.

3. Ví dụ sử dụng java.io.tmpdir

package com.javaio.simple.tmp;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
public class TmpDirExample {
    public static void main(String[] args) throws IOException {
        // Lấy thư mục tạm của hệ thống
        String tmpdir = System.getProperty("java.io.tmpdir");
        System.out.println("Thư mục tạm: " + tmpdir);
 
        // Thông tin file tạm
        String prefix = "maixuanviet_tmp_";
        String suffix = ".txt";
        File directory = new File("C:/usr");
 
        // Tạo các file tạm
        File tempFile = File.createTempFile(prefix, suffix);
        File tempFile2 = File.createTempFile(prefix, null);
        File tempFile3 = File.createTempFile(prefix, suffix, directory);
 
        // Lưu nội dung file tạm
        BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
        bw.write("Welcome to maixuanviet.com");
        bw.close();
 
        // Đọc nội dung file tạm
        System.out.println("---");
        BufferedReader br = new BufferedReader(new FileReader(tempFile));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        System.out.println("Nội dung file tạm: " + sb.toString());
 
        // Hiển thị thông tin file
        System.out.println("---");
        System.out.println("File " + tempFile.getName() + " được tạo ở thư mục: " + tmpdir);
        System.out.println("File " + tempFile2.getName() + " được tạo ở thư mục: " + tmpdir);
        System.out.println("File " + tempFile3.getName() + " được tạo ở thư mục: " + tmpdir);
 
        // Hiển thị thư mục cha
        System.out.println("---");
        System.out.println("Thư mục cha của " + tempFile.getName() + " là " + tempFile.getParent());
        System.out.println("Thư mục cha của " + tempFile2.getName() + " là " + tempFile2.getParent());
        System.out.println("Thư mục cha của " + tempFile3.getName() + " là " + tempFile3.getParent());
 
        // Xóa các file tạm
        System.out.println("---");
        System.out.println("Xóa file: " + tempFile.getName() + " -> " + tempFile.delete());
        System.out.println("Xóa file: " + tempFile2.getName() + " -> " + tempFile2.delete());
        System.out.println("Xóa file: " + tempFile3.getName() + " -> " + tempFile3.delete());
    }
}

Kết quả thực thi chương trình trên:

Thư mục tạm: C:\Users\maixuanviet\AppData\Local\Temp\
---
Nội dung file tạm: Welcome to maixuanviet.com
---
File maixuanviet_tmp_7234532703604598775.txt được tạo ở thư mục: C:\Users\maixuanviet\AppData\Local\Temp\
File maixuanviet_tmp_8116987631049481862.tmp được tạo ở thư mục: C:\Users\maixuanviet\AppData\Local\Temp\
File maixuanviet_tmp_2776246472534424156.txt được tạo ở thư mục: C:\Users\maixuanviet\AppData\Local\Temp\
---
Thư mục cha của maixuanviet_tmp_7234532703604598775.txt là C:\Users\maixuanviet\AppData\Local\Temp
Thư mục cha của maixuanviet_tmp_8116987631049481862.tmp là C:\Users\maixuanviet\AppData\Local\Temp
Thư mục cha của maixuanviet_tmp_2776246472534424156.txt là C:\usr
---
Xóa file: maixuanviet_tmp_7234532703604598775.txt -> true
Xóa file: maixuanviet_tmp_8116987631049481862.tmp -> true
Xóa file: maixuanviet_tmp_2776246472534424156.txt -> true

Related posts:

Spring Boot - Actuator
Exploring the New Spring Cloud Gateway
Java Program to Implement Circular Doubly Linked List
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Solve any Linear Equations
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Tính đóng gói (Encapsulation) trong java
Giới thiệu Json Web Token (JWT)
Concurrent Test Execution in Spring 5
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Find All Pairs Shortest Path
Error Handling for REST with Spring
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Limiting Query Results with JPA and Spring Data JPA
Hướng dẫn Java Design Pattern – Builder
Mix plain text and HTML content in a mail
Configure a Spring Boot Web Application
The DAO with JPA and Spring
Spring Data MongoDB Transactions
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Perform Searching Based on Locality of Reference
Marker Interface trong Java
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java 8 Predicate Chain
Getting the Size of an Iterable in Java
Apache Commons Collections BidiMap
Java Program to Implement Interval Tree
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
How to Change the Default Port in Spring Boot
Java Program to Check Whether a Given Point is in a Given Polygon
Java Program to Implement Find all Cross Edges in a Graph