“Java 11 Baca baris file demi baris” Kode Jawaban

Java Baca setiap baris dalam file

        BufferedReader br = null;
        try {
            File file = new File("myfile.txt"); // java.io.File
            FileReader fr = new FileReader(file); // java.io.FileReader
            br = new BufferedReader(fr); // java.io.BufferedReader
            String line;
            while ((line = br.readLine()) != null) {
              // process the line
            }
          }
          catch(IOException e) { e.printStackTrace();}
          finally
          {
              try { if (br != null) br.close(); }
              catch(IOException e) { e.printStackTrace(); }
          }
Zealous Zebra

Java membaca baris dari file

        Scanner sc = null;
        try {
            File file = new File("myfile.txt"); // java.io.File
            sc = new Scanner(file);     // java.util.Scanner
            String line;
            while (sc.hasNextLine()) {
              line = sc.nextLine();
              // process the line
            }
          }
          catch(FileNotFoundException e)
          {
              e.printStackTrace();
          }
          finally {
            if (sc != null) sc.close();
          }
Zealous Zebra

Java 11 Baca baris file demi baris

try(Scanner scanner = new Scanner(new File("myFile.txt"))){
	while(scanner.hasNext()){
      String line = scanner.nextLine();
      // process the line
    }
}
Carey Foushee

Jawaban yang mirip dengan “Java 11 Baca baris file demi baris”

Pertanyaan yang mirip dengan “Java 11 Baca baris file demi baris”

Lebih banyak jawaban terkait untuk “Java 11 Baca baris file demi baris” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya