Cara Maca File ing Jawa

Java nyedhiyakake sawetara cara kanggo maca file. Saben cara kasebut cocog kanggo maca macem-macem jinis file ing macem-macem kahanan. Sawetara luwih becik maca file sing luwih dawa, sing liyane luwih becik maca sing luwih cendhek, lsp.

Ing tutorial iki, kita bakal nggunakake kelas Java ing ngisor iki kanggo maca file

  • BufferedReader
  • File
  • Scanner

Sajrone tutorial, kita nggunakake file sing disimpen ing src direktori ing endi dalane file kasebut src/file.txt.


Simpen sawetara baris teks ing file iki sadurunge dilanjutake.

Cathetan:Sampeyan kudu ngatasi kesalahan nalika nggunakake implementasi kasebut supaya bisa nindakake praktik kodhe paling apik.

Maca File Teks ing Jawa nganggo BufferedReader

Sing BufferedReader kelas maca stream input karakter. Buffer karakter ing buffer kanthi ukuran standar 8 KB kanggo nggawe proses maca luwih efisien. Yen sampeyan pengin maca baris file miturut baris, nggunakake BufferedReader minangka pilihan sing tepat.


BufferedReader efektif kanggo maca file gedhe.



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

Sing readline() cara ngasilake batal nalika pungkasane file wis tekan.

Cathetan:Aja lali nutup file nalika maca wis rampung.

Maca File sing Dienkode UTF-8 ing Jawa nganggo BufferedReader

Kita bisa nggunakake BufferedReader kelas kanggo maca file sing dienkode UTF-8.

Kali iki, kita liwati InputStreamReader obyek nalika nggawe BufferedReader Kayata


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


Nggunakake Kelas File Java kanggo Maca File

Jawa Files kelas, dikenalake ing Java 7 ing Java NIO, kalebu cara statis sing dioperasikake ing file.

Nggunakake Files kelas, sampeyan bisa maca isi lengkap file menyang larik. Iki nggawe pilihan sing apik kanggo maca file sing luwih cilik.

Ayo goleki cara nggunakake kelas File ing skenario loro kasebut.

Maca File Cilik ing Jawa Kanthi Kelas Kelas

Sing readAllLines() cara kanggo Files kelas ngidini maca kabeh isi file lan nyimpen saben baris ing larik minangka senar.


Sampeyan bisa nggunakake Path kelas kanggo entuk dalan menyang file wiwit Files kelas nampa Path obyek file.

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

Sampeyan bisa nggunakake readAllBytes() kanggo njupuk data sing disimpen ing file menyang larik bait tinimbang larik senar.

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

Maca File Gedhe ing Jawa Kanthi Kelas Kelas

Yen sampeyan pengin maca file gedhe nganggo Files kelas, sampeyan bisa nggunakake newBufferedReader() cara kanggo njupuk conto BufferedReader kelas lan waca baris file kanthi baris nggunakake 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();
} }


Maca File nganggo Files.lines ()

Java 8 ngenalake cara anyar menyang kelas Files kanggo maca kabeh file dadi Stream saka senar.


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


Maca File Teks ing Jawa nganggo Scanner

Sing Scanner kelas ngilangi isi file dadi bagean nggunakake pembates tartamtu lan maca kanthi bagean. Cara iki paling cocog kanggo maca konten sing dipisahake karo pembatas.

Contone, ing Scanner kelas cocog kanggo maca dhaptar bilangan bulat sing dipisahake karo spasi putih utawa dhaptar senar sing dipisahake karo koma.

Pembatas standar Scanner kelas minangka papan putih. Nanging sampeyan bisa nyetel pembates kanggo karakter liyane utawa ekspresi biasa. Uga duwe macem-macem cara sabanjure, kayata next(), nextInt(), nextLine(), lan nextByte(), kanggo ngowahi konten dadi macem-macem jinis.

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

Ing conto ing ndhuwur, kita nyetel pembates menyang papan putih lan nggunakake next() cara kanggo maca bagean sabanjure konten sing dipisahake karo spasi putih.


Maca File Seluruh

Sampeyan bisa nggunakake Scanner kelas kanggo maca kabeh file sekaligus tanpa mbukak daur ulang. Sampeyan kudu ngliwati “\Z” minangka pembatas kanggo iki.

scanner.useDelimiter('\Z'); System.out.println(scanner.next()); scanner.close(); Cathetan:Kelas Pemindai ora diselarasake, mula ora aman karo utas.

Kesimpulan

Kaya sing sampeyan deleng ing tutorial iki, Java nawakake akeh cara sing bisa dipilih miturut jinis tugas sing sampeyan lakoni kanggo maca file teks. Sampeyan bisa nggunakake BufferedReader kanggo maca file gedhe miturut baris.

Yen sampeyan pengin maca file sing isine dipisahake karo pembatas, gunakake Scanner kelas.

Sampeyan uga bisa nggunakake Java NIO Files kelas kanggo maca file cilik lan gedhe.