Solved

EOF Exception

Posted on 2002-07-09
9
1,831 Views
Last Modified: 2008-02-01
I have a simple code that
1) opens text file ("orig.txt")
2) writes in UTF to "unreadable.txt"
3) reads in the "unreadable.txt", writes to text file ("readable.txt")

I am encountering an EOF exception, and I think I am missing something...

import java.sql.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class copyit
{
  public static void main(String[] args) throws IOException
  {
    String c = null;
    System.out.println("hello world!");
    System.out.println(System.getProperty("file.encoding"));


    /*
     * READ FROM ASCII FILE, WRITE TO ENCODED FILE
     */
    BufferedReader asciiIn =
      new BufferedReader(new FileReader("orig.txt"));
    try
    {
      DataOutputStream utfOut =
        new DataOutputStream(new FileOutputStream("unreadable.txt"));
      while ((c = asciiIn.readLine()) != null)
      {
        utfOut.writeUTF(c);
      }
      utfOut.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    asciiIn.close();

    /*
     * READ FROM ENCODED FILE, WRITE TO ASCII FILE
     */
    DataInputStream utfIn =
      new DataInputStream(new FileInputStream("unreadable.txt"));
    PrintWriter asciiOut = new PrintWriter(new FileWriter("readable.txt"));

    try
    {
      while ((c = utfIn.readUTF()) != null)
      {
        asciiOut.println(c);
      }
      asciiOut.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    utfIn.close();
  }
}
0
Comment
Question by:happyloman
9 Comments
 
LVL 5

Expert Comment

by:fontaine
ID: 7141259
Please post the full stack trace.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7141319
listening
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7141340
I'm interested to know what your motive is for this piece of code. According to the docs for java.io.DataInput:

"If end of file is encountered at any time during this entire process, then an EOFException is thrown."

The 'entire process' referred to is .. reading the file! The fact is that this is not *really* UTF-8 encoding anyway.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 7141358
I meant by my last sentence that readUTF "Reads in a string that has been encoded using a modified UTF-8 format"
0
 

Author Comment

by:happyloman
ID: 7141397
For fontaine:

        at java.lang.Throwable.fillInStackTrace(Native Method)
        at java.lang.Throwable.<init>(Throwable.java:82)
        at java.lang.Exception.<init>(Exception.java:33)
        at java.io.IOException.<init>(IOException.java:35)
        at java.io.EOFException.<init>(EOFException.java:43)
        at java.io.DataInputStream.readUnsignedShort(Compiled Code)
        at java.io.DataInputStream.readUTF(Compiled Code)
        at java.io.DataInputStream.readUTF(Compiled Code)
        at copyit.main(Compiled Code)


For CEHJ

I plead ignorance.  I don't understand what you mean.

The code is just trying to do two things
a. make an output file ("unreadable.txt") so that a user cannot edit it (e.g. using "vi" in unix)
b. verify that I can programmatically "recover" the original file to a "readable.txt" file

Any suggestions, teachings would be appreciated!!
0
 
LVL 35

Expert Comment

by:girionis
ID: 7141532
From Sun's web site:

"Many of the DataInputStream readXXX()
methods can't do this because any value that could be returned to indicate end-of-file
may also be a legitimate value read from the stream. For example, suppose that you
wanted to use -1 to indicate end-of-file? Well, you can't because -1 is a legitimate
value that can be read from the input stream using readDouble(),
readInt(), or one of the other read methods that reads numbers.
So DataInputStream's readXXX() methods throw an EOFException instead.
When the EOFException occurs the while (true) terminates."

  There is nothing wrong with your piece of code. I would suggest you catch this EOFException:

   try
   {
     while ((c = utfIn.readUTF()) != null)
     {
       asciiOut.println(c);
     }
     asciiOut.close();
   }
   catch (IOException e)
   {
     e.printStackTrace();
   }
   catch(EOFException eofe)
   {
   }

  Hope it helps.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7141541
Sorry, the previous was not from Sun's web site. I got it from here:

 http://www.ecs.umass.edu/ece/wireless/people/emmanuel/java/java/io/dataIO.html

0
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 7141545
 Forgive my persistance but I missed out a line from my first post. The full paragraph should be:

"The readLine() method returns a value, null, that indicates that
the end of the file has been reached. Many of the DataInputStream readXXX()
methods can't do this because any value that could be returned to indicate end-of-file
may also be a legitimate value read from the stream. For example, suppose that you
wanted to use -1 to indicate end-of-file? Well, you can't because -1 is a legitimate
value that can be read from the input stream using readDouble(),
readInt(), or one of the other read methods that reads numbers.
So DataInputStream's readXXX() methods throw an EOFException instead.
When the EOFException occurs the while (true) terminates."
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7142664
>>
I plead ignorance.  I don't understand what you mean.

The code is just trying to do two things
a. make an output file ("unreadable.txt") so that a user cannot edit it (e.g. using "vi" in unix)
b. verify that I can programmatically "recover" the original file to a "readable.txt" file
>>

I guessed you were trying to do something like this. I haven't tried to edit unreadable.txt in vi yet, but I'd be very surprised to find any difficulty. UTF-8 encoding is specifically designed to be backwardly compatible with what are loosely called ASCII encodings. Therefore, apart from a few stray binary characters in the file, the plaintext will still be visible.

If you want to implement a trivial encryption routine, why not try rot13? If you haven't come across this before, pass a string to this program, then pass its output back in:

import java.io.*;

public class Rot13 {
  public static void main (String args[]) {
    if(args.length < 1){
     System.out.println("Usage: rot13 <text to rotate>");
     System.exit(-1);
    }
    System.out.println(TextRotate.rotateText(args[0]));
  }
  static class TextRotate {

     private static final int LOWER_A = 97;
     private static final int LOWER_M = 109;
     private static final int LOWER_N = 110;
     private static final int LOWER_Z = 122;
     private static final int UPPER_A = 65;
     private static final int UPPER_M = 77;
     private static final int UPPER_N = 78;
     private static final int UPPER_Z = 90;

     public static String rotateText (String s) {

        int stringLen = s.length();
        StringBuffer buffer = new StringBuffer(s);

        for (int position = 0; position < stringLen; position++) {

              if (
                 ((buffer.charAt(position) >= LOWER_A) && (buffer.charAt(position) <= LOWER_M)) ||
                 ((buffer.charAt(position) >= UPPER_A) && (buffer.charAt(position) <= UPPER_M))
                 ) {

                 // rotate up 13
                 buffer.setCharAt(position, (char)((int)buffer.charAt(position) + 13));

              } else if (
                 ((buffer.charAt(position) >= LOWER_N) && (buffer.charAt(position) <= LOWER_Z)) ||
                 ((buffer.charAt(position) >= UPPER_N) && (buffer.charAt(position) <= UPPER_Z))
                 ) {

                 // rotate down 13
                 buffer.setCharAt(position, (char)((int)buffer.charAt(position) - 13));

              }
        }

        return buffer.toString();

     }

  }
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 87
What's wrong with this code? 4 26
jsp insert to database example 2 47
Bot application - advice 3 38
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question