- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序传输 Twitter 数据并将其写入文件。
while(true){
Status status = queue.poll();
if (status == null) {
Thread.sleep(100);
}
if(status!=null){
list.add(status);
}
if(list.size()==10){
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
String uuid = UUID.randomUUID().toString();
String filename = "C:/path/"+topic+"-"+uuid+".ser";
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject(list);
tweetsDownloaded += list.size();
if(tweetsDownloaded % 100==0)
System.out.println(tweetsDownloaded+" tweets downloaded");
// System.out.println("File: "+filename+" written.");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
list.clear();
}
我有这段代码可以从文件中获取数据。
while(true){
File[] files = folder.listFiles();
if(files != null){
Arrays.sort(//sorting...);
//Here we manage each single file, from data-load until the deletion
for(int i = 0; i<files.length; i++){
loadTweets(files[i].getAbsolutePath());
//TODO manageStatuses
files[i].delete();
statusList.clear();
}
}
}
方法 loadTweets() 执行以下操作:
private static void loadTweets(String filename) {
FileInputStream fis = null;
ObjectInputStream in = null;
try{
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
statusList = (List<Status>) in.readObject();
in.close();
}
catch(IOException | ClassNotFoundException ex){
ex.printStackTrace();
}
}
不幸的是,我不知道为什么有时它会抛出
EOFException
运行此行时
statusList = (List<Status>) in.readObject();
有人知道我该如何解决这个问题吗?谢谢。
最佳答案
我发现您根据您之前的问题使用 getAbsolutePath() 正确传递了文件
根据我所读到的内容,可能有几件事,其中之一是文件为空。
解释这个想法,您可能已经编写了该文件,但某些原因导致该文件内部没有任何内容,这可能会导致 EOFException。该文件实际上存在,只是空的
编辑
尝试将代码括在 while(in.available() > 0)
看起来像这样
private static void loadTweets(String filename) {
FileInputStream fis = null;
ObjectInputStream in = null;
try{
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
while(in.available() > 0) {
statusList = (List<Status>) in.readObject();
}
in.close();
}
catch(IOException | ClassNotFoundException ex){
ex.printStackTrace();
}
}
关于java - ObjectInputStream.readObject 上的文件结尾异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47659784/
我在学习 Java IO 的基础知识时发现,为了在类中使用自定义 writeObject 函数,必须将其声明为私有(private)。 private void writeObject(ObjectO
通常,当您反序列化一个类时,您必须为其创建一个单独的实例: try (ObjectInputStream objectInputStream = new ObjectInputStream(new F
这是我的第一个问题,因为到目前为止我总是能找到我需要的东西,但这次一些相对基本的东西让我发疯。所以我有这个类(class): public class QImage { protected Strin
只是为了说明我的问题的背景;我正在编写一个客户端服务器应用程序。当客户端最初连接时,服务器访问数据库,并通过在 for 循环中将对象写入对象输出流,将特定表中的所有项目发送到客户端。 问题是,客户端仅
我正在开发一个 UI,它从 Zookeeper 读取序列化对象,反序列化它,然后将其转换为 JSON。由于某种原因,我无法反序列化 MQTopic 对象。但我可以对其他对象执行相同的操作。 这是将 b
我有一个使用泛型和对象序列化的简单服务器。 (T是输入格式,U是输出格式)。仅处理输入的简化版本如下所示: public class Server implements Runnable {
我正在编写一个服务器/客户端程序。客户端将“请求”(这是为此目的而设计的对象)发送到服务器,服务器使用 ObjectInputStream 对其进行解码。所有“Request”对象都属于同一类,只是数
我正在尝试创建一个实时消息应用程序。但是,当尝试从 ObjectInputStream 读取对象时,代码无缘无故地挂起。没有抛出异常。 try { System.out.println("Tr
我正在尝试测试自定义序列化,但未调用 readObject() 方法。尽管我可以调用 writeObject 方法。我们可以重载readObject方法()吗? public class Te
在我必须做的作业中,我必须编写一个学生对象,其中包含姓名、社会安全号码、通过的类(class)数量以及每门类(class)的成绩。我创建了一个 gpa 方法来计算 gpa,并用我自己的方法重写了 wr
我已将对象保存到数组列表并将文件另存为 .dat,但当我想读取对象时,它只显示 null 而不是值。 类 public class Person implements Serializable {
我刚刚开始学习序列化并尝试实现它。我有一个服务器、一个客户端和一个学生类。 服务器创建我的学生类(class)的初始实例。然后客户端连接到服务器并篡改与学生相关的属性,即提高 GPA。 出于某种原因,
我想从文件中读取不同类型的对象到 ArrayList ,它们都是扩展类 Advertising 的类的实例。我正在尝试使用以下代码: ArrayList ads = new ArrayList();
我们正在尝试使用简单的嵌套对象反序列化一个相当大的对象。这通常需要大约 5 - 10 毫秒。然而,最近我们在这次通话期间遇到了高达 3000 毫秒的随机延迟。我可以一遍又一遍地运行请求,并从调用中获得
我想使用ObjectInputStream从文件中读取对象。下面是 readObject 方法的内部结构: public void readObject(ObjectInputStream inbos
当我运行下面的代码时,objIn.readObject();抛出异常: classNotFoundException project.Edge project.Edge 是我尝试从文件中读取的对象。
我在 Android 应用程序中使用 ObjectOutputStream 和 ObjectInputStream 保存和读取保存在文件中的对象时遇到一些问题我有两个类(class)“锻炼”和“锻炼”
似乎是一个非常简单的问题,但我在谷歌上找不到直接答案。 我在两个进程之间通过 UDP 有一个简单的 IPC channel 。每条消息都包装在 header 类中(例如数据驱动)。 例如。 enum
我的应用程序传输 Twitter 数据并将其写入文件。 while(true){ Status status = queue.poll(); if (status ==
我在加载保存文件时遇到问题,它在 2 个 int 变量的 .readObject() 中显示“找不到符号”错误。我正确声明了变量,并且这两个变量都是全局变量。我不知道为什么。我试图寻找答案,所以我最终
我是一名优秀的程序员,十分优秀!