IO流概述
- IO流用来处理设备之间的数据传输,(内存-硬盘)
- 流:系统资源,Windows系统本身就可以操作设备,各种语言只是使用系统的这个资源
使用完该资源一定要释放 - Java对数据的操作是通过流(系统资源)的方式
- Java对于操作流的对象都在java.io包中
File f1 = new File("c:" + File.separator + "abc" + File.separator + "1.txt");
File f2=new File("C:\\Users\\jackchan\\Desktop\\gggg");
String file_name = f2.getName();
System.out.println(file_name);
long len = f2.length();
System.out.println(len);
String par = f2.getParent();
System.out.println(par);
--------------------------------------------------------------------------------------------
File f1 = new File("abc\\gggg");
String path1 = f1.getAbsolutePath();
String path2 = f1.getPath();
File f2 = new File("D:\\gggg.txt");
long time = f2.lastModified();
Date date = new Date(time);
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String s1 = df.format(date);
System.out.println(s1);
File f3 = new File("C:\\gg.txt");
boolean b = f3.createNewFile();
boolean b1 = f3.delete();
boolean b2 = f3.exists();
boolean b3 = f3.mkdir();
boolean b4 = f3.mkdirs();
boolean b5 = f3.isFile();
boolean b6 = f3.isDirectory();
boolean b6 = f3.isHidden();
File file = new File("D:\\");
long l = file.getFreeSpace();
long l1 = file.getTotalSpace();
long l2 = file.getUsableSpace();
---------------------------------------------------------------------------
文件名过滤器 / 文件过滤器
class FilterByName implements FilenameFilter {
private String s;
public FilterByName(String s) {
this.s = s;
}
public boolean accept(File dir, String name) {
return name.endsWith(s);
}
}
class Filetor implements FileFilter {
public boolean accept(File pathname) {
return pathname.isFile();
}
}
public class DEMO2 {
public static void main(String[] args) throws IOException {
File file = new File("C:\\");
File[] fs = file.listFiles(new Filetor());
for (File f : fs) {
System.out.println(f + " " + f.length());
}
}
}
递归
- 递归:函数自身调用自身,功能被重复使用,但每次该功能使用的运算参数不同
- 注意:一定要定义条件…如果递归次数过多会出现栈溢出
public static void main(String[] args) throws IOException {
File file = new File("E:\\abc");
getAll(file);
}
public static void getAll(File file) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) {
getAll(f);
} else {
System.out.println(f);
}
}
****************************************************
public static void deletAll(File file) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) {
System.out.println("文件夹"+f);
deletAll(f);
} else {
System.out.println(f+" : "+f.delete());
}
}System.out.println(file+" "+file.delete());
}
********************************************************
public static int xiangjia(int num){
if (num==1){
return 1;
}return num+xiangjia(num-1);
}
*********************************************************
public static List<File> get(File file) {
List<File> list = new ArrayList<>();
FileTor ff = new FileTor();
select(file, list, ff);
return list;
}
public static void select(File file, List<File> al, FileFilter ff) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) {
select(f, al, ff);
} else {
if (ff.accept(f)) {
al.add(f);
}} } }}
class FileTor implements FileFilter {
public boolean accept(File pathname) {
return pathname.getName().endsWith(".dll");
}
}