「java文件变化」怎么把文本文件变java文件
本篇文章给大家谈谈java文件变化,以及怎么把文本文件变java文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
Java 如何监控文件目录的变化
JavaSE 1.7提供了相关的API,去监视文件或者文件夹的变动,主要的API都在java.nio.file下面,其大概流程如下:
package org.xdemo.superutil.j2se.filewatch;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
/**
* 文件夹监控
*
* @author Goofy a href="";/a
* @Date 2015年7月3日 上午9:21:33
*/
public class WatchDir {
private final WatchService watcher;
private final MapWatchKey, Path keys;
private final boolean subDir;
/**
* 构造方法
*
* @param file
* 文件目录,不可以是文件
* @param subDir
* @throws Exception
*/
public WatchDir(File file, boolean subDir, FileActionCallback callback) throws Exception {
if (!file.isDirectory())
throw new Exception(file.getAbsolutePath() + "is not a directory!");
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMapWatchKey, Path();
this.subDir = subDir;
Path dir = Paths.get(file.getAbsolutePath());
if (subDir) {
registerAll(dir);
} else {
register(dir);
}
processEvents(callback);
}
@SuppressWarnings("unchecked")
static T WatchEventT cast(WatchEvent? event) {
return (WatchEventT) event;
}
/**
* 观察指定的目录
*
* @param dir
* @throws IOException
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
keys.put(key, dir);
}
/**
* 观察指定的目录,并且包括子目录
*/
private void registerAll(final Path start) throws IOException {
Files.walkFileTree(start, new SimpleFileVisitorPath() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
register(dir);
return FileVisitResult.CONTINUE;
}
});
}
/**
* 发生文件变化的回调函数
*/
@SuppressWarnings("rawtypes")
void processEvents(FileActionCallback callback) {
for (;;) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
System.err.println("操作未识别");
continue;
}
for (WatchEvent? event : key.pollEvents()) {
Kind kind = event.kind();
// 事件可能丢失或遗弃
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
// 目录内的变化可能是文件或者目录
WatchEventPath ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
File file = child.toFile();
if (kind.name().equals(FileAction.DELETE.getValue())) {
callback.delete(file);
} else if (kind.name().equals(FileAction.CREATE.getValue())) {
callback.create(file);
} else if (kind.name().equals(FileAction.MODIFY.getValue())) {
callback.modify(file);
} else {
continue;
}
// if directory is created, and watching recursively, then
// register it and its sub-directories
if (subDir (kind == StandardWatchEventKinds.ENTRY_CREATE)) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
}
}
}
boolean valid = key.reset();
if (!valid) {
// 移除不可访问的目录
// 因为有可能目录被移除,就会无法访问
keys.remove(key);
// 如果待监控的目录都不存在了,就中断执行
if (keys.isEmpty()) {
break;
}
}
}
}
}
java 如何监控文件夹的变化
jdk7 新增了WatchService,就是监控文件夹变化的。
可以监控 新增、改变、删除等.
如果不能用 jdk7,就需要自己判断了,得到目录文件,每隔几秒,再次检测,是否有增加、删除、改变
JAVA查看文件夹中变化的文件
可以,不过开销会比较大,需要不间断地检查文件夹,以下是个例子,比较简单,需要判断的很多东西都不写了
public void detect() {
ListString fileNames = new ArrayListString();
ListLong lastModifyTime = new ArrayListLong();
while(true) {
try {
File f = new File("D:\detect"); //需要检查的目录
File[] files = f.listFiles();
for(File file : files) {
boolean found = false;
for(String s : fileNames) {
if(s.equals(file.getName()) {
found = true;
if(file.lastModified() != lastModifyTime.get(fileNames.indexOf(s)).longValue()) {
System.out.println(file.getName() + " is modified");
}
break;
}
if(!found) {
System.out.println(file.getName() + " is created");
fileNames.add(file.getName());
lastModifyTime.add(new Long(file.lastModified());
}
}
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
JAVAcopy文件变小了是什么原因
原因:执行环境中的JRE版本与被执行代码的class文件不兼容,一本是因为编译Java代码时使用的JRE版本比代码运行环境的JRE版本要高。我们所遇到的问题正式这样,有的同学的Mac机上模式使用的Java 7,而远程Linux服务器上我们是默认使用Java 6.
Java版本的主版本号如下:
J2SE 8 = 52,
J2SE 7 = 51,
J2SE 6.0 = 50,
版权声明:本文为CSDN博主「常一二」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:
java文件变化的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于怎么把文本文件变java文件、java文件变化的信息别忘了在本站进行查找喔。