자바 IO 흐름 을 통 해 파일 저장 및 읽 기

9848 단어
package hy.ea.util.savereadfile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 *    :lnn
 *     :2018-6-7
 *   :        (json   )
 */
public class JsonCache {




    /**
     * //    
     * @param fileName     
     * @param data json   
     */
    public void saveDataToFile(String fileName,String data){
        BufferedWriter writer = null;
        String destDirName="d:\\jsonfile";
        File dir = new File(destDirName);
        if (dir.exists()) {
            System.out.println("    " + destDirName + "  ,        ");
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //    
        if (dir.mkdirs()) {
            System.out.println("    " + destDirName + "  !");
        } else {
            System.out.println("    " + destDirName + "  !");
        }
        File file = new File(destDirName+ fileName + ".txt");
        System.out.println(file+"file");
        //       ,     
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(fileName + ".txt     ");
        }
        //  
        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("      !");
    }


    /**
     * //    
     * @param fileName     
     */
    public String (String fileName) {

        //               ,         File  
        File file = new File("d:\\jsonfile\\" + fileName+ ".txt");
        if(!file.exists()){
            //throw new RuntimeException("         ");
            return null;
        }
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            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();
                }
            }
        }
        System.out.println("      ");
        return laststr;
    }



}

좋은 웹페이지 즐겨찾기