编写程序从文件夹中的所有文件中读取字节...
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class xmlfile {
public static void main(String[] args) throws IOException {
File folder = new File ("07072013");
File[] listOfFiles = folder.listFiles();
String filesin;
BufferedWriter xmlfile = null;
String outxmlfile = ("07072013\\" + "jayraj" + ".xml");
int offset = 0;
int size = 0;
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
filesin = listOfFiles[i].getName();
if (filesin.endsWith("pdf")) {
System.out.println(filesin);
Path filesin1= Paths.get(filesin);
System.out.println(filesin1);
xmlfile = new BufferedWriter(new FileWriter(outxmlfile));
byte[] bytes = Files.readAllBytes(filesin1);
size = bytes.length;
xmlfile.append("File = " + filesin1 + ", Offset = " + offset + ", Size = " + size);
}
}
}
xmlfile.close();
}
}
我收到错误..
601688450_eBill_20130708.pdf
601688450_eBill_20130708.pdf
Exception in thread "main" java.nio.file.NoSuchFileException: 601688450_eBill_20130708.pdf
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(Unknown Source)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.readAttributes(Unknown Source)
at java.nio.file.Files.readAttributes(Unknown Source)
at java.nio.file.Files.size(Unknown Source)
at java.nio.file.Files.readAllBytes(Unknown Source)
at xmlfile.main(xmlfile.java:50)
并且System.out.println
只打印一行。如果文件夹中有5个文件,应该写5行?
我试过没有指定字节和大小,它给出了:
File = 601693971_eBill_20130708.pdf, Offset = 0, Size =
那么为什么不打印其他文件呢?