|
| 1 | +import java.io.File; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class JavaFileReadExample { |
| 5 | + public static void main(String args[]){ |
| 6 | + System.out.println("Read File Example : "); |
| 7 | + |
| 8 | + File file=new File("temp_file.txt"); |
| 9 | + Scanner scannerread=new Scanner(System.in); |
| 10 | + try { |
| 11 | + Scanner scanner = new Scanner(file); |
| 12 | + while (scanner.hasNextLine()){ |
| 13 | + String data=scanner.nextLine(); |
| 14 | + System.out.println(data); |
| 15 | + } |
| 16 | + |
| 17 | + System.out.println("File Information : "); |
| 18 | + System.out.println("Name : "+file.getName()); |
| 19 | + System.out.println("Path : "+file.getAbsolutePath()); |
| 20 | + System.out.println("Parent : "+file.getParent()); |
| 21 | + System.out.println("Writeable : "+file.canWrite()); |
| 22 | + System.out.println("Readable : "+file.canRead()); |
| 23 | + System.out.println("Executeable : "+file.canExecute()); |
| 24 | + System.out.println("Size : "+file.length()); |
| 25 | + System.out.println(file); |
| 26 | + } |
| 27 | + catch (Exception e){ |
| 28 | + System.out.println("Something Wrong on Reading File"); |
| 29 | + } |
| 30 | + |
| 31 | + System.out.println("Do You Want to Delete File ? Yes / No"); |
| 32 | + String input=scannerread.nextLine(); |
| 33 | + |
| 34 | + if(input.equalsIgnoreCase("yes")){ |
| 35 | + if(file.delete()){ |
| 36 | + System.out.println("File Deleted"); |
| 37 | + } |
| 38 | + else{ |
| 39 | + System.out.println("Error in Deleting File"); |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + else{ |
| 44 | + System.out.println("File Not Deleted"); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + } |
| 49 | +} |
0 commit comments