Sida Loo Sameeyo Fayl Ku Meel Gaarka ah Java

Waxaa jira waqtiyo aan u baahannahay inaan ku abuurno feylallo ku meelgaar ah duulimaadka si aan u kaydinno macluumaadka qaarkood oo aan u tirtirno ka dib.

Java dhexdeeda, waxaan ku isticmaali karnaa Files.createTempFile() hababka loo abuuro faylal ku meel gaar ah.

Abuur Faylal Ku-meelgaar ah

Tusaalaha soo socda ayaa adeegsanaya Files.createTempFile(prefix, suffix) in la abuuro feyl ku meel gaar ah

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();
}
} }

Wax soo saarka:

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

Halkan, 'temp-' waa horgale iyo '.text' waa ku-dheh.

Fiiro gaar ah:Marka hore Java ayaa sameysa feylka ku meel gaarka ah ee galka ku meel gaarka ah. Waxaan ku heli karnaa diiwaanka ku meelgaarka ah annaga oo samaynaya System.getProperty('java.io.tmpdir')

Haddii dabakhku uusan waxba ka jirin, faylka ku meel gaarka ah ayaa lagu abuuray .tmp kordhinta.



Tusaale ahaan:

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

Sidoo kale haddii dhejiska aan la bixin feyl temp ah ayaa la abuuray iyada oo aan la kordhinayn:

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

Abuur Fayl ku-meel-gaar ah Diiwaanka Gaarka ah

Halkii aan u oggolaan lahayn Java inay doorto galka, waxaan u sheegi karnaa halka laga abuurayo feylka ku meel gaarka ah.

Tusaale ahaan:

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();
}
} }
Fiiro gaar ah:Diiwaanka la cayimay waa inuu jiraa faylka ku meel gaarka ah ee loo qorayo.

Abuur Fayl ku-meel-gaar ah oo ku qor

Tusaalaha koodhka soo socdaa wuxuu abuuraa fayl ku meel gaar ah ka dibna wuxuu u qoraa xoogaa qoraal ah:

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();
}
} }

Akhrin Dheeraad ah: