Open file with absolute path in java

user112112 :

I wrote this code to read the content of a file to a bytes array. It works fine when path (given in the constructor) is relative. But I would like it to work in an absolute path instead. I looked up in java File class docs but got confused. How can I changed it to work with absolute path?

        File file = new File(path);
        byte[] bytesArray = new byte[(int) file.length()];
        FileInputStream fis = new FileInputStream(file);
        fis.read(bytesArray);
        fis.close();
Shivam Puri :

In your code here;

File file = new File(path);

Your path String variable just needs to be absolute instead of relative.

I don't see why it would not work. Did you try to update the path variable to absolute path of your file?

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=389273&siteId=1