简体   繁体   中英

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. I'm using Eclipse on a 64-bit Windows 7 machine, if that makes any difference. 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. 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.

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.

When you try to readLine(), the stream has already been released so that's why it says No such file exists.

I think you meant to put the in.close() after the loop, that should work.

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

Just remove "/" character with File.separator . Example:

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

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