Apache FileUtils

To read a file as String, readFileToString(File file) method is used, below is an example

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class FileUtilsSample {

    @SuppressWarnings("CallToThreadDumpStack")
    public static void main(String[] args) {
        // Creating a file object 
        File file = new File("F:\\MyProjects\\sample.txt");
        try {
            // Reading the content of sample.txt as string
            String content = FileUtils.readFileToString(file);
            System.out.println(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now reading the a file line by line

try {
            // Reading the content of sample.txt line by line
            List<String> contents = FileUtils.readLines(file);
            
            //Total numbers of line present in sample.txt
            System.out.println("Lines = "+contents.size());

            // Iterating to print each line
            for (String line : contents) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
}

Now to write something into file we will use the method writeStringToFile(File file, String data), below is the example

public class FileUtilsSample {

    @SuppressWarnings("CallToThreadDumpStack")
    public static void main(String[] args) {
        try {
            // Creating a file object 
            File file = new File("F:\\MyProjects\\sample.txt");

            String data = "Debraj Mallick";

            // To write into the file, we have to pass
            // two parameters i.e. the file and the data
            // to be written (Previous content will be lost)
            FileUtils.writeStringToFile(file, data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now to get the size of directory

long size = FileUtils.sizeOfDirectory(new File("C:/Windows"));
System.out.println("Size is : " + size + " bytes");
System.out.println("Size is : " + size / (1024 * 1024) + " mb");
Advertisements

Java System Class

System class cannot be instantiated. Here are some example how to retrieve the system property using keys:

public class PropertiesExample {

    public static void main(String[] args) {

        System.out.println("Version of java : " + System.getProperty("java.version"));
        System.out.println("Vendor : " + System.getProperty("java.vendor"));
        System.out.println("User Name : " + System.getProperty("user.name"));
        
    }
}

Debraj Malllick output

output


To retrieve all the keys use the following code:

Enumeration<String> prop = (Enumeration<String>) System.getProperties().propertyNames();
while(prop.hasMoreElements()){
            //To get all the keys
            System.out.println("Key = "+prop.nextElement().toString());
}

To get all the values of the keys, use the above code with a small change:

Enumeration<String> prop = (Enumeration<String>) System.getProperties().propertyNames();
while(prop.hasMoreElements()){
   System.out.println("Value = "+System.getProperty(prop.nextElement().toString()));
}

Now if you like to know how many processors your machine have, copy and paste the following code:

System.out.println("Number of Available processors = " + Runtime.getRuntime().availableProcessors());

Java Heap Memory

There are more than 4.5 billion JVM-enabled devices at present.

Java heap memory is required by JVM to create new object and if the objects which are created by JVM exceeds the heap size then memoryOutof Bound  exception will be thrown, Here are some codes to check the availble heap memory.

public class HeapMemory {

public static void main(String[] args) {

System.out.println("JVM Heap Size : " + Runtime.getRuntime().totalMemory()/ (1024 * 1024));

System.out.println("Memory Available (Maximum) in Mb : " + Runtime.getRuntime().maxMemory() / (1024 * 1024));

System.out.println("Free Memory Available in Mb : " + Runtime.getRuntime().freeMemory() / (1024 * 1024));

System.out.println("Total Memory Used in Mb : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024));
}
}