[英]Open file with absolute path in java
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.当
path
(在构造函数中给出)是相对的时,它工作正常。 But I would like it to work in an absolute path instead.但我希望它在绝对路径中工作。 I looked up in java
File
class docs but got confused.我在 java
File
类文档中查找,但感到困惑。 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();
In your code here;在您的代码中;
File file = new File(path);
Your path
String variable just needs to be absolute instead of relative.您的
path
字符串变量只需要是绝对的而不是相对的。
I don't see why it would not work.我不明白为什么它不起作用。 Did you try to update the path variable to absolute path of your file?
您是否尝试将路径变量更新为文件的绝对路径?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.