Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Cannot read last byte of a file...."

  1. #1
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Unhappy Cannot read last byte of a file...."

    I have a text file named "abcd.txt"
    It contains data as "abcdefghijklmnopqrstuvwxyz"
    '
    So....i need to print the last byte of a file . . that is "z" after program execution'
    I just tried to read file and using seek function, i tried to print...'
    But...it gives error..'
    Plz help me to solve this....'
    Thanx in advance...'
    '
    Code...
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
     
    public class last_byte
    {
        public static void main(String args[]) throws FileNotFoundException, IOException
        {
                File rcfile=new File("D:/abcd.txt");
                RandomAccessFile raf = new RandomAccessFile(rcfile,"rwd");
                raf.seek(rcfile.length()-1);
                char lb=raf.readChar();
                System.out.print(lb);
        }
     
    }
    '
    Error...
    Exception in thread "main" java.io.EOFException
    	at java.io.RandomAccessFile.readChar(RandomAccessFile.java:747)
    	at data.last_byte.main(last_byte.java:13)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Cannot read last byte of a file...."

    Read the API doc for the class's readChar() method and see if you are using it correctly.
    What does it say it will do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member Banafshe's Avatar
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Cannot read last byte of a file...."

    Hi,
    If you print rcfile.length(), It'll show 27, but this is 26 charachters in your abcd.txt file.
    so if you put raf.seek(rcfile.length()-2); the problem will be solved.

    may be there is an End of File charachter in text file that you can not see.

    Note: It is better the class name starts with capital letter, e.g. Last_byte insted of last_byte
    Banafshe

  4. #4
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Cannot read last byte of a file...."

    @Banafshe....Thanx....it solved to some extent...'
    Now, i dont get error...'
    '
    But instead of pronting "z"...it prints "祺"(some chinese character)'
    '
    Output...
    祺
    BUILD SUCCESSFUL (total time: 0 seconds)

  5. #5
    Junior Member Banafshe's Avatar
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot read last byte of a file...."

    Are you insisting on using RandomAccessFile?
    Why don't you use something else like this?

    In this code, 10 is the last character in file.
    import java.io.*;
     
    public class last_byte
    {
        public static void main(String args[]) throws FileNotFoundException, IOException
        {
                File rcfile=new File("abcd.txt");
                InputStream in = new FileInputStream(rcfile);
                Reader reader = new InputStreamReader(in);
     
                int intCh;
                int theLastChar = 10;
     
                while ((intCh = reader.read()) != -1) {
                    char ch = (char) intCh;
                    if (intCh != 10) {
                    	theLastChar = ch;
                    }
                }
                System.out.println((char)theLastChar);
        }
     
    }
    Banafshe

  6. #6
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Cannot read last byte of a file...."

    Thanxx.....@Banafshe..'
    It worked...'
    Be free to help......n.....being free to help.....'

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Cannot read last byte of a file...."

    If you look at the contents of the file in a hex editor you will see what it contains.
    Most files created with a text editor write out files with one character per byte.
    If you save text in Unicode format (wordpad will do it) it will use two bytes per character.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: October 5th, 2012, 08:27 AM
  2. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 12:01 PM
  3. access denied (java.io.FilePermission "report.jrxml" read)
    By banny7 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 3rd, 2011, 07:02 AM
  4. Replies: 0
    Last Post: June 19th, 2011, 03:16 AM
  5. Replies: 1
    Last Post: April 7th, 2011, 02:32 PM

Tags for this Thread