Java create and write to a file
In Java, we can use Files.write to create and write to a file. The Files.write also accepts an Iterable interface; it means this API can write a List to a…
How to write to file in Java – BufferedWriter
In Java, we can use BufferedWriter to write content into a file, safety with try-resources in Java 7+. If possible, uses Files.write instead, one line, simple and nice. For Append…
How to read file in Java – FileInputStream
In Java, we use FileInputStream to read bytes from a file, such as an image file or binary file. The FileInputStream.read() reads a byte at a time, and it will…
How to write to file in Java – FileOutputStream
In Java, FileOutputStream is a bytes stream class that’s used to handle raw binary data. To write the data to file, you have to convert the data into bytes and…
How to read file in Java – BufferedInputStream
Here is another example to show how to read a file in Java with BufferedInputStream and DataInputStream classes. The readLine() from the type DataInputStream is deprecated. Sun officially announced this…
How to set the file permission in Java
In Java, file permissions are very OS specific: *nix , NTFS (windows) and FAT/FAT32, all have different kind of file permissions. Java comes with some generic file permission to deal…
Java : getResourceAsStream in static method
In non static method, we use getClass() of the object or instance. In static method, the getClass() method will be failed, and prompts Cannot make a static reference to the…
How to get the environment variables in Java
In Java, the System.getenv() returns an unmodifiable string Map view of the current system environment. If a requested environment variable is not defined, the System.getenv will return a null, and…
Java Properties file examples
Normally, Java properties file is used to store project configuration data or settings. In this tutorial, we will show you how to read and write to/from a .properties file. The…
How to sort a Map in Java
Few Java examples to sort a Map by its keys or values: (1) Uses java.util.TreeMap, it will sort the Map by keys automatically, (2) Yet another java.util.TreeMap example, provide a…