Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1846
  • Last Modified:

EOF Exception

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
happyloman
Asked:
happyloman
1 Solution
 
fontaineCommented:
Please post the full stack trace.
0
 
bobbit31Commented:
listening
0
 
CEHJCommented:
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 expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
CEHJCommented:
I meant by my last sentence that readUTF "Reads in a string that has been encoded using a modified UTF-8 format"
0
 
happylomanAuthor Commented:
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
 
girionisCommented:
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
 
girionisCommented:
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
 
girionisCommented:
 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
 
CEHJCommented:
>>
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now