Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. package com.tutorialspoint;
  2.  
  3. import java.io.File;
  4.  
  5. public class FileDemo {
  6.    public static void main(String[] args) {      
  7.       File f = null;
  8.       boolean bool = false;
  9.      
  10.       try {
  11.          // create new File object
  12.          f = new File("C:/test.txt");
  13.          
  14.          // returns true if file exists
  15.          bool = f.exists();
  16.          
  17.          // if file exists
  18.          if(bool) {
  19.          
  20.             // set file access to writable for everybody
  21.             bool = f.setWritable(true, false);
  22.            
  23.             // print
  24.             System.out.println("setWritable() succeeded?: "+bool);
  25.            
  26.             // checks whether the file is writable
  27.             bool  = f.canWrite();
  28.            
  29.             // prints
  30.             System.out.print("Is file writable?: "+bool);
  31.          }
  32.          
  33.       } catch(Exception e) {
  34.          // if any error occurs
  35.          e.printStackTrace();
  36.       }
  37.    }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement