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"));
        }
    
    
    
    
    
  • 相关阅读:
    SQL try catch处理
    JAVA学习书籍
    JAVA中jar包和war包的区别是
    tomcat webapps 是什么
    JAVA使用Gson解析json数据,实例
    java 返回json格式的数据
    目前常见的三种SQL分页方式:
    Eclipse如何新建一个tomcat_server发布web项目
    SQLHelper--java类
    编写jsp代码时出现的红色提示线错误
  • 原文地址:https://www.cnblogs.com/syscn/p/7742251.html
Copyright © 2011-2022 走看看