Java Gadzira Mienzaniso yeFaira

Kugadzira mafaera muJava zviri nyore. Mune ino posvo, isu tichatarisa nzira ina dzakasiyana dzekugadzira mafaera muJava. Zvese isu zvatinofanirwa kuita kuendesa akakodzera pasuru uye kushandisa akakodzera nzira.

Iyi mienzaniso iripazasi inoshandisa java.io.file, java.io.fileOutputStream, uye java.nio package. Aya makirasi anopihwa kunze kwebhokisi muJava API. Isu tinotarisawo kugadzira faira neApache Commons.

Gadzira Faira nejava.io.file kirasi

Mumuenzaniso wekutanga, isu tinoshandisa createNewFile() nzira kubva java.io.file kirasi. Iyi nzira inodzosera kukosha boolean. Iyo inodzoka nhema kana iyo faira yatovepo, kana ichokwadi kana yakagadzirwa.

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

ONA: Muenzaniso wepamusoro unogadzira isina chinhu faira munzvimbo yakapihwa.

Inoenderana:

Uchishandisa Java.io.fileOutputStream

Muenzaniso unotevera unoshandisa fileOutputStream. Izvo zvakakosha kuti uzive kuti izvi zvinonyanya kushandiswa kugadzira faira uye kunyora zvemukati mairi mune imwe nzira.

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

Kana iyo faira isipo, nzira iri pamusoro ichaigadzira. Kana iyo faira iripo, ichipfuura true ndichangoisa zvemukati mairi.

ONA: Ngwarira paunenge uchishandisa fileOutputStream. Kana iyo faira riripo nezviri mukati, kana tikapasa false senge paramende kune fileOutputStream nzira, ichatsikisa iyo faira uye zvemukati zvicharasika!

Gadzira Faira ne Java.nio Package

Mumuenzaniso unotevera, isu tinoshandisa java.nio package iyo yakaunzwa muJDK 7.

Kuti ugadzire faira ne nio package, isu tinotanga tada kuseta nzira tobva tashandisa createFile() nzira kubva Files kirasi. Kugadzira mafaera kuburikidza neiyo nyowani _ _ + _ | package ndiyo sarudzo inosarudzika sezvo iyo API iri nyore kunzwisisa.

nio

Iyi iri pamusoro kodhi muenzaniso inotora nzira 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) {

}
} }
yatovepo.

Apache Commons FileUtils

Kana iwe usiri kuda kushandisa akajairwa maraibhurari akapihwa kunze kwebhokisi kubva kuJava, unogona kushandisa src/test/resources kirasi kubva Apache Commons

FileUtils

Mumuenzaniso uri pamusoro, tinoshandisa 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());
}
} }
nzira yekugadzira faira.