Solved

EOF Exception

Posted on 2002-07-09
9
1,834 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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: 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.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

707 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