About 8,270,000 results
Open links in new tab
  1. Reading a .txt file using Scanner class in Java - Stack Overflow

    you must use a file name by it's extension in File object constructor, as an example: File myFile = new File("test.txt"); but there is a better way to use it inside Scanner object by pass the filename absolute address, as an example: Scanner sc = new Scanner(Paths.get("test.txt")); in this way you must import java.nio.file.Paths as well.

  2. Java Read Files - W3Schools

    In the following example, we use the Scanner class to read the contents of the text file we created in the previous chapter: myReader.close(); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } } } Files in …

  3. How to read file in Java using Scanner Example - text files

    From Java 5 onwards java.util.Scanner class can be used to read file in Java. Earlier we have seen examples of reading file in Java using FileInputStream and reading file line by line using BufferedInputStream and in this Java tutorial, we will See How …

  4. java.util.scanner - how to read a text file using scanner in Java ...

    Feb 15, 2013 · // Prepare to read from the file, using a Scanner object. File file = new File(filename); Scanner in = new Scanner(file); ArrayList<DogShop> shops = new ArrayList<DogShop>(); // Read each line until end of file is reached. while (in.hasNextLine()) // Read an entire line, which contains all the details for 1 account. String line = in.nextLine();

  5. Different ways of Reading a text file in Java - GeeksforGeeks

    Jan 4, 2025 · There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.

  6. Read Contents of a File Using Scanner Class - Online Tutorials …

    Aug 1, 2019 · Learn how to read the contents of a file in Java using the Scanner class with this detailed tutorial.

  7. java.util.scanner - Java read file using Scanner - Stack Overflow

    Here are two ways to read a file with the Scanner class - using default encoding and an explicit encoding. This is part of a long guide of how to read files in Java. Scanner – Default Encoding. public static void main(String [] pArgs) throws FileNotFoundException { String fileName = "c:\\temp\\sample-10KB.txt"; File file = new File(fileName);

  8. Java Scanner - Baeldung

    Jan 5, 2024 · In this tutorial, we went over multiple real-world examples of using the Java Scanner. We learned how to read input from a file, console, or String using Scanner. We also learned how to find and skip a pattern using Scanner and how to change the Scanner delimiter. Finally, we explained how to handle the NoSuchElementException exception.

  9. How to Read a File in Java - Baeldung

    Jan 8, 2024 · In this tutorial, we’ll explore different ways to read from a File in Java. First, we’ll learn how to load a file from the classpath, a URL, or from a JAR file using standard Java classes. Second, we’ll see how to read the content with BufferedReader, Scanner, StreamTokenizer, DataInputStream, SequenceInputStream, and FileChannel.

  10. Read contents of a file using Scanner in Java | Techie Delight

    May 1, 2021 · This post will discuss how to read the contents of a file using Scanner class in Java. Scanner is a utility class in java.util package which can parse primitive types and strings using regular expressions. It can read text from any …

  11. Some results have been removed
Refresh