Reputation: 275
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();
Upvotes: 1
Views: 16108
Reputation: 1
I think you can create an object from the Path interface using a relative path and getting that path using the Paths class using the static get method. once done you can get the absolute path from the created object and use it as a string if you prefer.
I hope I have helped you.
Path path1 = Paths.get("res/ficheroPrueba.txt");
File file = new File(path1.toAbsolutePath().toString());
Upvotes: 0
Reputation: 1666
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?
Upvotes: 4