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
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.d
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. File.createNewFile() creates a new empty file if a file doesn’
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 code
Using CDN for jQuery – Google and Microsoft
Using CDN for jQuery is a very good practice and has lots of benefits over using jQuery from local. Using CDN for jQuery – Google
1 |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
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
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
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
Try-with-resources Statement – Java 7
Many a times we forget to the close the file/jdbc resources that we open inside a try block after the operations are complete. This can cause memory leak issue. Starting from JDK 1.7, a new feature
Sorting of ArrayList in Java using Comparators
In this article we will see the sorting of arraylist in java using comparators. This is mainly used for sorting arraylist that contain user defined objects. Collection.sort() takes 2 parameters. 1s
Sorting of ArrayList in Java – primitives and literals
In this article we will explain the method used for sorting of arraylist for primitives/literals. Sorting of Arraylist for primitives and literals For sorting arrayList of any primitives or literal