user4559332
user4559332

Reputation:

cannot read file from a directory

i have a program that create some files, puts them into a directory, then read them and sends them to a receiver. after each sending of a file it gets deleted. however after the first pack of files gets send the program cant read any other files and for each new file i am getting this error:

java.io.FileNotFoundException: UBX_MSG.bin (The system cannot find the file specified)

each time i read the filers i check whether they actually exists and the method always returns true.

can any one shed some light on this problem? any help would be appreciated. thank you. here is my functions, one is reading the file and one is sending it.

public void push2rec (File[] LOF){
    try {        
        for (File f : LOF){
            System.out.println(f.exists());
            byte[] rd = read(f.getName());
            SP.writeBytes(rd);
            f.delete();
        } 
    }
    catch (SerialPortException ex) {System.out.println(ex);} 
}

public static byte[] read(String name){
    File file = new File(name);     
    byte[] bytes = new byte[(int) file.length()];
    try {
        FileInputStream inputStream = new FileInputStream(file);
        inputStream.read(bytes);
        inputStream.close(); 
    } 
    catch (FileNotFoundException ex) {System.out.println(ex);} 
    catch (IOException ex) {System.out.println(ex);}

    return bytes;
}

Upvotes: 0

Views: 1073

Answers (1)

Turo
Turo

Reputation: 4914

f.getName() retuns only the filename without the path. Use f.getAbsolutePath().

Upvotes: 2

Related Questions