• We just launched and are currently in beta. Join us as we build and grow the community.

Create Multiple Files using Java

wache

Semantic Versioning Guru
W Rep
0
0
0
Rep
0
W Vouches
0
0
0
Vouches
0
Posts
165
Likes
79
Bits
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.

  1. import

    java.io.*

    ;

3. In your main, initialize the following files and its file extension.
- For word document
  1. File

    file1 =

    new

    File

    (

    "data.doc"

    )

    ;

- For rich text file
  1. File

    file1 =

    new

    File

    (

    "data.rtf"

    )

    ;

- For text file
  1. File

    file1 =

    new

    File

    (

    "data.txt"

    )

    ;

- For excel file
  1. File

    file1 =

    new

    File

    (

    "data.xls"

    )

    ;

- For access file
  1. File

    file1 =

    new

    File

    (

    "data.accdb"

    )

    ;

4. Now, create a try and catch method. In your try method, do the following code. This will trigger to create files.

  1. try
  2. {
  3. FileWriter

    writer1 =

    new

    FileWriter

    (

    file1, true

    )

    ;
  4. FileWriter

    writer2 =

    new

    FileWriter

    (

    file2, true

    )

    ;
  5. FileWriter

    writer3 =

    new

    FileWriter

    (

    file3, true

    )

    ;
  6. FileWriter

    writer4 =

    new

    FileWriter

    (

    file4, true

    )

    ;
  7. FileWriter

    writer5 =

    new

    FileWriter

    (

    file5, true

    )

    ;

  8. }

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.
  1. catch

    (

    IOException

    e)
  2. {
  3. e.printStackTrace

    (

    )

    ;
  4. }


Output:

createfile.png


Here's the full code of this tutorial:

  1. import

    java.io.*

    ;
  2. public

    class

    CreateFiles
  3. {
  4. public

    static

    void

    main(

    String

    [

    ]

    args)
  5. {
  6. File

    file1 =

    new

    File

    (

    "data.doc"

    )

    ;
  7. File

    file2 =

    new

    File

    (

    "data.rtf"

    )

    ;
  8. File

    file3 =

    new

    File

    (

    "data.txt"

    )

    ;
  9. File

    file4 =

    new

    File

    (

    "data.xls"

    )

    ;
  10. File

    file5 =

    new

    File

    (

    "data.accdb"

    )

    ;
  11. try
  12. {
  13. FileWriter

    writer1 =

    new

    FileWriter

    (

    file1, true

    )

    ;
  14. FileWriter

    writer2 =

    new

    FileWriter

    (

    file2, true

    )

    ;
  15. FileWriter

    writer3 =

    new

    FileWriter

    (

    file3, true

    )

    ;
  16. FileWriter

    writer4 =

    new

    FileWriter

    (

    file4, true

    )

    ;
  17. FileWriter

    writer5 =

    new

    FileWriter

    (

    file5, true

    )

    ;

  18. }

    catch

    (

    IOException

    e)
  19. {
  20. e.printStackTrace

    (

    )

    ;
  21. }
  22. }
  23. }


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.
 

452,496

329,258

329,266

Top