简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM