简体   繁体   English

Java FileInputStream错误:“没有这样的文件或目录”

[英]Java FileInputStream error: “No such file or directory”

I'm writing a java code to go through a .sfo (a combination of SQL and Fortran) file and remove a certain set of characters whenever they show up in the file. 我正在编写Java代码来遍历.sfo(SQL和Fortran的组合)文件,并在文件中显示某些字符时将其删除。 I'm using Eclipse on a 64-bit Windows 7 machine, if that makes any difference. 我在64位Windows 7计算机上使用Eclipse,如果有任何区别的话。 The code is doing what I want, removing the blocks of characters and whatnot, but at the end, after it gives me my output, it shows "Error: No such file or directory." 该代码正在执行我想要的操作,删除了字符块和其他内容,但是最后,在给我输出之后,它显示“错误:没有这样的文件或目录”。 I don't know why; 我不知道为什么; the only external file that I am referencing is the aforementioned .sfo. 我要引用的唯一外部文件是上述.sfo。 The file exists, and the file path that I specified in the code is correct. 该文件存在,并且我在代码中指定的文件路径正确。 I have permissions to read and write to the file. 我有权读取和写入文件。 Here is my code (more or less; a lot of it is repetitive, so I'll cut out some of the unimportant stuff): 这是我的代码(或多或少;很多都是重复的,因此我将删去一些不重要的内容):

The absolute path is 绝对路径是

C:/Users/frencke/p4/frencke_LOHEPCE00294173/pcs/main/lib/gp/file.sfo. C:/用户/frencke/p4/frencke_LOHEPCE00294173/pcs/main/lib/gp/file.sfo。

Yes, I have full permissions on the file. 是的,我对该文件拥有完全权限。

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class StringSplit {
    public static void main(String args[]) {
        try {
            ArrayList<String> arr = new ArrayList<String>();
            // Here I initialized a bunch of ArrayLists; nothing relevant
            ArrayList<String> arr26 = new ArrayList<String>();
            FileInputStream fstream = new FileInputStream(
                    "C:/Users/.../file.sfo");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                arr.add(strLine);
                String[] temp;
                String delimiter = "\\s+\\s+\\s+\\s+\\s+&\\s+";
                temp = strLine.split(delimiter);
                for (int i = 0; i < temp.length; i++)
                    arr2.add(temp[i]);
                // Here I did all of the removal of the various blocks of text
                String[] temp27;
                String delimiter27 = "\t9";
                String strLine27 = null;
                for (int i = 0; i < temp26.length; i++)
                    strLine27 = temp26[i];
                temp27 = strLine27.split(delimiter27);
                for (int i = 0; i < temp27.length; i++)
                    System.out.println(temp27[i]);
                in.close();
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

Again, the error message I got was: "Error: No such file or directory." 同样,我得到的错误消息是:“错误:没有这样的文件或目录。” If anyone knows why this is happening, I would love to hear it; 如果有人知道为什么会这样,我很想听听。 thanks! 谢谢!

You are closing the InputStream at the end of the first iteration of your while loop - this releases any system resources associated with the stream. 您将在while循环的第一次迭代结束时关闭InputStream-这将释放与该流关联的所有系统资源。

When you try to readLine(), the stream has already been released so that's why it says No such file exists. 当您尝试读取Line()时,流已经被释放,因此它说不存在这样的文件。

I think you meant to put the in.close() after the loop, that should work. 我认为您打算将in.close()放在循环之后,这应该可以工作。

尝试提供这样的文件路径“ C:\\\\ Users \\\\ ... \\\\ file.sfo”

Just remove "/" character with File.separator . 只需使用File.separator删除“ /”字符。 Example: 例:

String path = "C:/Users/.../file.sfo";
path = path.replaceAll("//",File.separator);
FileInputStream fstream = new FileInputStream(path);

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

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