ois = new ObjectInputStream(is);时抛出异常java.io.EOFException 
不知道怎么解决? public Object getObject(String key) {
ShardedJedis shardedJedis = null;
Object obj = null;
ByteArrayInputStream is = null;
ObjectInputStream ois = null;
try {
shardedJedis = shardedJedisPool.getResource();
byte[] ret = shardedJedis.get(key.getBytes("UTF-8"));
if(ret != null && ret.length >0){
is = new ByteArrayInputStream(ret);
ois = new ObjectInputStream(is);
obj = ois.readObject();

}
} catch (Exception e) {
log.error(e.getMessage() + "-->get:" + e.getCause());
log.error("getObject:"+key);
//e.printStackTrace();
} finally {
try {
if(ois!=null){
ois.close();
}
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
shardedJedisPool.returnResource(shardedJedis);
}
return obj;
}

解决方案 »

  1.   

    没解决过!!!
    作为一个linuxer一看到EOF就很敏感因为我们经常<<EOF的。
    EOF是个文件结束符。
    ois = new ObjectInputStream(is);时抛出异常java.io.EOFException 
    看样子你是在读序列化对象了!!!
    应该是你序列化之后的对象没有成功写入文件吧
    还有,虽说java夸平台但windows的EOF和linux的是不一样的....经常用vim打开windows的文件就看到乱七八糟的东西。
    EOF这东西跟计算机的发展史有很大关系的,
    你只在度对象,如果要弄清楚的话,你最好把你怎么写入对象到文件的代码也发上来看看...
      

  2.   

    这一个是写的方法。
    public String setObject(String key, Object value) {
    String ret = null;
    if (value != null) {
    ShardedJedis shardedJedis = null;
    ByteArrayOutputStream outputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
    outputStream = new ByteArrayOutputStream();
    objectOutputStream = new ObjectOutputStream(outputStream);
    objectOutputStream.writeObject(value);
    shardedJedis = shardedJedisPool.getResource();
    ret = shardedJedis.set(key.getBytes("UTF-8"),
    outputStream.toByteArray());
    } catch (Exception e) {
            log.error(e.getMessage() + "-->" + e.getCause());
            log.error("setObject:"+key);
    e.printStackTrace();
    } finally {
    try {
    objectOutputStream.close();
    outputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    shardedJedisPool.returnResource(shardedJedis);
    }
    }
    return ret;
    }
      

  3.   

    EOFException 指的是读到了文件的结尾,一般的解决方式是在读之前对即将读入的内容进行判断,看是不是结尾。如果是的话就跳出,不是的话就继续读。当然也可以catch了之后直接跳出。
      

  4.   

    这个ois = new ObjectInputStream(is);语句是个很诡异的东西,如果你要执行这个语句,那么你要保证你要读的文件或者字节数组中不为空,里面有数据,否则会报java.io.EOFException异常。如果你的代码ois = new ObjectInputStream(is);和oos =new ObjectOutputStream(os);在一个主函数中,不妨先完成写入,然后oos.close();关闭写入,再进行ois = new ObjectInputStream(is);就不会有java.io.EOFException异常了。