一、文件的過濾
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 }