我想在java中读取bin文件。我在项目文件夹中创建了名称为doc的文件夹,并在doc文件夹中添加了bin文件。我想阅读这段代码;
String fileName = "10.bin";
byte[] data = new byte[1024];
int readBytes;
try {
FileInputStream in = new FileInputStream(fileName);
while((readBytes = in.read(data)) != -1){
System.out.println("read " + readBytes + "bytes, and placed them into temp array named data");
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这个名为10.bin的bin文件位于doc文件夹中,如图所示
我无法使用此代码访问文件。请帮帮我。
答案 0 :(得分:0)
首先你必须给出fileName的路径
File fileName = new File("your fileName path goes here");
FileInputStream in = new FileInputStream(fileName);