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 Scanner class
import java.io.*;
import java.util.*;
public class ScannerDemo
{
public static void main (String[] args) throws IOException
{
String line="";
String word="";
Scanner reader;
// convert txt file into a File object first
File fileObj = new File("scannerdemo.txt");
Scanner freader = new Scanner(fileObj);
//instantiate write object for output
PrintWriter writerobject = new PrintWriter(new FileWriter("moo.txt")); // foo.txt is output file
while (freader.hasNextLine()) // checks to see if there is a line, if true, loop
{
line = freader.nextLine(); // get entire line
reader = new Scanner(line);
word = reader.next();
writerobject.println(word); // write to moo.txt
}
freader.close(); // MUST close this object
writerobject.close(); // MUST close this object
}
}