Advertisement
makispaiktis

17. Read data from an existing file

May 31st, 2022 (edited)
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. import javax.crypto.SecretKey;
  2. import java.io.*;
  3. import java.lang.*;
  4. import java.util.*;
  5.  
  6. public class CreateFile {
  7.  
  8.     // Variables
  9.     private Scanner x;
  10.  
  11.     // Methods
  12.     public void openFile(){
  13.         try{
  14.             x = new Scanner(new File("Students.txt"));
  15.         }
  16.         catch (Exception e){
  17.             System.out.println("You have got an error");
  18.         }
  19.     }
  20.  
  21.     public void readFile(){
  22.         while(x.hasNext()){
  23.             String name = x.next();
  24.             String surname = x.next();
  25.             String age = x.next();
  26.             System.out.printf("%s %s %s \n", name, surname, age);
  27.         }
  28.     }
  29.  
  30.  
  31.     public void closeFile(){
  32.         x.close();
  33.     }
  34.  
  35.     // Main Function
  36.     public static void main(String[] args){
  37.         CreateFile f = new CreateFile();
  38.         f.openFile();
  39.         f.readFile();
  40.         f.closeFile();
  41.     }
  42.  
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement