java.io

 

Create a StringWriter object



The default constructor allows us to create a StringWriter with default string buffer size.
  1. import java.io.StringWriter;  
  2.   
  3. /** 
  4.  * StringWriter class examples. 
  5.  */  
  6. public class StringWriterExample {  
  7.     public static void main(String[] args){  
  8.         // Create a stringWriter object.  
  9.         StringWriter writer = new StringWriter();  
  10.     }  
  11. }  
  12. Compare two files using java
    The compareTo method of File class helps us to create a temporary file. This method returns an integer. If the value of the integer is greater than zero, the file will be greater than the argument, if the file is less than zero, the file will be less than the argument and a value of zero means both the files are equal.
    1. import java.io.File;  
    2. /** 
    3.  * File class examples. 
    4.  * 
    5.  */  
    6. public class FileExample  {  
    7.     public static void main(String[] args) {  
    8.         // Create file object representing the source file/directory  
    9.         File file = new File("d:\\temp\\test.txt");  
    10.   
    11.         // Create another file for comparison.  
    12.         File cFile = new File("d:\\temp\\Copy of test.txtt");  
    13.   
    14.         // Compare two files.  
    15.         int value = file.compareTo(cFile);  
    16.   
    17.         System.out.println("File comparison result :  "+value);  
    18.     }  
    19. }  

    Create a temp file using java



    Create a temp file using java
    The createTempFile method of File class helps us to create a temporary file.
    1. import java.io.File;  
    2. import java.io.IOException;  
    3. /** 
    4.  * File class examples. 
    5.  * 
    6.  */  
    7. public class FileExample  {  
    8.     public static void main(String[] args) throws IOException{  
    9.         // Create file object representing the source file/directory  
    10.         File file = new File("d:\\temp");  
    11.   
    12.         // Create the temporary file.  
    13.         File tempFile = File.createTempFile("test"".tmp",file);  
    14.     }  
    15. }  

    Finds out total used space in a partition using java



    Finds out total used space in a partition using java
    In Java we can find the total used space of any drive or partition using getTotalSpace and getFreeSpace methods.
    1. import java.io.File;  
    2. /** 
    3.  * File class examples. 
    4.  * 
    5.  */  
    6. public class FileExample  {  
    7.     public static void main(String[] args){  
    8.         // Create file object representing the source file/directory  
    9.         File file = new File("d:\\temp\\test.txt");  
    10.   
    11.         // Get the total space available in current drive  
    12.         long totalSpace = file.getTotalSpace();  
    13.   
    14.         // Get the total free space available in current partition  
    15.         long freeSpace = file.getFreeSpace();  
    16.   
    17.         // Finds the total used space  
    18.         long value = totalSpace - freeSpace;  
    19.   
    20.         // Convert it to GigaBytes  
    21.         double valueGB = (double) value / 1024 / 1024 / 1024;  
    22.   
    23.         System.out.println("Total used space in the current partition is "+valueGB);  
    24.     }  
    25. }  

    Finds out total usable space available in a partition using java



    Finds out total usable space available in a partition using java
    The getUsableSpace method of File class can be used for getting the total usable space available in a partition or drive using java.
    1. import java.io.File;  
    2. /** 
    3.  * File class examples. 
    4.  * 
    5.  */  
    6. public class FileExample  {  
    7.     public static void main(String[] args){  
    8.         // Create file object representing the source file/directory  
    9.         File file = new File("d:\\temp\\test.txt");  
    10.   
    11.         // Get the total usable space available in current drive  
    12.         long value = file.getUsableSpace();  
    13.   
    14.         // Convert it to GigaBytes  
    15.         double valueGB = (double) value / 1024 / 1024 / 1024;  
    16.   
    17.         System.out.println("Total free space available in the current partition is "+valueGB);  
    18.     }  
    19. }  

    Finds out total free space available in a partition using java



    Finds out total free space available in a partition using java
    The getFreeSpace method of File class can be used for getting the total free space available in a partition or drive using java.
    1. import java.io.File;  
    2. /** 
    3.  * File class examples. 
    4.  * 
    5.  */  
    6. public class FileExample  {  
    7.     public static void main(String[] args){  
    8.         // Create file object representing the source file/directory  
    9.         File file = new File("d:\\temp\\test.txt");  
    10.   
    11.         // Get the total free space available in current drive  
    12.         long value = file.getFreeSpace();  
    13.   
    14.         // Convert it to GigaBytes  
    15.         double valueGB = (double) value / 1024 / 1024 / 1024;  
    16.   
    17.         System.out.println("Total free space available in the current partition is "+valueGB);  
    18.     }  
    19. }  

    Finds out total space available in a partition using java



    Finds out total space available in a partition using java
    The getTotalSpace method of File class can be used for getting the total space available in a partition or drive using java.
    1. import java.io.File;  
    2. /** 
    3.  * File class examples. 
    4.  * 
    5.  */  
    6. public class FileExample  {  
    7.     public static void main(String[] args){  
    8.         // Create file object representing the source file/directory  
    9.         File file = new File("d:\\temp\\test.txt");  
    10.   
    11.         // Get the total space available in current drive  
    12.         long value = file.getTotalSpace();  
    13.   
    14.         // Convert it to GigaBytes  
    15.         double valueGB = (double) value / 1024 / 1024 / 1024;  
    16.   
    17.         System.out.println("Total space available in the current partition is "+valueGB);  
    18.     }  
    19. }  

    Lists all the drives present in Windows using java



    Lists all the drives present in Windows using java
    The listRoots method of File class is used for finding out all the drives available in Windows.
    1. import java.io.File;  
    2. /** 
    3.  * File class examples. 
    4.  * 
    5.  */  
    6. public class FileExample  {  
    7.     public static void main(String[] args){  
    8.         // Get all the drives  
    9.         File drives[] = File.listRoots();  
    10.   
    11.         // Loop through the drive list and display the drives.  
    12.         for (int index = 0; index < drives.length; index++) {  
    13.             System.out.println(drives[index]);  
    14.         }  
    15.     }  
    16. }  
    Note: Since Linux does not have any drives concept, the output of the above program in Linux operating system will be "/"

    Checks whether a file or directory has executable permission or not.



    Checks whether a file or directory has executable permission or not.
    The canExecute method of File class can be used for finding out whether the given file has executable permission or not.
    1. import java.io.File;  
    2. /** 
    3.  * File class examples. 
    4.  * 
    5.  */  
    6. public class FileExample  {  
    7.     public static void main(String[] args){  
    8.         // Create file object representing the source file/directory  
    9.         File file = new File("d:\\temp");  
    10.   
    11.         // Check whether the file has executable permission or not.  
    12.         boolean value = file.canExecute();  
    13.   
    14.         System.out.println("File executable permission status : "+value);  
    15.     }  
    16. }  

    Change file or directory permission only to owner using java



    Change file or directory permission only to owner using java
    The setReadOnly, setWritable,setReadable and setExecutable methods of the file class is used for changing the permissions on a file only to owner of the file or directory.
    1. import java.io.File;  
    2. /** 
    3.  * File class examples. 
    4.  * 
    5.  * The class sets read only, writable, executable and readable permissions 
    6.  * for a file or directory. This permissions will be applied to the owner of 
    7.  * the file or directory only. Not everyone will get the permissions. 
    8.  */  
    9. public class FileExample  {  
    10.     public static void main(String[] args){  
    11.         // Create file object representing the source file/directory  
    12.         File file = new File("d:\\temp\\test.txt");  
    13.   
    14.         // Given below are the different file permissions  
    15.         // operations that can be performed on java.  
    16.   
    17.         // Give executable permission to a file or directory  
    18.         file.setExecutable(truetrue);  
    19.   
    20.         // Set read only property to a file or a directory  
    21.         file.setReadOnly();  
    22.   
    23.         // Give write permission to the file  
    24.         file.setWritable(truetrue);  
    25.   
    26.         // Make the file or directory as readable  
    27.         file.setReadable(truetrue);  
    28.     }  
    29. }  



     

No comments:

Post a Comment