我该怎么做?
File myFile = getFile(this.getClass.getResourceAsStream("test.txt"))
OR
File myFile = getFile(this.getClass.getResource("test.txt"))
我将在getFile()
方法中编写哪些代码?
答案 0 :(得分:4)
这是功能:
public File getFile(FileInputStream inStream){
File file =new File("file.txt");
OutputStream outStream = null;
outStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
outStream.close();
return file;
}
答案 1 :(得分:3)
哟可以这样做
File f = new File(this.getClass().getResource("test.txt").toURI());
System.out.println(f);
输出(我的Eclipse测试)
D:\workspace1\x\target\classes\text.txt
但它总是不起作用。如果你的资源在一个罐子里怎么办?你会得到
java.lang.IllegalArgumentException: URI is not hierarchical