How can I change file attribute to read only?

This code demonstrate how we can modify file attribute to be read only. File class has a setReadOnly() method to make file read only and a canWrite() method to know whether it is writable or not.

package org.kodejava.io;

import java.io.File;

public class FileReadOnlyExample {
    public static void main(String[] args) throws Exception {
        File file = new File("ReadOnly.txt");

        // Create a file only if it doesn't exist.
        boolean created = file.createNewFile();

        // Set file attribute to read only so that it cannot be written
        boolean succeeded = file.setReadOnly();

        // We are using the canWrite() method to check whether we can
        // modified file content.
        if (file.canWrite()) {
            System.out.println("File is writable!");
        } else {
            System.out.println("File is in read only mode!");
        }
    }
}

This code snippet print the following output:

File is in read only mode!

How do I know the memory size in virtual machine?

If you want to know the free memory, and the total memory available in the Java runtime environment then you can use the following code snippet to check.

package org.kodejava.lang;

public class MemoryExample {
    public static void main(String[] args) {
        long freeMemory = Runtime.getRuntime().freeMemory();
        long totalMemory = Runtime.getRuntime().totalMemory();

        System.out.println("Free Memory  = " + freeMemory);
        System.out.println("Total Memory = " + totalMemory);
    }
}

Here is the result of the code snippet above:

Free Memory  = 1018439896
Total Memory = 1029177344

How do I ping a host?

Since Java 1.5 (Tiger) the java.net.InetAddress class introduces isReachable() method that can be used to ping or check if the address specified by the InetAddress is reachable.

package org.kodejava.net;

import java.net.InetAddress;

public class PingExample {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getByName("172.16.2.0");

            // Try to reach the specified address within the timeout
            // period. If during this period the address cannot be
            // reach then the method returns false.
            boolean reachable = address.isReachable(10000);

            System.out.println("Is host reachable? " + reachable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

How do I append data to a text file?

One of the common task related to a text file is to append or add some contents to the file. It’s really simple to do this in Java using a FileWriter class. This class has a constructor that accept a boolean parameter call append. By setting this value to true a new data will be appended at the end of the file when we write a new data to it.

Let’s see an example.

package org.kodejava.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class AppendFileExample {
    public static void main(String[] args) {
        File file = new File("user.txt");

        try (FileWriter writer = new FileWriter(file, true)) {
            writer.write("username=kodejava;password=secret" + System.lineSeparator());
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

What’s needed to be prepared for learning Java programming?

At the time you decided to start to learn Java Programming you can start by downloading the Java Development Kit (JDK) from Java Download website. There are three different types of JDK, the Java SE (Java Standard Edition), Java EE (Java Enterprise Edition), Java ME (Java Mobile Edition).

From the website you can also download the Java API documentations which will sure be your first companion when learning the language. It is better also to download the Java Tutorial Series that was written by the Java experts.

In the tutorial you can learn from the basic of Java programming, introduction of the fundamental of Object-Oriented Programming (OOP) which is Java all about. Next you can also find trails in each subject of the API (Application Programming Interface) provided by Java library, such as the core package, how to communicate with a database, Java GUI programming, image manipulation, RMI, Java Beans Framework, etc.

When you want to write a code, you might wonder what editor or IDE you will need to use to start learning. A good text editor that supports a coloring will be a good candidate, colorful screen is better than just a black and white, isn’t it?

There are a lot of good text editor available today such as the VIM, NotePad++, TextPad, Editplus, UltraEdit. If you already have your preferred editor, you can use it of course.

If you’re ready for the big stuff, a bigger homework project, you might consider using an IDE (Integrated Development Environment) as you’ll be working with lots of Java classes, configuration files and build script for examples. There are many great IDE on the Java world from the free to the commercial product.

What IDE to use is really a developer decision, use whatever tools that can help you to improve your learning and programming activities. You can find IDE such as NetBeans, Eclipse, IntelliJ IDEA, etc.

Beside learning from the Java tutorials there are also many forums on the internet where you can discuss your doubts or your problems. Forums like JavaRanch, Stack Overflow are great forums with Java gurus that can help you to clarify your doubts and help you to solve your problem. Remember one thing when you ask for help, be polite, elaborate your problem clearly.

Good Java books on your desktop are good resources to study Java, from good books you can learn the nuts and bolts of the Java programming languages. When you have all your arsenal you can get the best out of you in learning Java. Have fun!