-4

Write program to read bytes from all the files in folder...

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();
    }
}

I am getting error..

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)

And System.out.println only prints one line. If there is 5 files in the folder, it should write 5 lines?

I tried without specifying bytes and size, which gave:

File = 601693971_eBill_20130708.pdf, Offset = 0, Size = 

So why aren't the other files being printed as well?

x4nd3r
  • 855
  • 1
  • 7
  • 20
user2559055
  • 73
  • 3
  • 11
  • [`NoSuchFileException`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/NoSuchFileException.html)s are thrown when the target file does not exist. Are you 100% sure that `601688450_eBill_20130708.pdf` exists? – Jeffrey Jul 08 '13 at 00:22
  • ya, there is 7 files in folder, and 5 pdf files its include this file as well..I don't what's wrong please help me thanks!! – user2559055 Jul 08 '13 at 00:26
  • can you fix the spacing in your code so that it's easier for us to read please? – Mike 'Pomax' Kamermans Jul 08 '13 at 00:29
  • hey, I already remove some spacing so..it's ok?? please let me know!! Thanks!! – user2559055 Jul 08 '13 at 00:32
  • Why not include a `dir`/`ls` listing of the directory in question? – Hot Licks Jul 08 '13 at 00:41
  • Maybe, just maybe you need to specify WHERE the files are when you attempt to open them. – Hot Licks Jul 08 '13 at 00:47
  • actually, i tried;; byte[] bytes = filesin.getBytes(); size = bytes.length; and its worked but it only print one line for 1 file what about other files?? File = 601693971_eBill_20130708.pdf, Offset = 112, Size = 28 what about other files?? – user2559055 Jul 08 '13 at 01:05

2 Answers2

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();
  }
}
Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • I tried file.toURI() Getting error rename all file.. can't fix that error.. how to do that?? please help me thanks!! – user2559055 Jul 08 '13 at 00:49
  • actually, i tried;; byte[] bytes = filesin.getBytes(); size = bytes.length; and its worked but it only print one line for 1 file what about other files?? File = 601693971_eBill_20130708.pdf, Offset = 112, Size = 28 what about other files?? – user2559055 Jul 08 '13 at 01:06
  • Not sure about the error you are getting with the method I posted above. Are all the files in your folder pdf's? and lowercase pdf – Java Devil Jul 08 '13 at 01:09
  • yap.. there is 5 files in folder it should write 5 lines right?? for all 5 files!! I don't know why its not working!! please help me.. Thanks!! – user2559055 Jul 08 '13 at 01:11
  • Look at my edit above, add the changes and let me know the console output – Java Devil Jul 08 '13 at 01:51
  • Hey, I have question.. Its worked!! Thank you soo much!! But Above code i wrote that size it was 28.. and your is 111419 i think yours is correct right?? Thanks once again!! – user2559055 Jul 08 '13 at 22:37
  • Yes thats better. 28 is the length of the String `601693971_eBill_20130708.pdf` meaning it was just the bytes of the String. (1 byte per character) – Java Devil Jul 08 '13 at 22:40
  • ohh ok!!YES thats better?? what that mean?? like yours is correct or mine?? Thank you so much so mine says byte file name right?? your bytes says in file right?? – user2559055 Jul 08 '13 at 22:52
  • Yes, Mine is the bytes read from the file, yours was just the bytes of the file name, not what was in the file. – Java Devil Jul 08 '13 at 22:58
  • Hey, Is There any way to write all bytes in text file write 111419 bytes?? Please help me thanks!! – user2559055 Jul 09 '13 at 16:47
  • I suggest you read these [JavaIO tutorials](http://docs.oracle.com/javase/tutorial/essential/io/). In particular this one about [Reading, writing and creating files](http://docs.oracle.com/javase/tutorial/essential/io/file.html) or just look at this [question](http://stackoverflow.com/questions/4350084/byte-to-file-in-java) – Java Devil Jul 09 '13 at 19:58
0

The problem is that you are trying to access 601688450_eBill_20130708.pdf when you should be trying to access 07072013/601688450_eBill_20130708.pdf.

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

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • actually, i tried;; byte[] bytes = filesin.getBytes(); size = bytes.length; and its worked but it only print one line for 1 file what about other files?? File = 601693971_eBill_20130708.pdf, Offset = 112, Size = 28 what about other files?? – user2559055 Jul 08 '13 at 01:07