10.读取固定路径下的文件的一种方法:
private static String getJson() throws Exception {
FileInputStream inputStream = new FileInputStream("c://video.json");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String str = "";
StringBuffer json = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
json.append(str);
}
bufferedReader.close();
inputStream.close();
return json.toString();
}
11.将数据写入一个已知的文件中:
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
String currenttime=simpleDateFormat.format(date);
String jsondataPath=year+"/"+month+"/"+day;
//String jsondataPath = "D://" + year + "//" + month + "//" + day;
String filename = currenttime + ".json";
先创建文件夹,然后开始读写操作,
File file = new File(jsondataPath);
if (!file.exists()) {
file.mkdirs();
}
File file2 = new File(jsondataPath, filename);
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
FileOutputStream writerStream = new FileOutputStream(fileEntity);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
writer.write(jsondata);
writer.close();