一、文件的过滤
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();//关闭流 } }
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号