- private void saveDataToFile(String fileName,String data) {
- BufferedWriter writer = null;
- File file = new File("d:\\"+ fileName + ".json");
- //如果文件不存在,则新建一个
- if(!file.exists()){
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //写入
- try {
- writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
- writer.write(data);
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- try {
- if(writer != null){
- writer.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- System.out.println("文件写入成功!");
- }
取数据
- private String getDatafromFile(String fileName) {
- String Path="d:\\" + fileName+ ".json";
- BufferedReader reader = null;
- String laststr = "";
- try {
- FileInputStream fileInputStream = new FileInputStream(Path);
- InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
- reader = new BufferedReader(inputStreamReader);
- String tempString = null;
- while ((tempString = reader.readLine()) != null) {
- laststr += tempString;
- }
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return laststr;
- }