Results 1 to 8 of 8

Thread: File handling

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

    Default File handling

    package first;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;

    public class ReadingFromDayaTxt
    {
    public static void main(String[] args) throws IOException
    {
    File f = new File("daya.txt");
    FileReader fr = new FileReader(f);
    int size = "daya.txt".length();
    System.out.println(size);

    char all[] = new char[size];
    fr.read(all);
    String s1 = new String(all);
    System.out.println(s1);
    }
    }
    daya.txt contains the below characters:-
    DayanandChowdhary.T vl get job in 2011 say all the best

    but i got the below output:-
    8
    Dayanand


    y i did not got the remaining characters of the file daya.txt.?
    is there any specific reason.?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    In
    Java Code:
    int size = "daya.txt".length();
    size is not the length of the file, it's the length of the name of the file.
    To read the file data, wrap that RileReader with a BufferedReader and use its readLine() method.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The best way would be to do as suggested above, read one line at a time and store it as a string, then call string.length(); on it and add that to some variable.

    Java Code:
    //create reader wrapped with buffered reader
    String s;
    int total = 0;
    while((s = reader.nextLine()) != null){
      //find size of s
      //add size to total
    }

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

    Default

    Quote Originally Posted by r035198x View Post
    In
    Java Code:
    int size = "daya.txt".length();
    size is not the length of the file, it's the length of the name of the file.
    To read the file data, wrap that RileReader with a BufferedReader and use its readLine() method.

    name of the file is daya.txt.which is of 4 characters size
    but i got size has 8 and String content has Dayanand.........!!!!!!
    how its possible

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are finding the length of the string data.txt, which is 8 characters.

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

    Default

    Quote Originally Posted by sunde887 View Post
    You are finding the length of the string data.txt, which is 8 characters.
    thanks i got it
    Last edited by Dayanand; 03-23-2011 at 07:30 AM.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Dayanand View Post
    but how about Dayanad has output.?
    Because that is all that you read using
    Java Code:
    fr.read(all)
    check the API specs for that method and compare it with BufferedReader.readLine() method.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you want to find the length of a file you have to read through it as I showed. The line
    Java Code:
     
    int size = "daya.txt".length();
    just refers to the length of the specified string, that string is not representing a file. It is simply a string.

Similar Threads

  1. File handling
    By Dayanand in forum New To Java
    Replies: 1
    Last Post: 03-22-2011, 07:07 PM
  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
  •