Uzifunda njani iifayile kwiJava

IJava ibonelela ngeendlela ngeendlela zokufunda iifayile. Nganye yezi ndlela ifanelekile ekufundeni iintlobo ezahlukeneyo zeefayile kwiimeko ezahlukeneyo. Ezinye zibhetele ekufundeni iifayile ezinde, ezinye zilungele ukufunda ezimfutshane, njl.

Kule tutorial, siya kuba sisebenzisa ezi klasi zilandelayo zeJava ukufunda iifayile

  • IBufferedReader
  • Iifayile
  • Iskena

Ngalo lonke ixesha lokufundisa, sisebenzisa ifayile egcinwe kwi- src isikhombisi apho indlela eya kwifayile ikwi src/file.txt.


Gcina imigca emininzi yokubhaliweyo kule fayile ngaphambi kokuba uqhubeke.

Phawula:Kuya kufuneka uphathe ngokufanelekileyo iimpazamo xa usebenzisa oku kuphunyezwa ukuze unamathele kwezona ndlela zibalaseleyo zokufaka iikhowudi.

Kufundwa iifayile zetekisi kwiJava eneBufferedReader

I BufferedReader iklasi ifunda umjelo wokungenisa umlinganiswa Iyabalinganisa abalinganiswa kwisikhuseli esinobungakanani obungagqibekanga obuyi-8 KB ukwenza inkqubo yokufunda isebenze ngakumbi. Ukuba ufuna ukufunda umgca wefayile ngomgca, usebenzisa iBufferedReader lukhetho olufanelekileyo.


BufferedReader isebenza ngokukuko ekufundeni iifayile ezinkulu.

import java.io.*; public class FileReaderWithBufferedReader {
public static void main(String[] args) throws IOException{We
String file = 'src/file.txt';
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

String curLine;
while ((curLine = bufferedReader.readLine()) != null){

//process the line as required

System.out.println(curLine);
}
bufferedReader.close();
} }

I readline() indlela ibuyisela ukungasebenzi xa ukuphela kwefayile kufikelelwe.

Phawula:Ungalibali ukuvala ifayile xa ugqibile ukufunda.

Kufundwa i-UTF-8 yeFayile eKhowudiweyo kwiJava eneBufferedReader

Singasebenzisa BufferedReader iklasi yokufunda ifayile efakwe kwi-UTF-8.

Ngeli xesha, sidlula InputStreamReader into xa usenza BufferedReader Umzekelo.


import java.io.*; public class EncodedFileReaderWithBufferedReader {
public static void main(String[] args) throws IOException {
String file = 'src/fileUtf8.txt';
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), 'UTF-8'));

String curLine;
while ((curLine = bufferedReader.readLine()) != null){

//process the line as you require

System.out.println(curLine);
}
} }


Sebenzisa udidi lweefayile zeJava ukufunda ifayile

IJava Files class, Yaziswa kwiJava 7 kwiJava NIO, ineendlela ezimileyo ezisebenza kwiifayile.

Sebenzisa Files iklasi, ungafunda umxholo opheleleyo wefayile kuludwe. Oku kuyenza ibe lukhetho olufanelekileyo lokufunda iifayile ezincinci.

Makhe sibone ukuba singayisebenzisa njani iklasi yeefayile kuzo zombini ezi meko.

Kufundwa iifayile ezincinci kwiJava ngeKlasi yeeFayile

I readAllLines() indlela ye Files iklasi ivumela ukufunda wonke umxholo wefayile kwaye igcine umgca ngamnye kuluhlu njengeentambo.


Ungasebenzisa Path iklasi yokufumana indlela eya kwifayile ukusukela Files | iklasi yamkela Path into yefayile.

import java.io.IOException; import java.nio.file.*; import java.util.*; public class SmallFileReaderWithFiles {
public static void main(String[] args) throws IOException {
String file = 'src/file.txt';
Path path = Paths.get(file);
List lines = Files.readAllLines(path);
} }

Ungasebenzisa readAllBytes() ukubuyisa idatha egcinwe kwifayile kuludwe lwe-Byte endaweni yoluhlu lomtya.

byte[] bytes = Files.readAllBytes(path);

Kufundwa iifayile ezinkulu kwiJava ngeKlasi yeeFayile

Ukuba ufuna ukufunda ifayile enkulu nge Files iklasi, ungasebenzisa newBufferedReader() indlela yokufumana umzekelo BufferedReader iklasi kwaye ufunde umgca wefayile ngomgca usebenzisa BufferedReader.

import java.io.*; import java.nio.file.*; public class LargeFileReaderWithFiles {
public static void main(String[] args) throws IOException {
String file = 'src/file.txt';
Path path = Paths.get(file);
BufferedReader bufferedReader = Files.newBufferedReader(path);

String curLine;
while ((curLine = bufferedReader.readLine()) != null){

System.out.println(curLine);
}
bufferedReader.close();
} }


Kufundwa iifayile ngeefayile.line ()

IJava 8 yazise indlela entsha kwiklasi yeefayile yokufunda yonke ifayile kwi Stream yeentambo.


import java.io.IOException; import java.nio.file.*; import java.util.stream.Stream; public class FileReaderWithFilesLines {
public static void main(String[] args) throws IOException {
String file = 'src/file.txt';
Path path = Paths.get(file);
Stream lines = Files.lines(path);

lines.forEach(s -> System.out.println(s));
lines.close();
} }


Ukufunda iifayile zetekisi kwiJava kunye neSkena

I Scanner iklasi yaphula umxholo wefayile ibe ngamacandelo kusetyenziswa isinikezeli esinikiweyo kwaye siyifunde ngenxalenye. Le ndlela ilunge kakhulu ekufundeni umxholo owahlulwe ngumduli.

Umzekelo, i Scanner iklasi ilungele ukufunda uluhlu lwamanani apheleleyo ahlukaniswe zizithuba ezimhlophe okanye uluhlu lwemitya eyahlulwe ngeekoma.

Umda osisigxina we Scanner iklasi yindawo emhlophe. Kodwa unokuseta i-delimiter komnye umlinganiswa okanye intetho eqhelekileyo. Ikwanazo neendlela ezahlukeneyo ezilandelayo, ezinje nge next(), nextInt(), nextLine(), kunye nextByte(), ukuguqula umxholo ube ziindidi ezahlukeneyo.

import java.io.IOException; import java.util.Scanner; import java.io.File; public class FileReaderWithScanner {
public static void main(String[] args) throws IOException{
String file = 'src/file.txt';
Scanner scanner = new Scanner(new File(file));
scanner.useDelimiter(' ');

while(scanner.hasNext()){

String next = scanner.next();

System.out.println(next);
}
scanner.close();
} }

Kumzekelo ongentla, sibeka i-delimiter kwindawo emhlophe kwaye sisebenzise next() indlela yokufunda icandelo elilandelayo lomxholo ohlukaniswe yindawo emhlophe.


Kufundwa ifayile yonke

Ungasebenzisa Scanner iklasi yokufunda yonke ifayile ngaxeshanye ngaphandle kokuqhuba iluphu. Kuya kufuneka udlule “\Z” njengomda wokugqibela woku.

scanner.useDelimiter('\Z'); System.out.println(scanner.next()); scanner.close(); Phawula:Udidi lweSkena aluhambelani kwaye ke ngoko, alukhuselekanga ngentambo.

Isiphelo

Njengoko ubonile kwesi sifundo, iJava ibonelela ngeendlela ezininzi onokukhetha kuzo ngokwendalo yomsebenzi osesandleni sakho wokufunda iifayile zombhalo. Ungasebenzisa BufferedReader ukufunda iifayile ezinkulu ngomgca.

Ukuba ufuna ukufunda ifayile enomxholo wayo eyahlulwe ngumhleli, sebenzisa Scanner iklasi.

Kananjalo ungasebenzisa i-Java NIO Files iklasi yokufunda zombini iifayile ezincinci nezikhulu.