The course is part of this learning path
In this course, we'll learn about the Java Input/Output subject.
- API and Java I/O
- OutputStream Class
- InputStream Class
- Reader Class
- Writer Class
- Serialization, Deserialization, and Non-Serializable objects
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In the previous lesson, we talked about the OutputStream class. In this lesson, we will write the data we received from the user to an external file using the OutputStream class. If you're ready, let's start. First, I will create a new project. I click the 'File' menu and select the 'New', 'Java Project' options. The project name can be Java Input Output, and the Java version will be JavaSE-1.8 or Java 8. And I click the 'Finish' button. Now, I'll create a new class in this project. I right click on the 'src' folder and select the 'New', 'Class' options. The class name can be WriteData, and I will check the checkbox for the main() method and click the 'Finish' button. First, we'll create a reference object from the OutputStream class, and this reference object will reference the FileOutputStream class. Because the OutputStream class is an abstract class, it's not possible to use it directly. But, we can do the necessary operations by using its subclasses. So, I write OutputStream myOutputStream = new FileOutputStream();
The FileOutputStream class takes the name of the file into which we will write data as a parameter. Therefore, we need to specify the file name. You can specify it directly here as a string expression. For example, the file could be test.txt. The test will be the file name and .txt is its extension.
You can specify the file name as you wish, but it's useful to create the extension as txt because soon we'll open this file and check if data has been written. If you type a different extension, you may not be able to open the file. Yes, you can write the file name like this, or you can define it as final because in large projects, there may be a need to use the file name in different places. In this case, it would be more logical to define the file names as final to avoid misspellings. That's why I will cut this file name from here. Now, let's create a constant object in the class. public static final String FILE_NAME. I'm pasting the file name after the equal sign. Yes, now I'm giving this file name as a constructor parameter to FileOutputStream class.
Now, let's press the control key on the keyboard and look at where the FileOutputStream class is defined. Notice that this class has a single parameter constructor of the type string. Let's also examine the ternary operator here. If the file name is not null, notice that a new file will be created with the expression new File(), and the name of this file will be the string given as a parameter. In other words, if we run the application at this stage, an empty file named test with a txt extension will be created in the file directory. Yes, let's continue now. Of course, if you pay attention, the Eclipse gives a warning. This is to prevent possible errors in Java I/O operations. For example, the FileOutputStream class might throw a FileNotFoundException.
Therefore, we must create a try-catch block here. If you position your mouse over this error, the quick fix dialog will open. If you choose the surround with try-catch option from here, the try-catch block will be generated automatically. Now, let's convert the data we received from the user into a byte array. For this, first, let's get data from the user using the Scanner class. Scanner inputScanner = new Scanner(System.in); Let's give a message to the user. sout("Please enter a text: "); Now, let's take the text the user wrote and transfer it to a string. String data = inputScanner.nextLine(); Yes, now let's convert this string to a byte array in the try block. byte[ ] dataByte = data.getBytes();
Thus, the text we received from the user was converted into a byte array. Now, we can write it to the file using the write() method. myOutputStream.write(dataByte); If you pay attention, the Eclipse warns us because if you remember, the write() method was throwing IOException. Therefore, we'll create a catch block again here. Here, I choose the 'Add catch clause to surrounding try' option. As you can see, it added a second catch block. Yes, actually writing to the file is finished at this stage. However, closing both the OutputStream and the Scanner with the close() method will be better for the efficiency of the program. myOutputStream.close(); inputScanner.close(); You can also call the flush() method before the close() method if you want. These methods do not accept the operation of the program but increase efficiency.
After the writing, let's show a message on the console. Just after the write() method, I write sout("Data is written into the file"); Yes, let's test the app now. For the text, I write Hi Java Developers, and I press the 'Enter' key. The Data is written message has appeared. So, where was this file created? This file is created in the project folder by default. If you right click on your project folder and select the 'Show In', 'System Explorer' option, you will open the file directory where your projects are stored. If we open the Java Input Output project from here, we can see the test.txt file we created. Now, let's open this file. And as you can see, the Hi Java Developers text has been successfully written to the file.
Now, let's write only the word Java to the file by using the write() method with three parameters. Notice that the index number of J, which is the first letter of the word Java, is 3. Therefore, I write 3 for the second parameter. The other parameter is how many characters we want to write. Since the Java word is four characters, I write 4 for the third parameter. Yes, let's run the app again. I'll write Hi Java Developers on the console screen again. Finally, let's press 'Enter'. Yes, the write() operation was successful. Now, let's check the file. As you can see, only the word Java is written this time. Yes, that's how we learned to create a file using the OutputStream class and write data into it. Let's take a short break here. In our next lesson, we'll learn how to read the data that we have written. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.