How to convert String to int

In Java we have multiple ways to convert String to int. We will see a few of those ways in this post. 1. Convert String to int using Integer.parseInt() [crayon-5f971d3ed383f684029445/] If the String value doesn’t contain all digits OR if the String

Read More

String Pool in Java and String Initialization

How to Create a String As we know that String in java can be created using 2 types. 1. Direct initialization [crayon-5f971d3ed44c7232057369-i/] 2. Using a new operator [crayon-5f971d3ed44d0426046230-i/] Difference between these 2 types Before going to the differences, we will first understand

Read More

How to Create Dir in java

We can create dir in java either using [crayon-5f971d3ed4a16480301776-i/] or [crayon-5f971d3ed4a1f151957888-i/]. Example: Create Dir in java using File.mkdir() [crayon-5f971d3ed4a24315671366/] File.mkdir is used to create a directory that is given in the path. If we specify “C:\\kscodes\\work” as in the above example, then

Read More

How to Copy File in java

There are many ways in which you can copy file in java. Example : Copy File in java using InputStream/OutStream In the first example we will see the traditional way of copying file using the InputStream/OutStream. This is the method used prior

Read More

How to Rename file in Java

In this post we will see an example on how we can rename file in java. We will use the File.renameTo(File dest) method. [crayon-5f971d3ed5378633674868-i/] renames the current file with the name of the file in the param. In other words renameTo will

Read More

How to Delete File in java

In this post we will see an example on how we can delete file in java. We will see examples of the File.delete() and File.deleteOnExit() methods in detail Example : Delete File in java using File.delete() [crayon-5f971d3ed57fb091500484/] [crayon-5f971d3ed5805266862081-i/] returns “true” if a

Read More

How to Create File in Java

In this post we will see an example on how we can create file in java. We will use the File.createNewFile() method. [crayon-5f971d3ed5c09619193898-i/] creates a new empty file if a file doesn’t exists already in the given path. createNewFile() returns “true” if

Read More

Using String in a Switch Statement – JDK 7

JDK 7 had a lot of list of enhancements. One of it was use of string in a switch statement. In this post we will see how to use String in a switch statement. We will first see how we needed to

Read More

Writing file in java

Writing file in java can be done in various ways. We will see some ways along with the code. Writing File in java using FileOutputStream and BufferedOutputStream [crayon-5f971d3ed63fa042208953/] Please Note: FileOutputStream has another construtor that is FileOutputStream(File file, boolean append) If append =

Read More

Reading file in java

Reading file in java can be done using either FileInputStream or BufferedReader. Reading File in java using FileInputStream The below code uses a FileInputStream to get access of the File and then read each character of the file. We are using a StringBuffer to store

Read More