资源泄漏,输出文件未写入



我在return new ArrayList<>();中获得资源泄漏警告. 该文件不是写在朋友。txt,我试图保存列表在一个文本文件。请帮助。

import java.io.*;
import java.util.ArrayList;
public class ReadWrite {
public void writeFriends(ArrayList<Friend> friends) {
FileOutputStream friendFile;
ObjectOutputStream friendWriter;
try {
friendFile = new FileOutputStream(new File("C:\Users\aa\Desktop\src\friends.txt"));
friendWriter = new ObjectOutputStream(friendFile);
if(friends.size() >0) {
friendWriter.writeInt(friends.size());
for (Friend friend : friends) {
friendWriter.writeObject(friend);
}
}
else {
System.out.println("No data to write");
}
friendWriter.close();
friendFile.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found. Retry after creating File 'Friends.txt'");
} catch (IOException e) {
System.out.println("Stream cannot be initialized.");
}
}
public ArrayList<Friend> readFriends() {
FileInputStream friendFile;
ObjectInputStream friendReader;
ArrayList<Friend> friends = new ArrayList<>();
try {
friendFile = new FileInputStream(new File("C:\Users\aa\Desktop\src\friends.txt"));
friendReader = new ObjectInputStream(friendFile);
int size = friendReader.readInt();
if(size > 0){
for (int i = 0; i < friendReader.readInt(); i++) {
friends.add((Friend) friendReader.readObject());
}
}
else{
System.out.println("Empty File");
return new ArrayList<>();
}
friendReader.close();
friendFile.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found. Retry after creating File 'Friends.txt'");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("Stream cannot be inititalized");
}
return friends;
}
}

我试图在friends.txt文件中保存朋友列表。我在friends.txt文件中没有看到任何输出。是否与我的位置或FileOutputStream有关?

你的代码中有两个问题。

  1. ReadWrite类的readFriends方法的for循环存在错误。
  2. friends.txt文件可能无法关闭。

这是更正后的代码。请注意,我在你的问题中找不到Friend类的代码,所以我写了一个最小的类。由于您正在使用序列化,我假设类Friend实现接口Serializable。

代码后注释

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
public class ReadWrite {
public void writeFriends(ArrayList<Friend> friends) {
try (OutputStream friendFile = Files.newOutputStream(Paths.get("C:", "Users", "aa", "Desktop", "src", "friends.dat"));
ObjectOutputStream friendWriter = new ObjectOutputStream(friendFile)) {
if (friends.size() > 0) {
friendWriter.writeInt(friends.size());
for (Friend friend : friends) {
friendWriter.writeObject(friend);
}
}
else {
System.out.println("No data to write");
}
}
catch (FileNotFoundException e) {
System.out.println("File Not Found. Retry after creating File 'friends.dat'");
e.printStackTrace();
}
catch (IOException e) {
System.out.println("Stream cannot be initialized.");
e.printStackTrace();
}
}
public ArrayList<Friend> readFriends() {
ArrayList<Friend> friends = new ArrayList<>();
try (InputStream friendFile = Files.newInputStream(Paths.get("C:", "Users", "aa", "Desktop", "src", "friends.dat"));
ObjectInputStream friendReader = new ObjectInputStream(friendFile)) {
int size = friendReader.readInt();
if (size > 0) {
for (int i = 0; i < size; i++) {
friends.add((Friend) friendReader.readObject());
}
}
else {
System.out.println("Empty File");
return new ArrayList<>();
}
}
catch (FileNotFoundException e) {
System.out.println("File Not Found. Retry after creating File 'friends.dat'");
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
System.out.println("Stream cannot be inititalized");
e.printStackTrace();
}
return friends;
}
public static void main(String[] args) {
ArrayList<Friend> friends = new ArrayList<>();
Friend friend = new Friend("Jane");
friends.add(friend);
ReadWrite rw = new ReadWrite();
rw.writeFriends(friends);
ArrayList<Friend> newFriends = rw.readFriends();
System.out.println(newFriends);
}
}
class Friend implements Serializable {
private String name;
public Friend(String name) {
this.name = name;
}
public String toString() {
return name;
}
}

readFriends方法的for循环条件中,您有以下内容:

friendReader.readInt()

这意味着在每次循环迭代中,您都试图从文件friends.txt中读取另一个int。这个调用失败,因为文件中只有一个int。因此,您需要使用size,该变量包含在for循环之前读取的文件friends.txt中唯一的int

由于您正在使用序列化,因此建议将文件名扩展名为.dat而不是.txt,因为该文件是而不是文本文件

我总是在我的catch块中写printStackTrace(),因为这有助于我定位异常的原因。实际上,您不应该得到FileNotFoundException,因为如果该文件不存在,Java将创建该文件。如果Java创建文件失败,那么很可能是因为用户没有创建文件的权限,所以在运行代码之前显示一个错误消息,说要创建文件,这可能没有帮助。

你的代码可能会成功打开文件并写入一些数据,但在你写完所有数据之前就崩溃了。在这种情况下,代码不会关闭文件。如果您至少使用Java 7,那么您应该使用try-with-resources来确保文件总是关闭。

Java 7还引入了NIO.2作为一个更好的API,用于从Java代码与计算机的文件系统进行交互。我建议您按照我在上面的代码中所示的方式使用它。

最新更新