星空网 > 软件开发 > 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   @Override13   public boolean accept(File dir, String name)14   {15     if(name.endsWith(".classpath"))16     {17       return true;18     }else19     {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 }

 




原标题:Java I/O文件的过滤 、读取、写入

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流