This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable,
visit the help center。
7年前关闭。
我正在自学NIO2,并做一些练习来测试理论。目前,我已经实现了FileVisitor的实现,该实现可以编译并运行,并且可以在fileVisit中实现我期望的功能,但在postFileVisit中不能实现。这是因为它对.xml和.xhtml文件中的表行进行计数,并使用结果创建一个文本文件,但是它不附加给定目录的表行总数。 postFileVisit确实可以做一些事情,但是,如果目录中没有.xml或.xhtml文件,则仍然会创建一个日志文件并给它加上时间戳,但是代码行97至101似乎没有效果。因此,我相信我搞砸的是BufferedWriter。感谢您的帮助:
package com.purposeful_play.BasicIO;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.nio.file.*;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime;
/**
*
* @author Michael-Mosher
*/
public class CharacterCounter<T> implements FileVisitor<T> {
static int count = 0;
public static void main (String[] args){
Path path = FileSystems.getDefault().getPath(args[0]);
try {
Files.walkFileTree(path, new CharacterCounter<Path>());
}
catch (IOException x) { System.err.format("Unable to read file: %s%n", x); }
}
@Override
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException {
Path fname = (Path)file;
if((fname.getFileName().toString().contains(".xhtml"))||(fname.getFileName().toString().contains(".xml"))){
boolean withindiv = false;
int fcount = 0;
StringBuilder div = new StringBuilder("<div ID=center");
StringBuilder notdiv = new StringBuilder("</div");
StringBuilder table = new StringBuilder("<tr");
Charset cs = Charset.forName("UTF-8");
try (BufferedReader input = Files.newBufferedReader((Path)file, cs)){
while(input.ready()){
String line = input.readLine();
withindiv = line.contains(div) ? line.contains(div) : withindiv;
if(withindiv){
withindiv = !(line.contains(notdiv));
if(!withindiv){
line = line.split("</div")[0];
}
fcount = line.split("<tr", 0).length-1;
count += fcount;
}
}
}
Path path = (Path)file;
String ss = path.toString();
path = path.getParent().resolve("logfile.txt");
boolean newfile = Files.exists(path);
try (BufferedWriter output = Files.newBufferedWriter(
path, cs, StandardOpenOption.CREATE,
StandardOpenOption.APPEND)){
output.write(ss);
output.newLine();
ss = new Integer(fcount).toString();
output.write(ss);
output.newLine();
long currentTime = System.currentTimeMillis();
FileTime ft = FileTime.fromMillis(currentTime);
if(!newfile)
Files.getFileAttributeView(path, BasicFileAttributeView.class).setTimes(ft, null, ft);
else
Files.getFileAttributeView(path, BasicFileAttributeView.class).setTimes(ft, null, null);
}
} // End if(...xhtml || ...xml)
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException {
System.err.printf("visitFileFailed error: %s%n", exc);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException {
Path path = (Path)dir;
path = path.resolve("logfile.txt");
Charset cs = Charset.forName("UTF-8");
BufferedWriter output = Files.newBufferedWriter(path, cs,
StandardOpenOption.CREATE, StandardOpenOption.SYNC, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
String ss = "Total occurences: ";
output.write(ss);
output.newLine();
ss = new Integer(count).toString();
output.write(ss);
output.newLine();
count = 0;
long time = System.currentTimeMillis();
FileTime ft = FileTime.fromMillis(time);
Files.setLastModifiedTime(path, ft);
return FileVisitResult.CONTINUE;
}
}