1 / 12

How to Create File in Java

https://firstcode.school/how-to-create-file-in-java/

Sudhanshi
Télécharger la présentation

How to Create File in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CREATE FILE IN JAVA Presented by Firstcode

  2. wwww.firstcode.school Hello everyone! I am glad to meet you again with a wonderful article. In java, two packages provide all the necessary classes and methods for file manipulation. They are the java.io package and the java.nio package. File creation is the basic step in file manipulation. In java, file creation is made simpler with many built-in methods and classes. In this article, we will gain information on the various ways to create a file in java. Let’s start!! HOW TO CREATE FILE IN JAVA?

  3. wwww.firstcode.school DIFFERENT WAYS FOR FILE CREATION IN JAVA: Some of the different ways to create a file in java are: 1. createNewFile() 2. FileOutputStream 3. FileWriter 4. createFile() 5. write() 6. writeString() 7. newBufferedWriter()

  4. wwww.firstcode.school The createNewFile() method is a part of the File class in the java.io package. This method helps us to create a new empty file in the specified path. JAVA FILE() CREATENEW Method signature: public boolean createNewFile() throws IOException The return type of the createNewFile() method is a boolean. If the file creation is successful, it returns true. And if the file creation fails, it returns false. The common reason for the failure is that the file already exists in the given location. When the specified path is not available, it will throw an IOException.

  5. wwww.firstcode.school The FileOutputStream is a part of the java.io package. This class deals with byte data. If the file exists at the given location, then the FileOutputStream writes the data into it. Or else it creates a new file in the given name and then writes the data into it. Constructor signature: You can use any of the constructors given below based on our needs. JAVA STREAM FILEOUTPUT public FileOutputStream(String file_name, boolean append) throws FileNotFoundException public FileNotFoundException public append) throws FileNotFoundException public FileNotFoundException FileOutputStream(String file_name) throws FileOutputStream(File fileObject, boolean FileOutputStream(File fileObject) throws

  6. wwww.firstcode.school FileWriter is also a part of the java.io package. We use the FileWriter class to write short content into the file. Unlike FileOutputStream, We can write the string content directly into the file without converting it into bytes. Here also, If the file already exists at the given location, then the FileWriter writes the data into it. Otherwise, it creates a new file in the given name and then writes the given data into it. You can use any of the constructors given below based on our needs JAVA FILEWRITER public FileWriter(String fileName) throws IOException public FileWriter(String fileName, Boolean append) throws IOException public FileWriter(File fileObject) throws IOException public FileWriter(File fileObject, Boolean append) throws IOException

  7. wwww.firstcode.school The createFile() method is a part of the Files class in java.nio package. This method creates a new empty file. The main advantage of this method is there is no need to close the resources like other methods listed above. Method Signature: JAVA () CREATEFILE public static Path createFile(Path, Attribute) throws IOException This createFile() method returns a file. The path parameter is the path instance of the file. The attribute parameter represents the optional file attribute list.

  8. wwww.firstcode.school The write() method is a part of the Files class in java.nio package. It also automatically closes the opened resources like the createFile() method. If the file is not present in the given location, This method creates a new file and writes the content into it. Or else it writes the content into the file. This method deals with bytes. It is used for writing short content and is unsuitable for longer ones. Method Signature: JAVA WRITE() public static Path write(Path path, byte[] bytes, OpenOptions…) throws IOException This write() method returns a file. The path parameter is the path instance of the file. The bytes parameter represents the content for writing in the file. OpenOption represents the StandardOpenOption like StandardOpenOption.APPEND etc.

  9. wwww.firstcode.school It is a new method in the Files class in java.nio package. This method writes the string or text into the given file. If the file is not present in the given location, This method creates a new file and writes the content into it. Or else it writes the content into the file. Unlike the write() method, it can directly write the string data into a file. There is no need to convert the string into bytes. Method Signature: JAVA () WRITESTRING public static Path writeString(Path path, CharSequence csq, OpenOptions...) throws IOException This writeString() method returns a file. The path parameter is the path instance of the file. The second argument represents the Content in String type. OpenOption represents the StandardOpenOption like StandardOpenOption.APPEND etc. You can also add charset as the third argument.

  10. wwww.firstcode.school The newBufferedWriter() method is a part of the Files class in the java.nio.package. It is a new method added in Java8. If the file is available in the given location, the method writes the content into it. Otherwise, it creates a new file and then writes into it. Method Signature: JAVA WRITER() NEWBUFFERED public static BufferedWriter newBufferedWriter(Path path, Charset cs, OpenOptions...)

  11. wwww.firstcode.school SUMMARY There are many ways to create a file in java, as discussed in this article. Choosing the right one based on your requirements is an important step. I hope this article helps you to learn about various methods to create a file in java. Thank you for reading this article. Have a happy programming journey.

More Related