zoukankan      html  css  js  c++  java
  • java指定路径写、读文件

    package com.util;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /**
    * @ClassName: IOUtill 
    * @Description: I/O工具类
    * @author 无名
    * @date 2016-5-20 下午9:00:18 
    * @version 1.0
     */
    public final class IOUtill 
    {
        private IOUtill(){}
        
        public static void writeByUrl(String url,String content)
        {
            File file = new File(url);
            if (!file.getParentFile().exists())
            {
               file.getParentFile().mkdirs();
            }
            try 
            {
               file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();    
            }
            try {
               FileWriter fw = new FileWriter(file, true);
               BufferedWriter bw = new BufferedWriter(fw);
               bw.write(content);
               bw.flush();
               bw.close();
               fw.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
        }
        
        public static String readByUrl(String url)
        {
            File file = new File(url);
            if (!file.getParentFile().exists())
            {
               file.getParentFile().mkdirs();
            }
            String content = "";
            try 
            {
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                try
                {
                    String line = br.readLine();
                    content += line;
                    while(line != null)
                    {
                        line = br.readLine();
                        content += line;
                    } 
                }catch (IOException ex) {
                    ex.printStackTrace();
                }
            }catch(FileNotFoundException e){
                e.printStackTrace();
            }
            return content;
        }
    }
  • 相关阅读:
    QK对中断的特殊处理
    程序控制的软件复位方法
    软件的按契约设计(DbC---Design by Contract)
    Arduino平台基于DbC的软件调试
    软件测试中的测不准原理
    关于嵌入式软件
    程序设计的SOLID原则
    CPS---(Cyber-Physical Sytem,信息物理融合系统)
    QP之QF原理
    QP之QEP原理
  • 原文地址:https://www.cnblogs.com/rixiang/p/5513597.html
Copyright © 2011-2022 走看看