例如在项目中有一个包,包名是: myDir,这个包有一个1.txt文件:
1.首先编写释放方法:
public static void inputStreamToFile(InputStream inputStream, String targetFilePath)
{
File file = new File(targetFilePath);
try
{
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1)
{
os.write(buffer, 0, bytesRead);
}
os.flush();
os.close();
inputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
2.指定释放的资源:
inputStreamToFile(getClass().getResourceAsStream("../myDir/1.txt"), "c:/备用/2.txt");