zoukankan      html  css  js  c++  java
  • Java I/O文件的过滤 、读取、写入

    一、文件的过滤

     1 public class guolv implements FilenameFilter
     2 {
     3     public static void main(String[] args)
     4     {
     5         File file = new File("F:\java\workspace\Fanshe");//找到文件路径
     6         String[] files = file.list(new guolv());//把稳建议数组的方式打开
     7         System.out.println(files[0] + "===");
     8         
     9     }
    10     //返回值为true则说明文件符合要求求
    11     //返回值为false则说明文件不符合要求
    12     @Override
    13     public boolean accept(File dir, String name)
    14     {
    15         if(name.endsWith(".classpath"))
    16         {
    17             return true;
    18         }else
    19         {
    20             return false;
    21         }
    22     }
    23 }

    二、文件的读取

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

    三、文件的写入

     1 public class output
     2 {
     3     public static void main(String[] args) throws Exception
     4     {
     5         File file = new File("F:\a.txt");
     6         FileOutputStream out = new FileOutputStream(file);
     7         out.write("abmin".getBytes());
     8         out.flush();//清楚缓存
     9         out.close();//关闭流
    10     }    
    11 }
  • 相关阅读:
    使div浮动层显示在Select组件上面
    JS中,执行字符串的函数
    如何让Web自定义控件(WebCustomControl)能够被 验证控件 验证
    WIN 2003 中 IIS MIME 问题,导致FLV无法播放
    Response.ContentType的所有類型
    XPath语法
    如何加快页面加载速度
    网页页面实现自动刷新的3种代码
    div 内table 居中
    C#中调用Windows API的要点
  • 原文地址:https://www.cnblogs.com/cyg-06/p/5972647.html
Copyright © 2011-2022 走看看