⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loadfile.java

📁 一个简单的多线程下载程序
💻 JAVA
字号:
package com.network.flashget;

import java.io.*;
import java.util.LinkedList;

/**
 *用于从文件中读取信息
 **/
class LoadFile
{
	File downFile;
	File finishedFile;
	File deletedFile;
	File indexFile = new File(".\\Info\\index.txt");
	boolean downFlag = true;
	boolean finishedFlag = true;
	boolean deletedFlag = true;
	
	/**
	 *初始化一些变量----------------------------------------------
	 **/
	LoadFile(int index)
	{
		downFile = new File(".\\Info\\Down"+index+".txt");
		finishedFile = new File(".\\Info\\Fini"+index+".txt");
		deletedFile = new File(".\\Info\\Delt"+index+".txt");
		/**
		 *判断存储未下载完任务信息的文件是否为空------------------
		 **/
		try
		{
			//若文件存在,判断是否为空
			if(downFile.exists())
			{
				FileInputStream fis = new FileInputStream(downFile);
				ObjectInputStream ois = new ObjectInputStream(fis);
				
				//读取标志
				String s = (String)ois.readObject();
				
				//若标志为空,置标志位
				if(s.equals("empty"))
				{
					downFlag = false;		
				}
				ois.close();
			}
			
			//若文件不存在,创建新文件,并置标志位
			else
			{
				downFile.createNewFile();
				downFlag = false;
			}
		}
		catch(Exception e4)
		{
			e4.printStackTrace();
		}
				
		/**
		 *判断存储已下载完任务信息的文件是否为空
		 **/
		try
		{
			//若文件存在,判断是否为空
			if(finishedFile.exists())
			{
				FileInputStream fis = new FileInputStream(finishedFile);
				ObjectInputStream ois = new ObjectInputStream(fis);
				
				//读取标志
				String s = (String)ois.readObject();
				
				//若标志为空,置标志位
				if(s.equals("empty"))
				{
					finishedFlag = false;		
				}
				ois.close();
			}
			
			//若文件不存在,创建新文件,并置标志位
			else
			{
				finishedFile.createNewFile();
				finishedFlag = false;
			}
		}
		catch(Exception e5)
		{
			e5.printStackTrace();
		}
		
		
		/**
		 *判断存储已删除任务信息的文件是否为空
		 **/
		try
		{
			//若文件存在,判断是否为空
			if(deletedFile.exists())
			{
				FileInputStream fis = new FileInputStream(deletedFile);
				ObjectInputStream ois = new ObjectInputStream(fis);
				
				//读取标志
				String s = (String)ois.readObject();
				
				//若标志为空,置标志位
				if(s.equals("empty"))
				{
					deletedFlag = false;		
				}
				ois.close();
			}
			
			//若文件不存在,创建新文件,并置标志位
			else
			{
				deletedFile.createNewFile();
				deletedFlag = false;
			}
		}
		catch(Exception e6)
		{
			e6.printStackTrace();
		}
	}
	
	/**
	 *连接文件并读取信息
	 **/
	public void loadInfo()
	{
		if(downFlag)
		{
			try
			{
				FileInputStream fis = new FileInputStream(downFile);
				ObjectInputStream ois = new ObjectInputStream(fis);
				
				//读取文件开始标志字符串
				String s = (String)ois.readObject();
				System.out.println("DownFile:"+s);
				
				//读取真正有实际意义的对象
				Object loadedTaskManager = ois.readObject();
				
				//判断对象是不是文件结束标志
				while(!loadedTaskManager.equals("end"))
				{
					//将该对象用链表暂存起来,以便统一管理
					MainWindow.taskDownList.add((TaskManage)loadedTaskManager);
					loadedTaskManager = ois.readObject(); 
				}
				ois.close();
			}
			catch(Exception e1)
			{
				e1.printStackTrace();
			}
		}
		
		if(finishedFlag)
		{
			try
			{
				FileInputStream fis = new FileInputStream(finishedFile);
				ObjectInputStream ois = new ObjectInputStream(fis);
				
				//读取文件开始标志字符串
				String s = (String)ois.readObject();
				System.out.println("FinishedFile:"+s);
			
				//读取真正有实际意义的对象
				Object loadedTaskManager = ois.readObject();
				
				//判断对象是不是文件结束标志
				while(!loadedTaskManager.equals("end"))
				{
					//将该对象用链表暂存起来,以便统一管理
					MainWindow.taskFinishedList.add((TaskManage)loadedTaskManager);
					loadedTaskManager = ois.readObject(); 
				}
				System.out.println(MainWindow.taskFinishedList.size());
				ois.close();
			}
			catch(Exception e2)
			{
				e2.printStackTrace();
			}
		}
		
		if(deletedFlag)
		{
			try
			{
				FileInputStream fis = new FileInputStream(deletedFile);
				ObjectInputStream ois = new ObjectInputStream(fis);
				
				//读取文件开始标志字符串
				String s = (String)ois.readObject();
				System.out.println("DeletedFile:"+s);
				
				//读取真正有实际意义的对象
				Object loadedTaskManager = ois.readObject();
				
				//判断对象是不是文件结束标志
				while(!loadedTaskManager.equals("end"))
				{
					//将该对象用链表暂存起来,以便统一管理
					MainWindow.taskDeletedList.add((TaskManage)loadedTaskManager);
					loadedTaskManager = ois.readObject(); 
				}
				System.out.println(MainWindow.taskDeletedList.size());
				ois.close();
			}
			catch(Exception e3)
			{
				e3.printStackTrace();
			}
		}
	}
	
