Maitiro Ekuverenga Mafaira muJava

Java inopa nzira dzinoverengeka kuverenga mafaera. Imwe yeidzi nzira dzakakodzera kuverenga marudzi akasiyana emafaira mumamiriro akasiyana. Mamwe ari nani pakuverenga mafaera akareba, mamwe ari nani pakuverenga mapfupi, nezvimwe.

Muchidzidzo ichi, tichave tichishandisa anotevera Java makirasi kuverenga mafaera

  • BufferedReader
  • Mafaira
  • Scanner

Pese padzidziso, tiri kushandisa faira rakachengetwa mu | _ _ _ _ | dhairekitori uko nzira yefaira iri src.

Chengetedza mitsara yakati wandei yemafaira mune iyi faira usati waenderera.

Ongorora:Iwe unofanirwa kubata nemazvo zvikanganiso kana uchishandisa zviitwa izvi kunamatira kune akanakisa macoding maitiro.

Kuverenga Mavara Mafaira muJava neBufferedReader

I src/file.txt kirasi inoverenga rukova-rwekuisa rwizi. Iyo inocherekedza mavara mune buffer ine default size ye8 KB kuita kuti kuverenga kuve kwakanyanyisa Kana iwe uchida kuverenga tambo yefaira netambo, kushandisa BufferedReader isarudzo yakanaka.

BufferedReader inoshanda mukuverenga mafaera mahombe.

BufferedReader

I 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();
} }
nzira inodzoka null kana kupera kwefaira kwasvika.

Ongorora:Usakanganwa kuvhara iyo faira kana kuverenga kwapera.

Kuverenga UTF-8 Yakanyorwa Faira muJava neBufferedReader

Tinogona kushandisa readline() kirasi kuverenga iyo UTF-8 yakanyorwa faira.

Ino nguva, tinopfuura BufferedReader chinhu paunenge uchigadzira InputStreamReader Semuenzaniso.

BufferedReader

Uchishandisa Java Mafaira Kirasi Kuverenga Faira

Java 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);
}
} }
kirasi, yakaunzwa mu Java 7 muJava NIO, ine zvizere nzira dzetsika dzinoshanda pamafaera.

Uchishandisa Files kirasi, unogona kuverenga zvese zvirimo mufaira muchirongwa. Izvi zvinoita kuti ive sarudzo yakanaka yekuverenga madiki mafaira.

Ngatione mashandisiro atingaite Files kirasi mune ese maviri mamiriro.

Kuverenga Madiki Mafaira muJava ine Mafaira Kirasi

I Files nzira ye readAllLines() kirasi inobvumidza kuverenga zvese zvirimo mufaira uye inochengetera mutsetse wega wega munhevedzano kunge tambo.

Unogona kushandisa Files kirasi kuti utore nzira yefaira kubva iyo Path kirasi inogamuchira iyo Files chinhu chefaira.

Path

Unogona kushandisa 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);
} }
kutora data rakachengetwa mufaira kune byte rongedzo pachinzvimbo chetambo.

readAllBytes()

Kuverenga Makuru Makuru muJava ine Mafaira Kirasi

Kana iwe uchida kuverenga hombe faira ine byte[] bytes = Files.readAllBytes(path); kirasi, unogona kushandisa Files nzira yekuwana muenzaniso we newBufferedReader() kirasi uye verenga iyo faira mutsara netambo uchishandisa BufferedReader.

BufferedReader

Kuverenga Mafaera neFiles.lines ()

Java 8 yakaunza nzira nyowani kuFiles kirasi kuti iverenge iyo faira rese mu 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();
} }
tambo.

Stream

Kuverenga Mavara Mafaira muJava ine Scanner

I 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();
} }
kirasi inoputsa zvirimo mufaira muzvikamu ichishandisa yakapihwa delimita uye yoiverenga chikamu nechikamu. Iyi nzira inokodzera kwazvo kuverenga zvemukati izvo zvakapatsanurwa nedherimita.

Semuenzaniso, iyo Scanner kirasi yakakosha kuverenga runyorwa rwemanhamba akapatsanurwa nenzvimbo chena kana rondedzero yetambo dzakaparadzaniswa nemakoma

Iyo yekumisikidza delimita ye Scanner kirasi inzvimbo chena. Asi iwe unogona kuseta iyo delimiter kune imwe hunhu kana chirevo chenguva dzose. Iyo zvakare ine dzakasiyana dzakasiyana nzira, senge Scanner, next(), nextInt(), uye nextLine(), kushandura zvemukati kuita mhando dzakasiyana.

nextByte()

Mumuenzaniso wepamusoro, isu tinogadza delimita kune whitespace uye tinoshandisa 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();
} }
nzira yekuverenga chinotevera chikamu chemukati chakaparadzaniswa nechena nzvimbo.

Kuverenga Iyo Yese Faira

Unogona kushandisa next() kirasi kuverenga iyo yose faira kamwechete pasina kumhanya chiuno. Iwe unofanirwa kupasa Scanner semugadziri weizvi.

“\Z”Ongorora:Iyo Scanner kirasi haina kuwiriraniswa uye nekudaro, haina tambo-yakachengeteka.

Mhedziso

Sezvawaona mune ino dzidziso, Java inopa nzira dzakawanda dzaungasarudza kubva maringe nerudzi rwebasa riri paruoko rwako kuti uverenge zvinyorwa zvemavara. Unogona kushandisa scanner.useDelimiter('\Z'); System.out.println(scanner.next()); scanner.close(); kuverenga makuru mafaera mutsara netambo.

Kana iwe uchida kuverenga iyo faira ine yayo yezvinyorwa yakaparadzaniswa nedimendi, shandisa iyo BufferedReader kirasi.

Zvakare unogona kushandisa Java NIO Scanner kirasi kuverenga ese madiki uye makuru mafaera.