Friday, October 31, 2014

File directory in java



Delete file or directory when virtual machine terminates

import java.io.*;
public class DeleteFileWhenVMTerminates
{
public static void main(String[] args)
{
File file = new File("C://FileIO//DeleteDemo.txt");
file.deleteOnExit();
   }
} 

Determine File or Directory

import java.io.*;
public class DetermineDirOrFile
{
public static void main(String[] args)
{
File file = new File("C://FileIO");
boolean isFile = file.isFile();
if(isFile)
System.out.println(file.getPath() + " is a file.");
else
System.out.println(file.getPath() + " is not a file.");
boolean isDirectory = file.isDirectory();
if(isDirectory)
System.out.println(file.getPath() + " is a directory.");
else
System.out.println(file.getPath() + " is not a directory.");
      }
}

Determine if File or Directory is hidden

import java.io.*;
public class DetermineHiddenFileOrDirectory
{
public static void main(String[] args)
{
File file = new File("C://FileIO//HiddenDemo.txt");
boolean blnHidden = file.isHidden();
System.out.println("Is " + file.getPath() + " hidden ?: " + blnHidden);
    }
}

Determine if a file can be read

import java.io.*;
public class DetermineReadFile
{
 public static void main(String[] args)
{
String filePath = "C:/FileIO/ReadText.txt";
File file = new File(filePath);
if(file.canRead())
{
System.out.println("File " + file.getPath() +" can be read");
}
else
{
System.out.println("File " + file.getPath() +" can not be read");
     }
   }
}

Determine if a file can be written

 import java.io.*;
 public class DetermineWriteFile
{
public static void main(String[] args)
{
String filePath = "C:/FileIO/ReadText.txt";
File file = new File(filePath);
if(file.canWrite())
{
System.out.println("File " + file.getPath() +" can be written");
}
else
{
System.out.println("File " + file.getPath() +" can not be written");
     }
  }
}

No comments:

Post a Comment