	/**
	 *用于获得MainWindow中index信息-------------------------------------------------
	 **/
	public void loadIndex()
	{
		try
		{
			if(!indexFile.exists())
			{
				indexFile.createNewFile();		
			}
			
			FileInputStream fis = new FileInputStream(indexFile);
			MainWindow.index = fis.read();
			fis.close();
			
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	
	/**
	 *将任务信息加入到表格中-------------------------------------------------------
	 **/
	public void addToTabel()
	{
		//打开未下载完任务文件,并存储起来------------------------------------------------------
    	for(int i = 0;i < MainWindow.taskDownList.size();i++)
    	{
    	//	System.out.println("TaskCount is :"+taskCount);
    	//	System.out.println("RowValue is :"+((TaskManage)taskDownList.get(i)).rowValue);
    		
    		MainWindow.taskCount++;
    		MainWindow.runningList.add(new Boolean(false));
    		MainWindow.statusList.add(new Boolean(false));
    		String tempStatus = "Stop";
       		String tempName = ((TaskManage)MainWindow.taskDownList.get(i)).fileName;
    		long tempLength =  ((TaskManage)MainWindow.taskDownList.get(i)).fileLength;
    		long tempDown =  ((TaskManage)MainWindow.taskDownList.get(i)).totalDownLength;
    		float tempRate = ((TaskManage)MainWindow.taskDownList.get(i)).downRate;
    		String tempSave = ((TaskManage)MainWindow.taskDownList.get(i)).saveTo;
    		int tempThreadNum = ((TaskManage)MainWindow.taskDownList.get(i)).threadNum;
    		String tempURL = ((TaskManage)MainWindow.taskDownList.get(i)).url.toString();
    		
    		Object[] info = {tempStatus,tempName,tempLength,tempDown,tempRate,tempSave,tempThreadNum,tempURL};
			MainWindow.tableDownModel.addRow(info);
			
			((TaskManage)MainWindow.taskDownList.get(i)).stopFlag = false;
       	}
       	
       	//打开已下载完任务文件,并存储起来--------------------------------------------------------------------
       	for(int i = 0;i < MainWindow.taskFinishedList.size();i++)
       	{
    		String tempStatus = "Finished";
       		String tempName = ((TaskManage)MainWindow.taskFinishedList.get(i)).fileName;
    		long tempLength =  ((TaskManage)MainWindow.taskFinishedList.get(i)).fileLength;
    		
    		Object[] info = {tempStatus,tempName,tempLength,""};
			MainWindow.tableFinishedModel.addRow(info);
       	}
       	
       	//打开已删除任务文件,并存储起来--------------------------------------------------------------------
       	for(int i = 0;i < MainWindow.taskDeletedList.size();i++)
       	{
    		String tempStatus = "Deleted";
       		String tempName = ((TaskManage)MainWindow.taskDeletedList.get(i)).fileName;
    		long tempLength =  ((TaskManage)MainWindow.taskDeletedList.get(i)).totalDownLength;
    		    		
    		Object[] info = {tempStatus,tempName,tempLength,""};
			MainWindow.tableDeletedModel.addRow(info);
       	}
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -