一、获取文件列表
方法一:DirFilter类实现了FilenameFilter接口,需要重写FilenamFilter中的accept方法。
1 /** 2 * java文件操作 获取文件列表 3 * 2016/6/15 4 */ 5 package fileStream; 6 7 import java.io.File; 8 import java.io.FilenameFilter; 9 import java.util.regex.*; 10 import java.util.*; 11 12 public class DirList { 13 public static void main(String[] args){ 14 File path = new File("G:"); 15 String[] list; 16 if(args.length == 0){ 17 list = path.list(); 18 }else{ 19 list = path.list(new DirFilter(args[0])); 20 } 21 Arrays.sort(list, String.CASE_INSENSITIVE_ORDER); 22 for(String dirItem:list){ 23 System.out.println(dirItem); 24 } 25 } 26 } 27 28 class DirFilter implements FilenameFilter { 29 private Pattern pattern; 30 public DirFilter(String regex){ 31 pattern = Pattern.compile(regex); 32 } 33 @Override 34 public boolean accept(File dir, String name) { 35 return pattern.matcher(name).matches(); 36 } 37 }
方法二:使用匿名内部类
1 /** 2 * 获取文件列表方法2 匿名内部类 3 * 2016/6/15 4 */ 5 package fileStream; 6 7 import java.io.*; 8 import java.util.*; 9 import java.util.regex.*; 10 11 12 public class DirList2 { 13 public static FilenameFilter filter(final String regex){ 14 return new FilenameFilter(){ 15 private Pattern pattern = Pattern.compile(regex); 16 @Override 17 public boolean accept(File dir, String name){ 18 return pattern.matcher(name).matches(); 19 } 20 }; 21 } 22 public static void main(String[] args){ 23 File path = new File("G:"); 24 String[] list; 25 if(args.length == 0){ 26 list = path.list(); 27 }else{ 28 list = path.list(new DirFilter(args[0])); 29 } 30 Arrays.sort(list, String.CASE_INSENSITIVE_ORDER); 31 for(String dirItem:list){ 32 System.out.println(dirItem); 33 } 34 } 35 36 }
方法二改进:
1 /** 2 * Thinking in Java 3 * 获取文件列表 方法二改进 4 * 2016/6/15 5 */ 6 package fileStream; 7 8 import java.util.*; 9 import java.util.regex.*; 10 import java.io.*; 11 12 public class DirList3 { 13 public static void main(final String[] args){ 14 File path = new File("G:"); 15 String[] list; 16 if(args.length == 0){ 17 list = path.list(); 18 }else{ 19 list = path.list(new FilenameFilter() { 20 private Pattern pattern = Pattern.compile(args[0]); 21 public boolean accept(File dir, String name) { 22 return pattern.matcher(name).matches(); 23 } 24 }); 25 } 26 Arrays.sort(list, String.CASE_INSENSITIVE_ORDER); 27 for(String dirItem : list){ 28 System.out.println(dirItem); 29 } 30 } 31 }
二、文本文件的读写
1 /** 2 * 文本文件的读写 3 * 2016/6/15 4 */ 5 package fileStream; 6 7 import java.io.BufferedReader; 8 import java.io.FileOutputStream; 9 import java.io.FileReader; 10 import java.io.IOException; 11 import java.util.Scanner; 12 13 14 public class FileStream { 15 public static void main(String[] args) throws IOException{ 16 //写入文件 17 FileOutputStream outFile = new FileOutputStream("E:/1.txt",true); 18 StringBuffer outText = new StringBuffer(); 19 String str = null; 20 Scanner input = new Scanner(System.in); 21 byte[] writeByte = null; 22 while(true){ 23 str = input.nextLine(); 24 outText.append(str); 25 if(str.equals("quit")) break; 26 } 27 28 writeByte = (outText+"").getBytes(); 29 outFile.write(writeByte); 30 outFile.close(); 31 32 //读取文件 33 BufferedReader reader = new BufferedReader(new FileReader("E:/1.txt")); 34 String text = null; 35 while((text = reader.readLine()) != null){ 36 System.out.println(text); 37 } 38 reader.close(); 39 } 40 }
时间: 2024-10-09 20:55:07