Skip to main content

Posts

Showing posts from December, 2018

I/O in Java - Masterclass (Part 0)

Java input and output processes the input and produces the output based on the input. Java uses streams to perform input and output operations.

In this masterclass on I/O, we will learn about the nitty gritty details of the File IO/NIO with lots of code examples.

You can follow the below trail to dive deep into Java I/O
Part 1 - BasicsPart 2 - IO vs. NIOPart 3 - try-with-resourcesPart 4 - Directory OperationsPart 5 - File OperationsPart 6 - Temporary File OperationsAll of these posts include code examples to understand the concept. You can find the code of these examples on my GitHub repository. Feel free to fork or open issues, if any.
I would love to hear your thoughts on this and would like have suggestions from you to make it better. 
Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.
Happy Coding 😊

I/O in Java - Temporary File Operations (Part 6)

Sometimes it happens that we need to work with temporary files. This generally happens while we create unit test cases. As soon as the test case completes its execution, we simply do not care about the file or its content.

For such cases, it is good to create temporary files rather than creating actual files. In this post, we will look into the code snippets that deal with temporary files in Java.
Create a temporary file using java.io.File classprivatestaticvoidcreateUsingFile(){try{ File tempFile = File.createTempFile("tempFile",".txt"); System.out.println("Temp file is created at :: "+ tempFile.getAbsolutePath());}catch(IOException e){ e.printStackTrace();}}
Create a temporary file using NIO API privatestaticvoidcreateUsingNIO(){try{// Pathfinal Path filePath = Files.createTempFile("tempFile",".txt"); System.out.println("Temp file is created at :: "+ filePath);// Delete fi…

I/O in Java - File Operations (Part 5)

In this post, we will look into the code for commonly used file operations one by one with code snippets. So without further ado, let's jump into the code straight away.
Create a file using java.io.File classprivatestaticvoidcreateFileUsingFile(String path){try{// Creating a new file at the given path with the name "file1.txt" File file =new File(path +"file1.txt");// The condition in if block returns true if the file is createdif(file.createNewFile()){ System.out.println("New file is created!");}else{ System.out.println("File already exists or some error occurred");}// Writing some content in the file FileWriter fileWriter =new FileWriter(file); fileWriter.write("Test data in "+ file.getName()); fileWriter.close();}catch(IOException e){ e.printStackTrace();}}
Create a file using FileOutputStreamprivatestaticvoidcreateFileUsingFileOutputStr…

I/O in Java - Directory operations (Part 4)

In the previous parts of this series I/O in Java, we discussed various aspects of file handling in Java and the classes that are normally used.

Today we will focus on two directory operations - Copy contents of one directory to another directory and recursively delete the contents of a given directory.

We will now see the sample code to perform these two operations. The code is fairly simple and self-explanatory.
Copy directories and their contents Below code snippet can be used to perform this operation -  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55privatestaticvoidcopyDirectory(String sourceFolderPath, String destinationFolderPath){ System.out.println("Copying process starts...");try{// Getting file reference for source and destination folders File sourceFolder =new File(sourceFolderPath); File destinationFolder =new File…