tampurus

55 Printing data from file by user input

May 29th, 2022 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static void main(String[] args)
  8.     {
  9.         Scanner fn = new Scanner(System.in);
  10.         System.out.println("Enter the file name");
  11.         String name = fn.next();
  12.         try {
  13.             File Obj = new File(name);
  14.             Scanner Reader = new Scanner(Obj);
  15.             while (Reader.hasNextLine()) {
  16.                 String data = Reader.nextLine();
  17.                 System.out.println(data);
  18.             }
  19.             Reader.close();
  20.         }
  21.         catch (FileNotFoundException e) {
  22.             System.out.println("An error has occurred.");
  23.             e.printStackTrace();
  24.         }
  25.     }
  26. }
  27.  
  28. /*
  29. Enter the file name
  30. user.txt
  31.  
  32. id : 1
  33. Name : Suresh
  34. Address : 12 Rajesh Nagar MP
  35. phone no : 123
  36. */
Add Comment
Please, Sign In to add comment