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:

Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Java – Generate Random String
Simple Single Sign-On with Spring Security OAuth2
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Generate a Random Subset by Coin Flipping
Java Program to Implement PriorityQueue API
Java Program to Implement Max Heap
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Java Program to Implement Hash Tables Chaining with Binary Trees
OAuth2.0 and Dynamic Client Registration
Spring Security Login Page with React
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to Construct K-D Tree for 2 Dimensional Data
Automatic Property Expansion with Spring Boot
Test a REST API with Java
Java Program to Implement Bellman-Ford Algorithm
Spring 5 WebClient
REST Pagination in Spring
Spring WebClient and OAuth2 Support
Java Program to Create a Balanced Binary Tree of the Incoming Data
Java Program to Find Path Between Two Nodes in a Graph
Java Program to Implement Suffix Tree
Comparing Two HashMaps in Java
Java Program to Implement the Checksum Method for Small String Messages and Detect
How to Convert List to Map in Java
Truyền giá trị và tham chiếu trong java
Java – Reader to Byte Array
Java Program to Implement PrinterStateReasons API
An Intro to Spring Cloud Zookeeper
Java Program to Implement Counting Sort
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)