Java Programming Tutorial - 81 - Reading from Files
Why Stack Overflow recommends it
Stack Overflow users link it as a starting point for reading files with Scanner, both in answers to newcomers and in questions by people who followed it and hit errors.
Recommended by 1 people across 1 different months, 2014 to 2020.
What it is
A short beginner Java tutorial from thenewboston's long-running Java series that shows how to read text files using the Scanner and File classes. It walks through opening a file, looping over its contents, and closing the Scanner.
What people said
Recommends vedi0boy · 2014-07 · Stack Overflow · on “Identifying each word in a file”
I used this great tutorial from thenewboston when I started off reading files: [this video] This video seems perfect for you. It covers how to save file words of data. And just add the string data to the ArrayList. Here's what your code should look like: import java.io.*; import java.util.*; public class ReadFile { static Scanner x; static ArrayList<String> temp = new ArrayList<String>(); public static void main(String args[]){ openFile(); readFile(); closeFile(); } public static void openFile(){ try( x = new Scanner(new File("yourtextfile.txt"); }catch(Exception e){ System.out.println(e); } } public static void readFile(){ while(x.hasNext()){ temp.add(x.next()); } } public void closeFile(){ x.cl
Cites Tacolips · 2015-10 · Stack Overflow · on “NullPointerException - reading not empty file returns Null”
I'm fairly new to programming and I wanted to learn how to read data from a file. I watched this video and tried to copy the idea for my own purpose. [this video] Whenever I try to run my main it says Error Line 22 - NullPointerException. I have a text file with numbers and I have checked if the names match. I simply do not understand why it says my file is empty. package uge4; import java.util.*; import java.io.*; public class ReadFile { private Scanner x; public void openFile () { try { x = new Scanner (new File("gradeconverter.txt")); } catch(FileNotFoundException e){ System.out.println("File not found."); } } public void readFile() { Scanner input = new Scanner(System.in); System.out.print("I
Cites grzegorzs · 2017-11 · Stack Overflow · on “Opening a file FileNotFoundException with scanner in Java, Eclipse, Ubuntu”
I'm trying to write a parser to a file called "x". I want to use scanner. I tryied to follow actions from a tutorial: [this video] . package q; import java.io.File; import java.util.Scanner; public class Parser { public static void main(String [] args) { Scanner x = new Scanner(new File("/home/x/eclipse-workspace/q/src/q/x.txt")); String s=x.nextLine(); System.out.print(s); } } The file that I want to open is called "x", its text file. We can see it in Package Explorer on left side. I clicked right on its properties. There is visible file locatization. There appears F ileNotFoundException as on the picture. I doesn't understand why this file cannot be opened. [LINK] [LINK] [update] But I'm not su
Cites JustLearnin · 2020-01 · Stack Overflow · on “Exception in thread "main" java.util.NoSuchElementException ReadingFiles”
Well i have 12 items in the File, and I am following a tutorial link [this video] so i don't know if thats the case. Maybe the tutorial is outdated or something. Can you recommend a good tutorial on writing to Files and reading in from Files. thanks