Maitiro Ekugadzira Iyo Yechinguva Faira muJava

Pane dzimwe nguva patinoda kugadzira mafaera enguva pfupi pane nhunzi kuti tichengete rumwe ruzivo uye tigozvidzima mumashure.

MuJava, tinogona kushandisa Files.createTempFile() nzira dzekugadzira mafaera echinguvana.

Gadzira Mafaira Enguva pfupi

Muenzaniso unotevera unoshandisa Files.createTempFile(prefix, suffix) kugadzira faira renguva pfupi.

import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class CreateTempFile {
public static void main(String[] args) {

try {

// Create a temporary file

Path tempFile = Files.createTempFile('temp-', '.txt');

System.out.println('Temp file : ' + temp);

} catch (IOException e) {

e.printStackTrace();
}
} }

Kubuda:

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

Pano, 'temp-' ndicho chivakamberi uye '.text' ndicho chirevo.

Ongorora:Nokusingaperi Java inogadzira iyo yenguva pfupi faira mune dhairekitori dhairekitori. Tinogona kuwana dhairekitori rechinguva nekuita System.getProperty('java.io.tmpdir')

Kana chirevo chacho chisina basa, iyo faira rechinguvana inogadzirwa ne .tmp kuwedzera.

Semuyenzaniso:

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

Zvakare kana suffix isina kupihwa iyo temp faira inogadzirwa isina kuwedzerwa:

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

Gadzira Temp Faira mune Yakatarwa Dhairekitori

Panzvimbo pekurega Java ichisarudza dhairekitori, tinogona kuiudza iko kugadzira faira rechinguva.

Semuyenzaniso:

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/');

// Create a temporary file in the specified directory.

Path tempFile = Files.createTempFile(path, null, '.log');

System.out.println('Temp file : ' + temp);

} catch (IOException e) {

e.printStackTrace();
}
} }
Ongorora:Dhairekitori rakadomwa rinofanirwa kunge riripo kuti iyo faira rechinguva rinyorwe kwariri.

Gadzira Temp File uye Nyora kwariri

Iyi inotevera kodhi muenzaniso inogadzira faira rechinguva uye wozonyora rumwe rugwaro kwariri:

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/');

// Create an temporary file in a specified directory.

Path tempFile = Files.createTempFile(path, null, '.log');

System.out.println('Temp file : ' + tempFile);

// write a line

Files.write(tempFile, 'Hello From Temp File '.getBytes(StandardCharsets.UTF_8));

} catch (IOException e) {

e.printStackTrace();
}
} }

Kuenderera mberi kuverenga: