java - Java:只要我清空 ArrayList 从中获取其内容的文本文件,我的 ArrayList 就会自行清空
问题描述
我正在用 java 编写自己的库,您可以在其中非常简单地保存变量。但是我在更改变量的值时遇到了问题。一旦 txt 文件为空,ArrayList 就会自行清空。我的代码:
public class SaveGameWriter {
private File file;
private boolean closed = false;
public void write(SaveGameFile savegamefile, String variableName, String variableValue, SaveGameReader reader) throws FileNotFoundException
{
if(!reader.read(savegamefile).contains(variableName))
{
file = savegamefile.getFile();
OutputStream stream = new FileOutputStream(file, true);
try {
String text = variableName+"="+variableValue;
stream.write(text.getBytes());
String lineSeparator = System.getProperty("line.separator");
stream.write(lineSeparator.getBytes());
}catch(IOException e)
{}
do {
try {
stream.close();
closed = true;
} catch (Exception e) {
closed = false;
}
} while (!closed);
}
}
public void setValueOf(SaveGameFile savegamefile, String variableName, String Value, SaveGameReader reader) throws IOException
{
ArrayList<String> list = reader.read(savegamefile);
if(list.contains(variableName))
{
list.set(list.indexOf(variableName), Value);
savegamefile.clear();
for(int i = 0; i<list.size()-1;i+=2)
{
write(savegamefile,list.get(i),list.get(i+1),reader);
}
}
}
}
这是我的 SaveGameReader 类:
public class SaveGameReader {
private File file;
private ArrayList<String> result = new ArrayList<>();
public String getValueOf(SaveGameFile savegamefile, String variableName)
{
ArrayList<String> list = read(savegamefile);
if(list.contains(variableName))
{
return list.get(list.indexOf(variableName)+1);
}else
return null;
}
public ArrayList<String> read(SaveGameFile savegamefile) {
result.clear();
file = savegamefile.getFile();
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file));
String read = null;
while ((read = in.readLine()) != null) {
String[] splited = read.split("=");
for (String part : splited) {
result.add(part);
}
}
} catch (IOException e) {
} finally {
boolean closed = false;
while(!closed)
{
try {
in.close();
closed=true;
} catch (Exception e) {
closed=false;
}
}
}
result.remove("");
return result;
}
}
还有我的 SaveGameFile 类:
public class SaveGameFile {
private File file;
public void create(String destination, String filename) throws IOException {
file = new File(destination+"/"+filename+".savegame");
if(!file.exists())
{
file.createNewFile();
}
}
public File getFile() {
return file;
}
public void clear() throws IOException
{
PrintWriter pw = new PrintWriter(file.getPath());
pw.close();
}
}
所以,当我调用方法时setValueOf()
,ArrayList 是空的,并且在 txt 文件中只有第一个变量 + 值。Hier 的我的数据结构:
Name=Testperson
Age=40
Phone=1234
Money=1000
我的代码有什么问题?
解决方案
在您的SaveGameReader.read()
方法中,您有result.clear();
which clears ArrayList
。你甚至在打开文件之前就这样做了。因此,基本上在每次从文件读取操作之前,您都在清理现有状态并从文件中重新读取。如果文件为空,则以空列表结束
推荐阅读
- mysql - Mariadb中止连接错误,没有用户名
- c++ - 重载运算符的矩阵 C++ 问题
- python - 缺少测试覆盖率 pytest python 类
- amazon-dynamodb - 仅获取更新的字段以及 DynamoDB 流中的标识符
- pandas - 加入两个熊猫数据框并在合并后找到后续值
- opencv - CMake 错误:目标链接器不支持 TARGET_PDB_FILE
- amazon-web-services - 使用 Terraform 和 CodeDeploy 管理 ECS 修订
- python - 如何解析复制对象请求的 Boto3 200 响应
- sql - SQL Server 2019 Express 我无法连接
- flutter - 用项目均匀地填充列