Results 1 to 4 of 4

Thread: File handling

  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default File handling

    writer code:-

    package reader_Writer;

    import java.io.FileWriter;
    import java.io.IOException;

    public class My2
    {
    public static void main(String[] args) throws IOException
    {
    FileWriter writer = null;
    try
    {

    writer = new FileWriter("books.txt");
    writer.write("java\n");
    writer.write("hibernate\n");
    writer.write("spring\n");
    writer.write("android");
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    finally
    {
    if (writer != null)
    {
    writer.flush();
    writer.close();
    writer=null;
    }
    }
    }
    }

    is it necessary the below statement to be present in the above program.?
    writer=null;
    what is the use of writer=null;


    ReaderCode:-
    package reader_Writer;

    import java.io.File;
    import java.io.FileReader;

    public class My2Extend
    {
    public static void main(String[] args)
    {
    FileReader reader = null;
    try
    {
    reader = new FileReader("books.txt");
    int size = (int) new File("books.txt").length();
    char all[] = new char[size];
    reader.read(all);
    String s1 = new String(all);
    System.out.println(s1);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    finally
    {
    try
    {
    if (reader != null)
    {
    reader.close();
    reader = null;
    }
    }
    catch (Exception e2)
    {
    e2.printStackTrace();
    }
    }
    }
    }
    what is the use of reader = null; in the above reader code.?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    No need of doing that. close() does all.
    is there any real time example for again intializing writer=null; in writer code and reader = null; in reader`s code

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. File handling
    By Dayanand in forum New To Java
    Replies: 7
    Last Post: 03-23-2011, 07:31 AM
  2. File handling
    By Dayanand in forum New To Java
    Replies: 3
    Last Post: 03-22-2011, 04:20 PM
  3. File handling
    By Claude2005 in forum New To Java
    Replies: 17
    Last Post: 09-21-2010, 11:49 AM
  4. Regarding File Handling
    By ravjot28 in forum New To Java
    Replies: 1
    Last Post: 01-20-2010, 09:15 PM
  5. handling WAV file
    By zellazode in forum New To Java
    Replies: 1
    Last Post: 09-08-2009, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •