thenewboston

Java Programming Tutorial - 81 - Reading from Files

Aug 22nd, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3.  
  4. class ReadFile {
  5.    public static void main(String args[ ]) {
  6.       Engine f = new Engine();
  7.       f.openFile();
  8.       f.printFile();
  9.       f.closeFile();
  10.    }
  11. }
  12.  
  13. class Engine {
  14.    private Scanner f;
  15.    private String name, surname, age;
  16.    
  17.    void openFile() {
  18.       try {
  19.          f = new Scanner(new File("U1.txt"));
  20.       } catch(Exception e) {
  21.          System.out.println("File doesn't exists");  
  22.       }
  23.    }
  24.    
  25.    void printFile() {
  26.       while(f.hasNext()) {
  27.          name = f.next();
  28.          surname = f.next();
  29.          age = f.next();
  30.          
  31.          System.out.printf("%s %s %s\n", name, surname, age);
  32.       }
  33.    }
  34.    
  35.    void closeFile() {
  36.       f.close();  
  37.    }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment