一种对文件读取和写入数据的方法

来源:互联网 发布:c语言集成开发环境功能 编辑:程序博客网 时间:2024/05/17 19:14
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();

原创粉丝点击