Write text file using java
These are file path, character sequence, charset, and options. The first two parameters are mandatory for this method to write into a file. It writes the characters as the content of the file. It returns the file path and can throw four types of exceptions. It is better to use when the content of the file is short. Example: It shows the use of the writeString method that is under the Files class to write data into a file. Another class, Path, is used to assign the filename with a path where the content will be written.
Files class has another method named readString to read the content of any existing file that is used in the code to check the content is properly written in the file. IOException; import java. Files; import java. Method 2: Using FileWriter Class If the content of the file is short, then using the FileWriter class to write in the file is another better option. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 4. See more linked questions. Related Hot Network Questions. The simplest way to write text to a file requires us to use PrintWriter class from the standard package java.
The class PrintWriter has the familiar print and println methods we have been using for writing to the console. The following program writes the name of four oceans to the text file. During program execution, various things can happen — For example, suppose you create a PrintWriter object and pass the name of a file to its constructor The PrintWriter object attempts to create the file, but unexpectedly the disk is full and the file cannot be created.
If such things happen, we say that an exception has occurred. If an exception occurs in a method, then the method should either handle the exception or throw it for the calling environment to handle.
The actual text that needs formatting goes after the comma. The printf method is quite useful, and we'll go through the options in more detail in a later section. For now, let's crack on. To test out your new class, go back to your FileData class the one with the main method. Add the following line to create a new object from your WriteFile class:. So we've set up a WriteFile object called data. In between the round brackets of WriteFile, we've added two things: the name of the file, and an append value of true.
This will ensure that the second of the constructors we set up gets called. If we wanted to just overwrite the file, then the code would be this:. Because we set the default append value as false, we only need the file name if we want to overwrite the entire contents. To let the user know that something has happened, you can print something to the Output window:.
If you like, add another try … catch part for your text writing. Instead of the above, change it to this:. Now run your code to test it out. You should see the contents of your text file in the Output window followed by the message that the text file has been written to:.
Run the programme again and you should see the new line appear. You can comment out the code that writes to the text file.
0コメント