简体   繁体   English

无法从目录读取文件

[英]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) java.io.FileNotFoundException:UBX_MSG.bin(系统找不到指定的文件)

each time i read the filers i check whether they actually exists and the method always returns true. 每次读取文件管理器时,我都会检查它们是否确实存在,并且该方法始终返回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. f.getName()仅重新调整文件名,不包含路径。 Use f.getAbsolutePath(). 使用f.getAbsolutePath()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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