?
Solved

EOF Exception

Posted on 2002-07-09
9
Medium Priority
?
1,836 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 800 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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