File Operations in Java
Tags: java, beginners, tristan
Published: 2021-05-23 20:17:31

#Introduction
- This is the sixth post in my streams series and it will be on file operations in Java. However, this post will not show much code, instead we will be using this post to get ourselves familiar with the terminology that is needed to more forward. Since there is not code, I will also not be posting a YouTube video but you can still check out my [YouTube](https://www.youtube.com/channel/UCN9jWE9COJ0wN1m4huDFqzw) videos if you want.


###File operations
- the Files class is the main entry point for the Java.nio.file package. This class offers a rich set of static methods for reading, writing and manipulating files/directories. Before we get ahead of ourselves, lets try to understand exactly what a file is in Java.


###Files
- The files class is just like any other class in Java, except it consists exclusively of static methods that operate on files and directories. Now it is important to understand that these methods work on instances of [Path ](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html) objects. Just a quick reminder on Path objects. A Path object is an object that represents a path to a specific directory or file.


###Releasing System Resources
- Many of the resources that are used , like, streams and channels, implement or extent the closeable interface. In Java a channel is a representation of an open connection to a entity and an entity could be a hardware device or a file. Technically speaking, a "channel" is an interface and its super interface is closeable. If you are unfamiliar, a Java interfaces can extend another interface and the interface that gets extended is now considered to be the super interface of the interface that is doing the extending. 

- When dealing with resources, if anything implements or extends the closeable interface, then the close method must be invoked to release the resource. Neglecting to close a resource can have a negative impact on an application's performance.

- To summarize, anytime we use a resource that implements the closeable interface we must make sure to close that resource. If we fail to do so then our application could suffer performance issues. 


###Catching Exceptions
- With file I/O, unexpected conditions are bound to happen. All methods that can access the file system can also throw IOExceptions. It is best practice to catch these exceptions by using a try-with-resources statement. I will leave a link to the documentation [here](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) if you want to get into the specifics.

- Basically a try-with-resource statement is just a try block that declares a a resource in the opening statement. A resource being defined as an object that must be closed after the program finishes. The beauty of the try-with-resource statement is that it ensures each resource is closed at the end of the statement. The [try-with-resource statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) is the recommended way to handle IOExceptions.



###Varargs
- Several file methods accept an arbitrary number of arguments and we can access these arguments through varargs. Varargs is a construct that we use when we don't know how many of a particular type of argument we are passing to a method. This construct is just a short cut for making an array. If you would like to learn more about varargs, then do checkout the [documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/language/varargs.html). The point of this article is to familiarize ourselves with some basic terms. We will dive more deeply into the specifics when we come across them in future posts.

 
###Atomic Operations
- some methods, such as move() can perform certain operations atomically. An atomic file operation is an operation that cannot be interrupted or partially performed. Either the entire operation happens or the operation fails.


###Method Chaining
- Many of the file I/O methods support the concept of method chaining. This happens when you involve a method that returns an object and the immediately invoke a method on that object, which returns yet another object. This technique produces compact code and allows us to avoid declaring temporary variables 


#Conclusion
- Moving forward we will be creating and understanding code that will access and create files. I am very excited and I hope you are too.
- Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on [Twitter](https://twitter.com/TristAndroidDev).
- Also make sure to checkout my [YouTube](https://www.youtube.com/channel/UCN9jWE9COJ0wN1m4huDFqzw/playlists) channel for more programming tutorials