📄 savetaskinfo.java
字号:
package com.network.flashget;
import java.io.*;
import java.util.LinkedList;
/**
*用于向文件中存储信息
**/
class SaveTaskInfo
{
File downFile;
File finishedFile;
File deletedFile;
File indexFile = new File(".\\Info\\index.txt");
LinkedList downList = new LinkedList();
LinkedList finishedList = new LinkedList();
LinkedList deletedList = new LinkedList();
/**
*初始化一些变量
**/
SaveTaskInfo(LinkedList a,LinkedList b,LinkedList c,int index)
{
downFile = new File(".\\Info\\Down"+index+".txt");
finishedFile = new File(".\\Info\\Fini"+index+".txt");
deletedFile = new File(".\\Info\\Delt"+index+".txt");
this.downList = a;
this.finishedList = b;
this.deletedList = c;
//若文件不存在,则创建文件进行存储-------------------------------------
try
{
if(!downFile.exists())
{
downFile.createNewFile();
}
if(!finishedFile.exists())
{
finishedFile.createNewFile();
}
if(!deletedFile.exists())
{
deletedFile.createNewFile();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
//连接文件存储信息------------------------------------------------------------
public void saveInfo()
{
//正在下载文件信息存储------------------------------------------------------
try
{
FileOutputStream fos = new FileOutputStream(downFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println(downList.size());
writeToFile(downList,oos);
}
catch(Exception e1)
{
e1.printStackTrace();
}
//已下载文件信息存储--------------------------------------------------------
try
{
FileOutputStream fos = new FileOutputStream(finishedFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println(finishedList.size());
writeToFile(finishedList,oos);
}
catch(Exception e2)
{
e2.printStackTrace();
}
//已删除文件信息存储--------------------------------------------------------
try
{
FileOutputStream fos = new FileOutputStream(deletedFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println(deletedList.size());
writeToFile(deletedList,oos);
}
catch(Exception e3)
{
e3.printStackTrace();
}
}
/**
*用于写入对象信息---------------------------------------------------------------------
**/
public void writeToFile(LinkedList list,ObjectOutputStream oos)
{
try
{
//若任务链表为空,则向文件中写入空标志
if(list.size()==0)
{
oos.writeObject("empty");
oos.flush();
}
//若任务链表不为空,则将对象写入文件中
else
{
//写入不为空的标志----------------------------------------------------------
oos.writeObject("Not Empty");
oos.flush();
//写入对象信息---------------------------------------------------------------
for(int i = 0;i < list.size();i++)
{
oos.writeObject(list.get(i));
oos.flush();
}
//写入文件结束 标志----------------------------------------------------------
oos.writeObject("end");
oos.flush();
}
oos.close();
}
catch(Exception e5)
{
e5.printStackTrace();
}
}
/**
*用于存入MainWindow中的index信息--------------------------------------------------------
**/
public void saveIndex()
{
try
{
if(!indexFile.exists())
{
indexFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(indexFile);
System.out.println("Save index:"+MainWindow.index);
fos.write(MainWindow.index);
fos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -