-4

编写程序从文件夹中的所有文件中读取字节...

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 = 

那么为什么不打印其他文件呢?

4

2 回答 2

0

You are passing in just the file name to get the Path of the file which Is looking in the wrong directory for the file.

Try this

 Path filesin1= Paths.get(filesin.getAbsolutePath());

instead of

 Path filesin1= Paths.get(filesin);

If you want to check add these prints

 System.out.println(filesin.getAbsolutePath());
 System.out.println(filesin1.toString());

EDIT Do these changes an let me know the output

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();
  }
}
于 2013-07-08T00:45:24.470 回答
0

问题是您601688450_eBill_20130708.pdf在应该尝试访问时尝试访问07072013/601688450_eBill_20130708.pdf

利用Path filesin1 = new File(folder, filesin).toPath()

于 2013-07-08T01:01:30.557 回答