Home  >  Article  >  类库下载  >  Filtering, reading and writing of JAVA I/O files

Filtering, reading and writing of JAVA I/O files

高洛峰
高洛峰Original
2016-10-19 09:54:321662browse

一、文件的过滤

public class guolv implements FilenameFilter
{
    public static void main(String[] args)
    {
        File file = new File("F:\\java\\workspace\\Fanshe");//找到文件路径
        String[] files = file.list(new guolv());//把稳建议数组的方式打开
        System.out.println(files[0] + "===");
        
    }
    //返回值为true则说明文件符合要求求
    //返回值为false则说明文件不符合要求
    @Override
    public boolean accept(File dir, String name)
    {
        if(name.endsWith(".classpath"))
        {
            return true;
        }else
        {
            return false;
        }
    }
}

 二、文件的读取

public class readers
{
    public static void main(String[] args) throws Exception
    {
        File file = new File("F:\\java\\workspace\\Fanshe\\src\\com\\cyg\\fanshe.java");//读取文件
        FileInputStream fi = new FileInputStream(file);//创建字节流,打开该 文件
        byte[] b = new byte[fi.available()];//fi.available 可以获取文件占多少字节
        int a = -1;
        while((a= fi.read(b))!= -1)//判断文件是否到达文件末尾
        {
            //System.out.println(new String(b));
        }
        System.out.println(new String(b));
        //关闭流
        fi.close();
        
    }
}


三、文件的写入

public class output
{
    public static void main(String[] args) throws Exception
    {
        File file = new File("F:\\a.txt");
        FileOutputStream out = new FileOutputStream(file);
        out.write("abmin".getBytes());
        out.flush();//清楚缓存
        out.close();//关闭流
    }    
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn