wache
Semantic Versioning Guru
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
Today, I will teach you how to create different files using Java. Here, we will just use the io package.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of CreateFiles.java.
2. Import java.io package. Hence we will use an input/output in creating files.
3. In your main, initialize the following files and its file extension.
- For word document
- For rich text file
- For text file
- For excel file
- For access file
4. Now, create a try and catch method. In your try method, do the following code. This will trigger to create files.
In your Catch method, prefer to catch IOException then use printStackTrace() method. This will help to trace the exception and identify which method causes the bug.
Output:
Here's the full code of this tutorial:
Hope this helps! :)
Best Regards,
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Download
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of CreateFiles.java.
2. Import java.io package. Hence we will use an input/output in creating files.
- import
java.io.*
;
3. In your main, initialize the following files and its file extension.
- For word document
- For rich text file
- For text file
- For excel file
- For access file
4. Now, create a try and catch method. In your try method, do the following code. This will trigger to create files.
- try
- {
- FileWriter
writer1 =
new
FileWriter
(
file1, true
)
;
- FileWriter
writer2 =
new
FileWriter
(
file2, true
)
;
- FileWriter
writer3 =
new
FileWriter
(
file3, true
)
;
- FileWriter
writer4 =
new
FileWriter
(
file4, true
)
;
- FileWriter
writer5 =
new
FileWriter
(
file5, true
)
;
- }
In your Catch method, prefer to catch IOException then use printStackTrace() method. This will help to trace the exception and identify which method causes the bug.
- catch
(
IOException
e)
- {
- e.printStackTrace
(
)
;
- }
Output:

Here's the full code of this tutorial:
- import
java.io.*
;
- public
class
CreateFiles
- {
- public
static
void
main(
String
[
]
args)
- {
- File
file1 =
new
File
(
"data.doc"
)
;
- File
file2 =
new
File
(
"data.rtf"
)
;
- File
file3 =
new
File
(
"data.txt"
)
;
- File
file4 =
new
File
(
"data.xls"
)
;
- File
file5 =
new
File
(
"data.accdb"
)
;
- try
- {
- FileWriter
writer1 =
new
FileWriter
(
file1, true
)
;
- FileWriter
writer2 =
new
FileWriter
(
file2, true
)
;
- FileWriter
writer3 =
new
FileWriter
(
file3, true
)
;
- FileWriter
writer4 =
new
FileWriter
(
file4, true
)
;
- FileWriter
writer5 =
new
FileWriter
(
file5, true
)
;
- }
catch
(
IOException
e)
- {
- e.printStackTrace
(
)
;
- }
- }
- }
Hope this helps! :)
Best Regards,
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Download
You must upgrade your account or reply in the thread to view the hidden content.