编写程序以从文件夹中的所有文件中读取字节...
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 =
那么为什么不打印其他文件呢?
答案 0 :(得分:0)
您只是传入文件名以获取文件的路径,该文件正在查找文件的错误目录。
试试这个
Path filesin1= Paths.get(filesin.getAbsolutePath());
而不是
Path filesin1= Paths.get(filesin);
如果要检查添加这些打印件
System.out.println(filesin.getAbsolutePath());
System.out.println(filesin1.toString());
编辑这些更改让我知道输出
public class xmlfile {
public static void main(String[] args) throws IOException {
File folder = new File ("07072013");
File[] listOfFiles = folder.listFiles();
//ADD THIS
System.out.println("There are " + listOfFiles.length + " files");
String filesin;
BufferedWriter xmlfile = null;
// PUT THIS OUTSIDE YOUR LOOP
xmlfile = new BufferedWriter(new FileWriter(outxmlfile));
String outxmlfile = ("07072013\\" + "jayraj" + ".xml");
int offset = 0;
int size = 0;
for (int i = 0; i < listOfFiles.length; i++) {
//ADD THIS
File f = listOfFiles[i];
// AND THIS
System.out.println(i + " " + f.getAbsolutePath());
if (f.isFile()) {
filesin = listOfFiles[i].getName();
if (filesin.endsWith("pdf")) {
System.out.println(filesin);
//CHANGE THIS
Path aPath = Paths.get(f.getAbsolutePath());
// Change this
System.out.println(filesin1.toString());
// CHANGE THIS what you got to work
byte[] bytes = filesin.getBytes(); // This is actually just getting the bytes of the String probably not what you want
byte[] actualBytes = Files.readAllBytes(aPath);
size = actualBytes.length;
//EDIT THIS (ADD NEWLINE CHAR AT THE END)
xmlfile.append("File = " + filesin1 + ", Offset = " + offset + ", Size = " + size + "\n");
}
}
}
xmlfile.close();
}
}
答案 1 :(得分:0)
问题是,当您尝试访问601688450_eBill_20130708.pdf
时,您尝试访问07072013/601688450_eBill_20130708.pdf
。
使用Path filesin1 = new File(folder, filesin).toPath()