zoukankan      html  css  js  c++  java
  • java写入内容到本地文件 -读取文件内容

    /** 日志记录
         * @author sys
         * @param content 要写入的类容
         * @param path 目标路径 c:/log/
         * @param filename 文件名 log.txt
         */
        public static void writeFileToLoaction(String content,String path,String filename) {
            try {
                File file = new File(path);
                if (!file.exists()) {
                    file.mkdirs();
                }
                File f = new File(path+"//"+filename);
                if(!f.exists())
                    f.createNewFile();
                
                FileWriter fw = new FileWriter(f.getAbsoluteFile(),true);//追加
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(content+"
    
    
    ");
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /**
         * 读文件 text
         */
        public static String getStringstrByLocation(String filepath) {
            StringBuilder bufferstr=new StringBuilder();
            InputStreamReader contentfile = null ;
            BufferedReader Inputfile = null ;
            try {
                File f = new File(filepath);
                if (!f.exists()) {
                    f.createNewFile();
                }
                 contentfile = new InputStreamReader(
                        new FileInputStream(f), "UTF-8");
                 Inputfile = new BufferedReader(contentfile);
                String filerecord = "";
                while ((filerecord=Inputfile.readLine()) != null) {
                    bufferstr.append(filerecord);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }finally{
                try {
                    Inputfile.close();
                    contentfile.close();
                } catch (IOException e) {
                    //e.printStackTrace();
                }
            }
            return bufferstr.toString();
        }
    
        public static void main(String[] args) {
            System.out.println(getStringstrByLocation("c:/log/123.txt"));
        }
    
    
    
    
    
  • 相关阅读:
    配置对即时负载的优化
    通过重组索引提高性能
    使用索引视图提高性能
    sqlcmd
    (转)使用SQLCMD在SQLServer执行多个脚本
    在SQLServer处理中的一些问题及解决方法 NEWSEQUENTIALID()
    java反射机制与动态代理
    天天用的开发环境,你真的了解吗?
    通过IP获取对应所在地的地址
    unity3d KeyCode各键值说明
  • 原文地址:https://www.cnblogs.com/syscn/p/7742251.html
Copyright © 2011-2022 走看看