1 d array:: reading from files


Reading from a text file using the BufferedReader class

import java.io.*; public class ArrayFile { public static void main (String[] args) throws IOException { BufferedReader fileObj = new BufferedReader (new FileReader ("scores.txt")); int score[] = new int [9]; for (int i = 0 ; i < 9 ; i++) { score [i] = Integer.parseInt (fileObj.readLine ());//read from scores.txt System.out.println (score [i]); } } // main method } // ArrayFile class

Reading from a text file using the File class

import java.io.*; import java.util.Scanner; import java.io.File; public class ScannerReadFile { public static void main (String[] args) throws IOException { String line, boo; File file = new File("scores.txt");//instantiate file object //instantiate scanner object using file object as arg Scanner inFile = new Scanner(file); while (inFile.hasNextLine()) { line = inFile.nextLine();//get entire line } while (inFile.hasNext()) { boo = inFile.next();//get next token } inFile.close(); } // main method } // ArrayFile class