java写入内容到本地文件 -读取文件内容

/** 日志记录
     * @author sys
     * @param content 要写入的类容
     * @param path 目标路径 c:/log/
     * @param filename 文件名 log.txt
     */
    public static void writeFileToLoactionString content,String path,String filename) {
        try {
            File file = new Filepath);
            if !file.exists)) {
                file.mkdirs);
            }
            File f = new Filepath+"//"+filename);
            if!f.exists))
                f.createNewFile);
            
            FileWriter fw = new FileWriterf.getAbsoluteFile),true);//追加
            BufferedWriter bw = new BufferedWriterfw);
            bw.writecontent+"


");
            bw.close);
        } catch IOException e) {
            e.printStackTrace);
        }
    }
    /**
     * 读文件 text
     */
    public static String getStringstrByLocationString filepath) {
        StringBuilder bufferstr=new StringBuilder);
        InputStreamReader contentfile = null ;
        BufferedReader Inputfile = null ;
        try {
            File f = new Filefilepath);
            if !f.exists)) {
                f.createNewFile);
            }
             contentfile = new InputStreamReader
                    new FileInputStreamf), "UTF-8");
             Inputfile = new BufferedReadercontentfile);
            String filerecord = "";
            while filerecord=Inputfile.readLine)) != null) {
                bufferstr.appendfilerecord);
            }
        } catch Exception e) {
            e.printStackTrace);
            return "";
        }finally{
            try {
                Inputfile.close);
                contentfile.close);
            } catch IOException e) {
                //e.printStackTrace);
            }
        }
        return bufferstr.toString);
    }

    public static void mainString[] args) {
        System.out.printlngetStringstrByLocation"c:/log/123.txt"));
    }


Published by

风君子

独自遨游何稽首 揭天掀地慰生平