Learn Java Tutorial for Beginners, Part 45: Writing Text Files

preview_player
Показать описание
In this video by Quordnet Academy for the series Learn Java Tutorial for Beginners how to use Writing Text Files have been discussed.
If you would like to discover even more about java programming tutorial or the java complete course I advise you to check out the playlist :

Writing to a file is a little easier than reading a file. To write to a file, we'll use two more inbuilt classes: the FileWriter class and the PrintWriter class.

Create a new class in your project by clicking File -- New File from the NetBeans menu. Select Java in the Categories section of the dialogue box and Class from the File Types list. Click the Next button at the bottom. For the Class name type WriteFile, then click Finish. Add the following three import statements your new code:

Your new class should look like this:

Importing the FileWriter and PrintWriter

Again, the underlines are because we haven't used the imported classes yet.

When you write to a file, you can either start from the beginning and overwrite everything. Or you can start at the end and append to the file. The FileWriter class allows you to specify which. We'll add a field that sets the append value for the FileWriter class. We'll also add a field to set the name of the file.

So add the following two fields to your code, plus the constructor:

The WriteFile constructor

The boolean field is called append_to_file and has been set to a value of false. This is the default value for the FileWriter class, and means you don't want to append, but erase everything in the file.

The constructor sets a value for the path field (instance variable), which is the name and location of the file. This will get handed over when we create a new object from our WriteFile class.

As was mentioned in the previous section, however, you can set up more than one constructor in your code. We can set up a second one and pass in an append value. That way, a user can either just use the first constructor and hand over a file name, or a file name and an append value. So add the following constructor below the first one:

public WriteFile( String file_path , boolean append_value ) {

path = file_path;
append_to_file = append_value;

}

This second constructor now has two values between the round brackets, a file path and an append value. If you want to append to the file you can use this constructor when creating a new object. If you just want to overwrite the text file then you can use the first constructor.

Your code window should now look like this:

Java code showing two constructors

To write to the file, add the following method below your two constructors:

public void writeToFile( String textLine ) throws IOException {

}

This method doesn't need to return a value, so we've made it void. In between the round brackets of the method name we have a String variable called textLine. This is obviously the text we want to write to the file. Again, though, we need to add "throws IOException" as we need to do something to handle file-writing errors.

The first thing we need in the method is a FileWriter object. The FileWriter takes care of opening the correct file, and of storing the text as bytes.
So we're creating a new FileWriter object with the name write. In between the round brackets of FileWriter we pass the name and location of the file, plus the append value. This will either be true (append to the file) or false (don't append). If a file of the name you pass over does not exist, the FileWriter creates one for you.

Possibly if you have doubt comment below and let me understand what else I can help you with information in java.This playlist is a full fledged java programming for beginners and java tutorial for beginners which is given above.
Please share with your friends the video to assist other people looking for java programming tutorial or object oriented programming java .
To never miss an update from or channel hit the subscribe button first and if already subscribe hit the bell icon.
1.Follow us on INSTAGRAM for Interesting posts
2.Follow us on LINKEDIN for interesting content on different aspects
3.Don't forget to like our FACEBOOK to get the most out of it
4.Follow us on twitter to get a mix of all
5.If you want to get us on TUMBLR please then click on the link given below
6.Do join our OFFICIAL Telegram for notes of different things
7.For get job update regularly both private and government do join this telegram channel
#quordnetacademy, #java_tutorial_series, #javatutorialseries
Рекомендации по теме
visit shbcf.ru