zoukankan      html  css  js  c++  java
  • File相关操作

    文件操作

    流关闭方法

    public static void closeQuietly(Closeable closable)
    {
        if (null == closable)
        {
            return;
        }
        
        try
        {
            closable.close();
        }
        catch (IOException e)
        {
        }
    }
    

    读文件

    下面给出的是通用的读文件方法,获取到每行数据后,可以对每行数据进行处理

    public static String read(String fileName) throws IOException
    {
        file = new File(fileName);
        StringBuffer sb = new StringBuffer();
        
        BufferedReader br = null;
        try
        {
            br = new BufferedReader(new FileReader(file));
            
            String line = null;
            
            while ((line = br.readLine()) != null)
            {
                //获取到每行数据后,可以进行一些处理
                //如,读取的是配置文件格式为key=value,则可以在此处理
                //如将key和value解析后保存到map中,供其他地方调用
                
                sb.append(line);
                sb.append('
    ');
            }
            
            return sb.toString().trim();
        }
        finally
        {
            close(br);
        }
    }
    

    写文件

    可以通过最后一个参数设置,如果存在同名文件是直接覆盖还是追加

    /**
     * 可以追加写文件
     * @author 
     * @param append 是否追加写文件
     * @param fileName
     * @param s
     * @throws IOException
     */
     public static void write(boolean append, String fileName, String s)
            throws IOException
    {
        File file = new File(fileName);
        
        if (file.getParent() != null)
        {
            file.mkdirs(file.getParent());
        }
        
        if (file.exists())
        {
            String content = read(fileName);
            
            if (content.equals(s))
            {
                return;
            }
        }
        
        BufferedWriter bw = null;
        try
        {
            FileWriter fileWriter = null;
            if (append)
            {
                fileWriter = new FileWriter(file, true);
            }
            else
            {
                fileWriter = new FileWriter(file);
            }
            bw = new BufferedWriter(fileWriter);
            bw.flush();
            bw.write(s);
        }
        finally
        {
            close(bw);
        }
    }
    

    获取目标路径下的文件名

    public static String[] listFiles(String fileName) throws IOException
    {
        if (null == fileName || fileName.isEmpty())
        {
            return new String[0];
        }
    
        File file = new File(fileName));
        
        List files = new ArrayList();
    
        File[] fileArray = file.listFiles();
    
        for (int i = 0; (fileArray != null) && (i < fileArray.length); i++)
        {
            if (fileArray[i].isFile())
            {
                files.add(fileArray[i].getName());
            }
        }
    
        return (String[]) files.toArray(new String[files.size()]);
    }
    
  • 相关阅读:
    9.8 查找表
    LeetCode——Permutations
    利用rman自己主动备份转储spfile
    linux下非root用户怎样改动root权限的文件
    做一个有主见的女生
    APP-FND-00676: 弹性域例程 FDFGDC 无法读取为此说明性弹性域指定的默认引用字段
    矩阵高速幂模板篇
    Index statistics collected bug
    位运算
    poj 1190 生日蛋糕 , 强剪枝
  • 原文地址:https://www.cnblogs.com/z00377750/p/9355736.html
Copyright © 2011-2022 走看看