Java Yenza imizekelo yeFayile

Ukwenza iifayile kwiJava kulula. Kule posi, siza kujonga iindlela ezine ezahlukeneyo zokwenza iifayile kwiJava. Into ekufuneka siyenzile kukungenisa ipakeji efanelekileyo kwaye sisebenzise iindlela ezifanelekileyo.

Imizekelo engezantsi isebenzisa java.io.file, java.io.fileOutputStream, kunye java.nio iphakheji. Ezi klasi zibonelelwa ngaphandle kwebhokisi kwiJava API. Sijonge nokwenza ifayile nge-Apache Commons.



Yenza ifayile eneklasi yeJava.io

Kumzekelo wokuqala, siya kusebenzisa createNewFile() indlela evela java.io.file iklasi. Le ndlela ibuyisela ixabiso le-boolean. Ibuyisa ubuxoki ukuba ifayile sele ikho, okanye iyinyani ukuba yenziwe.


import java.io.File; import java.io.IOException; public class CreateFileJavaExamples {
public static void main(String[] args) {
File file = new File('c://examples//newFile.txt');

try {

if (file.createNewFile()) {


System.out.println('File create');

} else {


System.out.println('File already exists!');

}
} catch (IOException e) {

System.out.println(e.getMessage());
}
} }

PHAWULA: Lo mzekelo ungasentla wenza ifayile engenanto kwindawo ebonelelweyo.

Idibeneyo:




Sebenzisa Java.io.fileOutputStream

Umzekelo olandelayo usebenzisa fileOutputStream. Kubalulekile ukuba uqaphele ukuba le isetyenziselwa ukwenza ifayile kwaye ubhale umxholo kuyo ngexesha elinye.



import java.io.FileOutputStream; public class CreateFileJavaExamples {
public static void main(String[] args) {
try {

new FileOutputStream('newFile.txt', true);
} catch (Exception e) {

System.out.println(e.getMessage());
}
} }

Ukuba ifayile ayikho, le ndlela ingentla iya kuyenza. Ukuba ifayile ikhona, idlula true ndiza kufaka umxholo kuyo.

PHAWULA: Lumka xa usebenzisa fileOutputStream. Ukuba ifayile ikhona kunye nomxholo, ukuba siyapasa false njengeparameter kwi fileOutputStream indlela, iya kubhala ngaphezulu ifayile kwaye umxholo uya kulahleka!



Yenza ifayile ngeJava.nio Package

Kumzekelo olandelayo, siya kusebenzisa java.nio package eyaziswa kwi-JDK 7.


Ukuze wenze ifayile nge nio package, kuqala kufuneka sisete indlela emva koko sisebenzise createFile() indlela evela Files iklasi. Ukwenza iifayile ngokudlula nio package lolukhetho lukhethiweyo njengoko i-API inomdla ngakumbi.

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

Path newFilePath = Paths.get('src/test/resources/newFile.txt');

Files.createFile(newFilePath);
}
catch (IOException e) {

}
} }

Lo mzekelo ungasentla wekhowudi uthatha umendo src/test/resources sele ihlala.



Iifayile ze-Apache Commons FileUtils

Ukuba awufuni ukusebenzisa iilayibrari ezisemgangathweni ezinikezelwe ngaphandle kwebhokisi evela kwiJava, ungasebenzisa FileUtils iklasi ukusuka Iipache Commons

import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; public class CreateFileJavaExamples {
public static void main(String[] args) {
File myFile = new File('src/test/resources/newFile.txt');


try {

FileUtils.touch(myFile);
} catch (IOException e) {

System.out.println(e.getMessage());
}
} }

Kumzekelo ongentla, sisebenzisa touch indlela yokwenza ifayile.