V Javi obstaja več načinov za branje besedilne datoteke, odvisno od velikosti podatkov in primera uporabe. The java.io in paketi java.nio.file zagotoviti več razredov za učinkovito upravljanje branja datotek. Razpravljajmo o skupnih pristopih enega za drugim.
1. Uporaba razreda BufferedReader
BufferedReader razred bere besedilo iz toka znakov in shrani znake v medpomnilnik za učinkovito branje. Pogosto je ovit okoli a FileReader oz InputStreamReader za izboljšanje delovanja.
Sintaksa
JavaBufferedReader in = new BufferedReader(Reader in int size);
import java.io.*; public class UsingBufferReader { public static void main(String[] args) throws Exception{ // Creating BufferedReader for Input BufferedReader bfri = new BufferedReader( new InputStreamReader(System.in)); System.out.print('Enter the Path : '); // Reading File name String path = bfri.readLine(); BufferedReader bfro = new BufferedReader(new FileReader(path)); String st; while ((st = bfro.readLine()) != null) System.out.println(st); } }
Izhod
Izhod2. Razred FileReader za branje besedilne datoteke
The Razred FileReader se uporablja za branje besedilnih datotek v Javi. Bere znake iz datoteke in je primeren za branje navadnega besedila. Konstruktorji tega razreda predpostavljajo, da sta privzeto kodiranje znakov in privzeta velikost medpomnilnika bajtov ustrezna.
Konstruktorji, definirani v tem razredu, so naslednji:
- FileReader (datoteka): Ustvari nov FileReader glede na datoteko za branje
- FileReader(FileDescriptor fd): Ustvari nov FileReader, ki mu je dodeljen FileDescriptor za branje
- FileReader(String fileName): Ustvari nov FileReader z imenom datoteke za branje
import java.io.*; public class UsingFileReader { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print('Enter the Path : '); // Reading File name String path = br.readLine(); FileReader fr = new FileReader(path); int i; // Holds true till there is nothing to read while ((i = fr.read()) != -1) // Print all the content of a file System.out.print((char)i); } }
Izhod
Izhod3. Razred optičnega bralnika za branje besedilne datoteke
Razred optičnega bralnika ponuja preprost način za branje besedilnih datotek in razčlenjevanje primitivnih tipov ali nizov z uporabo regularni izrazi . Vnos razdeli na žetone z uporabo ločila (privzeto presledek).
Primer 1: Z uporabo zank
Javaimport java.io.*; import java.util.Scanner; public class UsingScannerClass { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print('Enter the Path : '); // Reading File name String path = br.readLine(); // pass the path to the file as a parameter File file = new File(path); Scanner sc = new Scanner(file); while (sc.hasNextLine()) System.out.println(sc.nextLine()); } }
Izhod
zanka v lupinskem skriptu
IzhodPrimer 2: Brez uporabe zank
Javaimport java.io.*; import java.util.Scanner; public class ReadingEntireFileWithoutLoop { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print('Enter the Path : '); // Reading File name String path = br.readLine(); File file = new File(path); Scanner sc = new Scanner(file); // we just need to use \Z as delimiter sc.useDelimiter('\Z'); System.out.println(sc.next()); } }
Izhod
Izhod4. Branje celotne datoteke na seznamu
Celotno besedilno datoteko lahko preberemo v seznam z uporabo Files.readAllLines() metoda iz paket java.nio.file . Vsaka vrstica v datoteki postane en element na seznamu.
Sintaksa
public static List readAllLines(Path pathCharset cs)vrže IOException
Ta metoda prepozna naslednje kot zaključke vrstic:
- u000Du000A -> Povratek na začetek vrstice + Pomik vrstice
- u000A -> Premik vrstice
- u000D -> Vrnitev v začetni vrstici
import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.*; public class ReadFileIntoList { public static List<String> readFileInList(String fileName) { // Created List of String List<String> lines = Collections.emptyList(); try { lines = Files.readAllLines( Paths.get(fileName) StandardCharsets.UTF_8); } catch(IOException e) { e.printStackTrace(); } return lines; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print('Enter the Path : '); // Reading File name String path = br.readLine(); List l = readFileInList(path); // Iterator iterating over List Iterator<String> itr = l.iterator(); while (itr.hasNext()) System.out.println(itr.next()); } }
Izhod
Izhod5. Branje besedilne datoteke kot niza
V Javi lahko beremo celotno besedilno datoteko kot en niz. To je uporabno, če želite obdelati vsebino datoteke kot celoto in ne vrstico za vrstico.
Sintaksa:
1 milijon v številkah
Podatki niza = nov niz(Files.readAllBytes(Paths.get(imedatoteke)));
primer:
Javapackage io; import java.nio.file.*; public class ReadTextAsString { public static String readFileAsString(String fileName) throws Exception { String data = ''; data = new String( Files.readAllBytes(Paths.get(fileName))); return data; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print('Enter the Path : '); // Reading File name String path = br.readLine(); String data = readFileAsString(path); System.out.println(data); } }
Izhod
Izhod Ustvari kviz