import java.io.File;
import java.util.Date;
import java.util.Scanner;
public class TheFileClass01
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path: ");
String path = sc.nextLine();
// create object of file class to denote this path
File f = new File(path);
// check if path is correct
if (f.exists())
{
System.out.println("path is correct");
// check if path is of file
if (f.isFile())
{
System.out.println("it is a file");
// get size of file
long filesize = f.length();
System.out.println("size of file is " + filesize + " bytes");
// get last modification time of file
long time = f.lastModified();
// convert milliseconds into actual date and time
Date date = new Date(time);
System.out.println("file modified at " + date);
}
// check if path is of a directory
if (f.isDirectory())
{
System.out.println("it is a directory");
}
} // end of outer if block
// this else will be executed if path is not correct
else
{
System.out.println("path is not correct");
}
}
}
import java.io.File;
import java.util.Date;
import java.util.Scanner;
public class TheFileClass02
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path: ");
String path = sc.nextLine();
// create object of file class to denote this path
File f = new File(path);
// lets create a directory inside this path
if(f.mkdir())
{
System.out.println("directory created");
}
else
{
System.out.println("directory already exists");
}
}
}
import java.io.File;
import java.util.Date;
import java.util.Scanner;
public class TheFileClass03
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path: ");
String path = sc.nextLine();
// create object of file class to denote this path
File f = new File(path);
// delete a resource inside this path (in order to delete
// a directory it must be empty)
if(f.delete())
{
System.out.println("resource deleted");
}
// else will be executed if resource is not deleted
else
{
System.out.println("resource cant be deleted");
}
}
}
import java.util.Date;
import java.util.Scanner;
public class TheFileClass01
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path: ");
String path = sc.nextLine();
// create object of file class to denote this path
File f = new File(path);
// check if path is correct
if (f.exists())
{
System.out.println("path is correct");
// check if path is of file
if (f.isFile())
{
System.out.println("it is a file");
// get size of file
long filesize = f.length();
System.out.println("size of file is " + filesize + " bytes");
// get last modification time of file
long time = f.lastModified();
// convert milliseconds into actual date and time
Date date = new Date(time);
System.out.println("file modified at " + date);
}
// check if path is of a directory
if (f.isDirectory())
{
System.out.println("it is a directory");
}
} // end of outer if block
// this else will be executed if path is not correct
else
{
System.out.println("path is not correct");
}
}
}
# # # # # # # # # # # # # # # # # # # #
import java.util.Date;
import java.util.Scanner;
public class TheFileClass02
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path: ");
String path = sc.nextLine();
// create object of file class to denote this path
File f = new File(path);
// lets create a directory inside this path
if(f.mkdir())
{
System.out.println("directory created");
}
else
{
System.out.println("directory already exists");
}
}
}
# # # # # # # # # # # # # # # # # # # #
import java.util.Date;
import java.util.Scanner;
public class TheFileClass03
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path: ");
String path = sc.nextLine();
// create object of file class to denote this path
File f = new File(path);
// delete a resource inside this path (in order to delete
// a directory it must be empty)
if(f.delete())
{
System.out.println("resource deleted");
}
// else will be executed if resource is not deleted
else
{
System.out.println("resource cant be deleted");
}
}
}
# # # # # # # # # # # # # # # # # # # #
import java.io.File;
import java.util.Date;
import java.util.Scanner;
public class TheFileClass04
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path that need to be renamed: ");
String oldPath = sc.nextLine();
System.out.print("Input a new name: ");
String newPath = sc.nextLine();
// create object of file class to denote old path
File file1 = new File(oldPath);
// create object of file class to denote new path
File file2 = new File(oldPath);
// lets rename oldPath name into new one
if(file1.renameTo(file2))
{
System.out.println("renaming done");
}
// else will be executed if renaming not done
else
{
System.out.println("error in renaming");
}
}
}
# # # # # # # # # # # # # # # # # # # #
import java.io.File;
import java.util.Scanner;
public class TheFileClass05
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path: ");
String path = sc.nextLine();
// create object of file class to denote this path
File f = new File(path);
// lets get the name of resource (file or directory)
String name = f.getName();
System.out.println("name of resource is "+name);
// lets get the name of parent directory of resource
String parent = f.getParent();
System.out.println("parent is "+parent);
}
}
# # # # # # # # # # # # # # # # # # # #
import java.io.File;
import java.util.Date;
import java.util.Scanner;
public class TheFileClass06
{
public static void main(String[] args)
{
// create object of scanner class to get input from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Input a path: ");
String path = sc.nextLine();
// create object of file class to denote this path
File f = new File(path);
// lets get a list of all the resources (files and sub-directories)
// inside this path
File[] fa = f.listFiles();
// fetch data from this array
for (int i = 0; i < fa.length; i++)
{
// lets get the name of resource
String name = fa[i].getName();
System.out.print(name);
// check if resource is a file
if(fa[i].isFile())
{
System.out.print(", file");
}
// check if resource is a directory
if(fa[i].isDirectory())
{
System.out.print(", directory");
}
// check if resource is a hidden resource
if(fa[i].isHidden())
{
System.out.print(" << its a hidden resource");
}
// print a blank line
System.out.println();
} // end of loop
}
}
No comments:
Post a Comment