1.分类
根据数据传输方向, 分为输入流和输出流.
程序从输入流中读取数据, 向输出流中写入数据.
根据操作数据不同, 分为字节流和字符流.
字节流: java.io.InputStream java.io.OutputStream 字符流: java.io.Reader java.io.Writer
2.File类
1 package deom02; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 public class FileDemo { 7 public static void main(String[] args) throws IOException { 8 //static String separator 与系统有关的默认名称分隔符 "\" 9 String separator = File.separator; 10 //static String pathSeparator 与系统有关的路径分隔符 ";"11 String pathSeparator = File.pathSeparator; 12 System.out.println(separator);13 System.out.println(pathSeparator);14 15 //boolean createNewFile() 新建文件16 File file = new File("c:\\java.txt");17 boolean b = file.createNewFile();18 System.out.println(b);19 20 //boolean mkdir() 新建文件夹21 File file1 = new File("c:\\abc");22 boolean b1 = file1.mkdir();23 System.out.println(b1);24 25 //boolean mkdirs() 新建多级文件夹26 File file2 = new File("c:\\ab\\ac");27 boolean b2 = file2.mkdirs();28 System.out.println(b2);29 30 //boolean delete() 删除文件或目录\31 File file3 = new File("c:\\del");32 boolean b3 = file3.delete();33 System.out.println(b3);34 35 //String getName() 返回文件/文件夹名36 File file4 = new File("c:\\getName");37 String name = file4.getName();38 System.out.println(name);39 40 //String getPath() 返回路径41 String path = file4.getPath();42 System.out.println(path);43 44 //long length() 返回文件字节数45 File file5 = new File("c:\\zijie.txt");46 long length = file5.length();47 System.out.println(length);48 49 //String getAbsolutePath() 返回绝对路径50 String absolutePath = file5.getAbsolutePath();51 System.out.println(absolutePath);52 //File getAbsoluteFile() 返回file对象53 File absoluteFile = file5.getAbsoluteFile();54 System.out.println(absoluteFile);55 56 //String getParent() 返回File对象对应目录的父目录57 File file6 = new File("c:\\aaa\\aaa\\1.txt");58 String parent = file6.getParent();59 System.out.println(parent);60 //File getParentFile()61 File parentFile = file6.getParentFile();62 System.out.println(parentFile);63 64 //boolean exists() 判断文件/文件夹是否存在65 boolean bexist = file6.exists();66 System.out.println(bexist);67 68 //boolean isDirectory() 判断是不是目录69 boolean bdirectory = file6.isDirectory();70 System.out.println(bdirectory);71 72 //boolean isFile() 判断是不是文件73 boolean bfile = file6.isFile();74 System.out.println(bfile);75 76 //String[] list() 列出指定目录全部内容77 File file7 = new File("c:\\");78 String[] strArr = file7.list();79 for (String str : strArr) {80 System.out.println(str);81 }82 System.out.println(strArr.length);83 84 //File[] listFiles()85 File file8 = new File("c:\\");86 File[] fileArr = file8.listFiles();87 for (File f : fileArr) {88 System.out.println(f);89 }90 91 }92 }
3.文件过滤器
1 package deom02; 2 3 import java.io.File; 4 import java.io.FileFilter; 5 6 //实现FileFilter接口, 重写抽象方法 7 public class MyFilter implements FileFilter { 8 public boolean accept(File pathname) { 9 //pathname接收到的是文件的全路径10 String name = pathname.getName();11 return name.endsWith(".txt");12 }13 }
1 package deom02; 2 3 import java.io.File; 4 5 //文件过滤 6 public class FilterDemo { 7 public static void main(String[] args) { 8 //listFiles(FileFilter filter) 9 File file = new File("c:\\");10 File[] fileArr = file.listFiles(new MyFilter());11 for (File f: fileArr) {12 System.out.println(f);13 }14 }15 }
4.递归遍历
1 package deom02; 2 3 import java.io.File; 4 5 //递归遍历指定目录下所有文件和文件夹 6 public class BianLi { 7 public static void main(String[] args) { 8 File dir = new File("d:\\browser"); 9 getFile(dir);10 }11 12 public static void getFile(File dir) {13 File[] fileArr = dir.listFiles();14 for (File f : fileArr) {15 System.out.println(f);16 if (f.isDirectory()) {17 getFile(f);18 } 19 }20 }21 }