栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java大作业——目录下文件监测应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java大作业——目录下文件监测应用

具体题目:

检查某目录文件是否有变动,如有变化,显示变化信息
 * 应用界面包括1个文本框,2个按钮,1个文本域
 * (1)安排一个文本框显示filedialog选取到的目录信息
 * (2)安排一个按钮,点击则记录目录下现有的文件信息,写入到一个记录文件中
 * (3)安排一个按钮进行检查,点击将检查结果显示在文本域中


package bisu;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Date;
import java.util.Scanner;

public class FileDiaLog  extends Frame implements ActionListener{
	
	TextField info = new TextField("目录信息",20);
	Button writeInfo = new Button("显示目录信息");
	Button writeInfo1 = new Button("信息写入文件");
	Button check = new Button("检查");
	Button quit = new Button("close");
	TextArea info1 = new TextArea("");
	File f;
	
	public FileDiaLog(File f0) {
		super("FileDialog");
		f = f0;
		setLayout(new GridBagLayout());
		GridBagConstraints g = new GridBagConstraints();
		addToBag(writeInfo, g, 0, 0, 4, 1);
		addToBag(writeInfo1, g, 0, 6, 4, 1);
		addToBag(check, g, 0, 1, 4, 1);
		addToBag(info, g, 0, 2, 2, 1);
		addToBag(quit, g, 0, 3, 1, 1);
		addToBag(info1, g, 0, 4, 1, 1);
		writeInfo.addActionListener(this);
		writeInfo1.addActionListener(this);
		check.addActionListener(this);
		quit.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==writeInfo) {
			info.setText(f.getAbsolutePath());
		} 
		
		
		//将检查结果写在文本域
		if (e.getSource()==check) {                        
			File f1 = new File ("C:\Users\ksk\Desktop\mg1.txt");
			InputStreamReader file;
			try {
				file = new InputStreamReader(new FileInputStream(f1),"GBK");
				LineNumberReader in=new LineNumberReader(file);
				boolean eof=false;
				String s = "";
	            while(!eof){
	            	String x = in.readLine();
	                if(x==null)
	                    eof=true;
	                else {
	                	s = s + x + "rn";
	                }
	            }
	            info1.setText(s);
				in.close();
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
			
			 //将目录信息写入文件
		if (e.getSource()==writeInfo1) {                   
			File [] fs = f.listFiles();
			File f0 = new File ("C:\Users\ksk\Desktop\mg1.txt");
			//文件内容清空
			try {
				OutputStreamWriter f3 = new OutputStreamWriter(new
				        FileOutputStream(f0), "GBK");
				f3.write("");
				f3.close();
			} catch (UnsupportedEncodingException e2) {
				e2.printStackTrace();
			} catch (FileNotFoundException e2) {
				e2.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			//将信息写入文件
			for (int i=0;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918921.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号