Advertisement
Jakzon123

java print file contents

Nov 13th, 2022 (edited)
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class testRead {
  5.     public static void main(String[] args) throws Exception {
  6.         try {
  7.             File f = new File("file.txt");
  8.             // if the file passed exists and is a file
  9.             if(f.isFile()){  
  10.                 Scanner s = new Scanner(f);
  11.                 while(s.hasNextLine())
  12.                     // outputs lines until eof
  13.                     System.out.println(s.nextLine());
  14.                 s.close();
  15.             }
  16.         }
  17.         catch (Exception e) {
  18.             System.exit(0);
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement