Friday, October 31, 2014

Java Directory Pro



Create new empty file

import java.io.*;  
public class CreateNewEmptyFile
{
public static void main(String[] args)
{
File file = new File("C://demo.txt");
boolean blnCreated = false;
try
{
blnCreated = file.createNewFile();
}
catch(IOException ioe)
{
System.out.println("Error while creating a new empty file :" + ioe);
}
System.out.println("Was file " + file.getPath() + " created ? : " + blnCreated);
   }
}

Create temporary file

import java.io.*;
public class CreateTemporaryFile
{
public static void main(String[] args)
{
File file1 = null;
File file2 = null;
try
{
file1 = File.createTempFile("JavaTemp", null);  file2 = File.createTempFile("JavaTemp", ".javatemp");
}
catch(IOException ioe)
{
System.out.println("Exception creating temporary file : " + ioe);
}
System.out.println("Temporary file without suffix extension: "
+ file1.getPath());
System.out.println("Temporary file with suffix extension: "
+ file2.getPath());
   }
}

Create temporary file in specified directory

import java.io.*;
public class CreateTempFileDirectory
{
public static void main(String[] args)
{
File file = null;
File dir = new File("C://FileIO");
try
{
file = File.createTempFile("JavaTemp", ".javatemp", dir);
}
catch(IOException ioe)
{
System.out.println("Exception creating temporary file : " + ioe);
}
System.out.println("Temporary file created at : " + file.getPath());
   }
}

Delete file or directory

import java.io.*;
public class DeleteFileOrDirectory
{
public static void main(String[] args)
{
File file = new File("C://FileIO/DeleteDemo.txt");
boolean blnDeleted = file.delete();
System.out.println("Was file deleted ? : " + blnDeleted);
   }
}

No comments:

Post a Comment