Create file in Java

Creating files in Java is a basic operation, everyone must know. So, in this tutorial, I just synthesized the ways that we can create a file in Java. Maybe it is useful for beginners of Java programming.

First, I have to mention the File object. This object has been in Java since the JDK 1.0 release, which means it’s been a long time.

In this object, we have a method for creating a file, called createNewFile().

When you use this method, after execution it returns back to you a boolean value. If we were able to create the file without any problems then the return value would be true otherwise if something went wrong, the return value would be false.

The second method we can use to create a file in Java is: to use the Files object, which is introduced from Java 7.

The static method createFile() in the Files object is used to create any file.

In that, the Path object holds the information about the path of the file which we want to create.


Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment