Determine if file
or directory exists
import java.io.*;
public class DetermineIfFileExists
{
public static void main(String[] args)
{
File file = new
File("C://FileIO/ExistsDemo.txt");
boolean blnExists =
file.exists();
System.out.println("Does
file " + file.getPath() + " exist ?: " + blnExists);
}
}
Get Absolute path
of the file
import java.io.*;
public class
GetAbsoluteFilePath
{
public static void
main(String[] args)
{
String filePath =
File.separator + "JavaExamples" + File.separator + "IO";
File file = new File(filePath);
System.out.println("Abstract
file path is :" + file.getPath());
System.out.println("Absolute
file path is : " + file.getAbsolutePath());
}
}
Get File size in
bytes
import java.io.*;
public class
GetFileSizeInByetes
{
public static void
main(String[] args)
{
File file = new File("C://FileIO/demo.txt");
long fileSize =
file.length();
System.out.println("File
size in bytes is: " + fileSize);
System.out.println("File
size in KB is : " + (double)fileSize/1024);
System.out.println("File
size in MB is :" + (double)fileSize/(1024*1024));
}
}
Get Last
modification time of a file or directory
import java.io.*;
import java.util.Date;
public class
GetLastModificationTimeOfAFile
{
public static void
main(String[] args)
{
File file = new
File("C://FileIO//demo.txt");
long lastModified =
file.lastModified();
System.out.println("File
was last modifed at : " + new Date(lastModified));
}
}
Get name of
parent directory
import java.io.*;
public class
GetParentDirectory
{
public static void
main(String[] args)
{
File file = new File("C://FileIO/demo.txt");
String
strParentDirectory = file.getParent();
System.out.println("Parent
directory is : " + strParentDirectory);
}
}
Get name of
specified file or directory
import java.io.*;
public class
GetNameOfFileOrDirectory
{
public static void
main(String[] args)
{
File file = new
File("C://FileIO//FileDemo.txt");
String strFileName =
file.getName();
System.out.println("File
name is : " + strFileName);
}
}
No comments:
Post a Comment