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:

The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Check if there is mail waiting
REST Web service: Basic Authentication trong Jersey 2.x
Java Program to Implement LinkedBlockingQueue API
Basic Authentication with the RestTemplate
Guide to ThreadLocalRandom in Java
Java Program to Implement Binomial Tree
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Một số nguyên tắc, định luật trong lập trình
Java Program to Perform the Shaker Sort
Jackson – Change Name of Field
Guide to the Synchronized Keyword in Java
A Guide to TreeMap in Java
Lớp Collections trong Java (Collections Utility Class)
Java – Write an InputStream to a File
Java Program to Implement Double Ended Queue
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement the Hill Cypher
“Stream has already been operated upon or closed” Exception in Java
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Cachable Static Assets with Spring MVC
Chuyển đổi giữa các kiểu dữ liệu trong Java
Fixing 401s with CORS Preflights and Spring Security
Giới thiệu JDBC Connection Pool
Java Program to Generate a Random Subset by Coin Flipping
Generic Constructors in Java
Java Program to Implement Weight Balanced Tree
ExecutorService – Waiting for Threads to Finish
Java – Reader to String
How to Get All Dates Between Two Dates?
Java Program to Implement Patricia Trie
Java Program to Implement ConcurrentHashMap API