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

3 Types of FILE IO part2

oZwW0Zqv

Visual Recon Expert
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
107
Likes
47
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
TUTORIAL NO 10


In this tutorial you will learn:

File Reading using
1.Standard Input Output
2.New Input Output
3.New Input Output2
4.Anonymous classes

Today we are going to write our remaining two filing functions i.e of new input output and new input output 2. If you haven’t followed my previous tutorial go through it first using this link
Basic step:
Follow the previous tutorial and write the complete program after that continue from here:

1.IMPORT STATEMENTS
First of all write these import statements in your .java file

  1. import

    java.nio.*

    ;
  2. import

    java.nio.file.*

    ;
  3. import

    java.nio.channels.*

    ;
  4. import

    java.util.EnumSet

    ;

We require nio imports for new input output, nio.channels import for new Input output 2 and enumSet import for some functions of new input output 2

2. SETTING UP NIO FUNCTION:
  1. void

    nio(

    )

    {

  2. file_name =

    tf.getText

    (

    )

    ;
  3. Path myfile =

    Paths.get

    (

    file_name)

    ;
  4. if

    (

    Files.exists

    (

    myfile)

    )

    {
  5. beforeReadingTime =

    getTime(

    )

    ;
  6. try

    {
  7. text =

    ""

    ;
  8. byte

    b[

    ]

    =

    Files.readAllBytes

    (

    myfile)

    ;
  9. afterReadingTime =

    getTime(

    )

    ;

First of all we will check whether the file exists in our current directory or not. We will do this by passing the path parameter to exists function. Remember we need to call it with a class named Files class because it is a static function of that class. Point to remember is that in new input output we don’t make a reference of the file class we simply make the reference of the path class so we need a class to handle the functions of the file so there is a Files class for that. Now we start our try catch block first of all we empty our text string. Then make a byte array and store all the bytes read from the file using the readAllbytes function which is called using the Files class name which indicates that it is also a static function. After that we simply calculated our after reading time to set the time in text area.

  1. for

    (

    int

    i =

    0

    ;

    i <

    b.length

    ;

    i++

    )

    {
  2. text +=

    (

    char

    )

    b[

    i]

    ;
  3. }
  4. computeReadTime(

    )

    ;
  5. t_area.setText

    (

    text)

    ;
  6. }

    catch

    (

    IOException

    e)

    {

    }
  7. }
  8. else
  9. {
  10. text =

    ""

    ;
  11. t_area.setText

    (

    text)

    ;
  12. tf.setText

    (

    "File Not Found.. !!"

    )

    ;
  13. }
  14. }

    // end nio

After reading the file we need to add the read contents to the text so we simply add the characters of the file to the text area. Then we ended our try blocks followed by the catch blocks to catch the IOException. If the file not found we simply set the message in the text area.

3. WRITING NIO2 FUNCTION:
  1. void

    nio2(

    )

    {
  2. file_name =

    tf.getText

    (

    )

    ;
  3. Path myfile =

    Paths.get

    (

    file_name)

    ;
  4. text =

    ""

    ;
  5. int

    i =

    0

    ;
  6. if

    (

    Files.exists

    (

    myfile)

    )

    {
  7. beforeReadingTime =

    getTime(

    )

    ;

In this function like always we first clear the text area then specify the file path after that we check if the files exist in the current directory after we compute the reading time because we are just about to read the file.
  1. try

    {
  2. ByteBuffer bf;
  3. bf =

    ByteBuffer.allocate

    (

    (

    int

    )

    Files.size

    (

    myfile)

    )

    ;

  4. ReadableByteChannel rbc =

    Files.newByteChannel

    (

    myfile)

    ;
  5. text =

    ""

    ;
  6. while

    (

    rbc.read

    (

    bf)

    !=

    -

    1

    &

    i <

    Files.size

    (

    myfile)

    )

    {
  7. byte

    b[

    ]

    =

    bf.array

    (

    )

    ;
  8. text +=

    (

    char

    )

    b[

    i]

    ;
  9. i++;
  10. }
  11. afterReadingTime =

    getTime(

    )

    ;
  12. computeReadTime(

    )

    ;
  13. t_area.setText

    (

    text)

    ;
  14. }

    catch

    (

    IOException

    e)

    {

    }

Now for new input output 2 we first created a file buffer and then allocate the size of the file length( Remember: If you allocate small size then remember to flip after reading the contents).Then we create a readablebytechannel and set the file path for that file channel. After that we empty the string and start reading the contents of the file using the byte channel and buffer. We store the values of the buffer in the byte array and convert it to character then add it to the string text variable. After all that we simply computed the reading time and set the text area.

  1. else
  2. {
  3. text =

    ""

    ;
  4. t_area.setText

    (

    text)

    ;
  5. tf.setText

    (

    "File Not Found.. !!"

    )

    ;
  6. }
  7. }

    // end nio2
  8. }

    // end Filepanel

In the else we simply set the text field to file not found. Now are application is complete and our all the button will be working with three different methods of reading the file.

OUTPUT:
output_32.png



Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

327,690

327,698

Top