3 ways to Create a New File in Java

The first task in the java i/o is to create a new file. Creating a new file is an easy task and it can be done in 3 ways. The three ways are
1. using java.io.File class
2. using java.io.FileOutputStream class
3. using java.nio.file.Files class

1. Create a file in java using java.io.File class

We will see how to create a file in java using createNewFile() method. If a file does not exist, then this method will create a file and returns true. If the file does not exist then it returns false.

import java.io.*;
import java.util.*;

public class CreateFileUsingFileClass

System.out.println in Java and How it works

System.out.println() is the most used statement in the java language.In this java tutorial we will learn what is System.out.println() and how it works. It is one of the most executed statement in the history of java. We shortly call it SOP. What is System.out.println() this question has been asked many times in java interviews.Let's find out below:

What is System.out.println

In simple words, System.out.println is a java statement that prints the argument passed, into the System.out which is generally stdout.

  • System - is a final class in java.lang package.According to Oracle docs: "Among the facilities provided by the System class are standard input, standard output, and error output streams;access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array".
  • Java Collections Framework Tutorial

    Collections in java are used in almost every enterprise application. Java Collections framework consists of interfaces and classes which helps in storing and processing the data efficiently. Java Collections Tutorial will look into each of the classes.

    Read Also :  Collections in Java Interview Questions 

    Java Collections Interfaces and Classes

  • Collection Interface
  • HashSet Class in Java Tutorial with Example

    Java HashSet class implements Set interface. It has the following properties.

    1. HashSet does not allow duplicates, means it contains unique elements.
    2. HashSet is unordered meaning it does not guarantee that the order will remain constant over time.
    3. HashSet is backed by Hashtable (actually a HashMap instance).
    4. HashSet permits the null elements.
    5. HashSet implementation is not synchronized. It can be synchronized externally.
        Set s = Collections.synchronizedSet(new HashSet(...));
    6. Iterators returned by HashSet class is fail- fast.

    HashSet Example

    TreeMap Class in Java Tutorial with Example

    TreeMap class implements the Map interface.It extends AbstractMap class and implements NavigableMap interface. It has the following properties:

    1. TreeMap sorts the keys according to the natural ordering of its keys, or by Comparator provided at  map creation time.
    2. TreeMap is a red black tree based NavigableMap implementation.
    3. TreeMap implementation provides guaranteed log(n) time cost for the containsKey,get,put and remove operations.
    4. TreeMap does not allow null keys and null values.
    5. TreeMap is not synchronized.It must be synchronized externally
        SortedMap  m = Collections.synchronizedSortedMap(new TreeMap(...));

    TreeMap Example