Answers for "how to read current line of a file java"

1

java read lines from 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();
          }
Posted by: Guest on March-12-2020
0

java read first line of file

// this is called "try-with-resources" statement in java 8.
// it ensures that the resources are closed at the end.
  
try (BufferedReader br = new BufferedReader(new FileReader(textFile))) {
    String text = br.readLine(); // first line only
    System.out.println(text);
}
Posted by: Guest on September-15-2021

Code answers related to "how to read current line of a file java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language