JAVA IO流简单读写文件

来源:互联网 发布:网络管理工具 编辑:程序博客网 时间:2024/06/06 16:34
//加载本地缓存
privateString okGetBenDiRequse(String path)throws Exception {
//此文件已经存在
File file =new File(path,"json.txt");

//文件读取流(输入流)
FileInputStream inputStream =new FileInputStream(file);
//这个类是android 中的类
ByteArrayOutputStream outputStream =new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
intlen=0;
while((len=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}

//不要忘记关流哦,注意关流顺序
outputStream.close();
inputStream.close();

returnoutputStream.toString();
}

//缓存
private voidwriteJsonToFile(InputStream stream) throws IOException {
//将数据写到文件中
File file =new File(cachedirectory);
file.mkdirs();
File file1 =new File(file,"json.txt");
file1.createNewFile();
FileOutputStream outputStream =new FileOutputStream(file1);
byte[] bytes = new byte[1024];
intlen=0;
while((len=stream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}

outputStream.close();
stream.close();

}
0 0
原创粉丝点击