Java Fai Faila Faʻataʻitaʻiga

Faigofie faila i Java e faigofie. I lenei pou, o le a tatou vaʻai ai i auala eseʻese e fa e fausia ai faila i Java. Pau a le mea e tatau ona tatou faia o le faʻaulufale mai o le afifi talafeagai ma faʻaaoga metotia talafeagai.

O faʻataʻitaʻiga o i lalo e faʻaaoga ai le java.io.file, java.io.fileOutputStream, ma le java.nio afifi O nei vasega o loʻo saunia mai le atigipusa i le Java API. Ma matou vaʻai foʻi i le fatuina o se faila ma le Apache Commons.

Fausia Faila ma le java.io.file vasega

I le faʻataʻitaʻiga muamua, o le a matou faʻaaogaina createNewFile() auala mai le java.io.file vasega. Lenei metotia faʻafoʻi se boolean aoga. E toe faʻafoʻi mai pe a fai o iai le faila, pe moni pe a fai.

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

FAʻAALIGA: O le faʻataʻitaʻiga i luga atu e fausia ai se faila gaogao i le nofoaga saunia.

Fesootai:

Faʻaaogaina java.io.fileOutputStream

O le isi faʻataʻitaʻiga faʻaaoga fileOutputStream. E taua le maitauina o lenei e tele lava faʻaaoga e fausia ai se faila ma tusi ai mea ia te ia i le tasi le alu.



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

Afai e le oi ai le faila, o le auala i luga o le a fausiaina ai. Afai o iai le faila, pasi true o le a na faʻaopopo mea i ai.

FAʻAALIGA: Faʻaeteete pe a faʻaaoga fileOutputStream. Afai o iai le faila ma mea i totonu, afai matou te pasi false e fai ma faʻailoga i le fileOutputStream metotia, o le a soʻose faila ma o le a leiloloa mea!

Fausia faila ma le java.nio Afifi

I le faʻataʻitaʻiga lenei, o le a matou faʻaaogaina java.nio afifi na faʻalauiloa i le JDK 7.

Ina ia mafai ona fai se faila ma le nio afifi, matou te manaʻomia muamua ona seti le ala ona faʻaoga ai lea o le createFile() auala mai le Files vasega. Fausia faila e ala i le fou nio afifi o le filifiliga filifilia ona o le API e sili atu tupu fua.

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

}
} }

O le faʻataʻitaʻiga i luga atu o le tulafono o loʻo avea ma ala src/test/resources ua uma ona i ai.

Apache Commons FileUtils

Afai e te le manaʻo e faʻaoga ni faletusi masani na tuʻufaʻatasia mai le pusa mai Java, e mafai ona e faʻaaoga FileUtils vasega mai 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());
}
} }

I le faʻataʻitaʻiga ua taua i luga, matou te faʻaaogaina ai le touch metotia e fausia ai se faila.