Java Abuur Tusaalooyin Fayl

Abuuritaanka faylasha Java waa fududahay. Qoraalkan, waxaan eegaynaa afar dariiqo oo kala duwan oo lagu abuuro faylasha Java. Waxa kaliya ee aan u baahanahay waa inaan soo dejino xirmada ku habboon oo aan isticmaalno habab ku habboon.

Tusaalooyinka hoose waxay adeegsadaan java.io.file, java.io.fileOutputStream, iyo java.nio xirmo. Fasalladaan waxaa lagu bixiyaa sanduuqa Java API. Waxaan sidoo kale fiirineynaa sameynta feyl leh Apache Commons.

Abuur Fayl leh fasalka java.io.file

Tusaalaha koowaad, waxaan adeegsan doonnaa createNewFile() Habka ka socda java.io.file fasalka. Habkani wuxuu soo celinayaa qiimaha boolika. Waxay ku soo noqotaa been haddii feylku horey u jiray, ama run haddii la abuuray.

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

FIIRO GAAR AH: Tusaalaha kore wuxuu abuuraa feyl madhan meesha la bixiyay.

La xiriira:

Isticmaalka java.io.fileOutputStream

Tusaalaha soo socda ayaa adeegsanaya fileOutputStream. Waxaa muhiim ah in la ogaado in tan badiyaa loo adeegsado sameynta feyl isla markaana lagu qoro nuxurka hal mar.



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

Haddii faylka uusan jirin, habka kor ku xusan ayaa abuuri doona. Haddii feylku jiro, gudbinta true kaliya ku lifaaqi doonaa waxa ku jira.

FIIRO GAAR AH: Ka taxaddar markaad isticmaaleyso fileOutputStream. Haddii feylku ku jiro nuxur, haddii aan gudubno false sida halbeegga loogu talagalay fileOutputStream habka, waxay dib uga qori doontaa feylka waxa ku jirana waa la waayi doonaa!

Abuur Fayl leh xirmada java.nio

Tusaalaha soo socda, waxaan u adeegsan doonnaa java.nio xirmo oo lagu soo bandhigay JDK 7.

Si loo abuuro feyl leh nio xirmo, waxaan marka hore u baahanahay inaan dejino wadada kadibna aan isticmaalno createFile() Habka ka socda Files fasalka. Abuuritaanka feylasha iyadoo loo marayo cusub nio xirmada ayaa ah xulashada la door bidaayo maaddaama API-ga uu yahay mid dareen badan.

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) {

}
} }

Tusaalaha kore ee kor ku xusan wuxuu qaadanayaa wadada src/test/resources hore u jirtay

Apache Commons FileUtils

Haddii aadan rabin inaad isticmaasho maktabadaha caadiga ah ee laga bixiyo sanduuqa Java, waxaad isticmaali kartaa FileUtils fasalka ka Apache 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());
}
} }

Tusaalaha kore, waxaan u adeegsanaa touch habka loo abuuro feyl.