Java – Add new line in String
Different operating system has a different new line or line separator string: UNIX, Linux or Mac OSX = \n, Windows = \r\n . We can also use the Java 1.7…
Java 7 try-with-resources example
Before Java 7, we can close a resource with finally. In Java 7, a new try-with-resources approach is introduced, it helps to close resources automatically.
Java – How to read input from the console
In Java, there are three ways to read input from a console : 1 - System.console (JDK 1.6), 2 - Scanner (JDK 1.5), 3 - BufferedReader + InputStreamReader (Classic). Since…
How to get the standard input in Java
A quick example to show you how to read the standard input in Java.
How to check if directory is empty in Java
Here’s an example to check if a directory is empty.
How to traverse a directory structure in Java
In this example, the program will traverse the given directory and print out all the directories and files absolute path and name one by one.
How to delete directory in Java
If we use the NIO Files.delete to delete a non-empty directory in Java, it throws DirectoryNotEmptyException; for legacy IO File.delete to delete a non-empty directory, it returns a false. The…
How to create directory in Java
In Java, we can use the NIO Files.createDirectory to create a directory or Files.createDirectories to create a directory including all nonexistent parent directories. For legacy IO java.io.File, the similar methods…
Java – Unable to assign group write permission to a file
In Java, we can use the NIO createFile() to assign file permission during file creation. But, the createFile() fails to assign the group writes file permission to a file on…