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

📄 page.java

📁 Learning automata Crawler
💻 JAVA
字号:
import java.io.*;
import java.util.ArrayList;

public class page {
	public String url;
	public String directory;
	public String fileLoc;
	public String fileName;
	public float state;
	
	public runtime runtime;
	
	public page(String Url) {
		url = Url;
		state = 0.5F;		
		
		/*
		 * Extract file and intended directory from the url
		 * 
		 * This is related to the crawler used so this might be modified
		 */
		directory = url.substring(7, url.lastIndexOf("/"));
		fileName = url.substring(url.lastIndexOf("/")+1, url.length());
		fileLoc = url.substring(7, url.length());
		fileLoc = fileLoc.replace('/', '\\');
	} 
		
	public float getState()
	{
		return state;
	}
	
	public void punish() {
		state -= 0.1F;
		if(state <0.1)
			state = 0.1F;
	}
	public void reward() {
		state += 0.1F;
		if(state>1.0)
			state = 1.0F;
	}
	
	public String getUrl() {
		return url;
	}
	public String getDir() {
		return directory;
	}
	public String getFileName() {
		return fileName;
	}
	
	/* Compare files in /old and /new folders to check if the site has been updated
	 * since the last visit
	 * 
	 * Use own compare function if wanted
	 */
	
	public boolean isUpdated() {
		
		runtime = new runtime();
		ArrayList<String> newFile, oldFile;
		String newFileName = "new\\"+fileLoc;
		String oldFileName = "old\\"+fileLoc;
		
		File file = new File(newFileName);

		newFile = readFile(file);

		file = new File(oldFileName);
		if(!file.exists())
		{
			backupFile();
			return true;
		}
		else oldFile = readFile(file);	
		
		if((newFile.toString().compareTo(oldFile.toString()) == 0))
			return false;
		else 
		{
			backupFile();
			return true;
		}
	}
	
	/*
	 * copies the file to the /old folder for comparison 
	 * 
	 */
	public void backupFile()
	{
		runtime.run("cmd /C mkdir old\\"+ directory);
		runtime.run("cmd /C copy new\\" + directory + " old\\"+directory);		
	}
	
	/*
	 * Reads the file and puts it in an ArrayList
	 */
	public ArrayList<String> readFile(File pFile)
	{
		ArrayList<String> list = new ArrayList<String>();
		File file = pFile;
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			while(br.ready())
			{
				list.add(br.readLine());
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return list;
	}
}

⌨️ 快捷键说明

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