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()
1 2 |
String str1 = "500"; int convertedInt = Integer.parseInt(str1); |
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()
1 2 |
String str1 = "500"; int convertedInt = Integer.parseInt(str1); |
How to Create a String As we know that String in java can be created using 2 types. 1. Direct initialization String textString = "This is test"; 2. Using a new operator [crayon-68eb8a8ecd14012691317
We can create dir in java either using File.mkdir() or File.mkdirs(). Example: Create Dir in java using File.mkdir()
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void createDir() { File directory = new File("C:\\kscodes"); boolean dirCreated = directory.mkdir(); if (dirCreated) { System.out.println("Directory Created Successfully !!!"); } else { System.out.println("Unable to create Directory"); } } |
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 InputS
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. File.renameTo(File dest) renames the current file with the name of
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.d
In this post we will see an example on how we can create file in java. We will use the File.createNewFile() method. File.createNewFile() creates a new empty file if a file doesn’
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 code
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static void usingBufferedOutputStream() { String CONTENT_TO_WRITE = "This is test content that we will write in a file"; File fout = new File("c:\\testFileForWriting.txt"); try (FileOutputStream fos = new FileOutputStream(fout); BufferedOutputStream bos = new BufferedOutputStream(fos)) { byte data[] = CONTENT_TO_WRITE.getBytes(); bos.write(data, 0, data.length); System.out.println("Written a file !!!!"); } catch (Exception e) { System.err.println("Error while writing file" + e.getMessage()); } } |
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 th