导包快捷键
ctrl+shift+o
java.io.File
将操作系统中 文件 ,目录(文件夹),路径
封装成File类的对象 File类与系统无关
文件 file 目录 directory 路径 path
使用路径分隔符和名称分隔符的目的是使程序有跨平台性,不能把符号写死了
路径分隔符
File.pathSeparator
就像配置环境变量时,路径之间需要;分割符将其分割
public class test {
public static void main(String[] args) {
//File类中的静态成员变量
//与系统有关的路径分隔符,为了方便,它被表示为一个字符串
String separator = File.pathSeparator;
System.out.println(separator);//;
}
}
名称分隔符
File.separator
文件夹之间的分隔符\
public class test {
public static void main(String[] args) {
//File类中的静态成员变量
//与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串
String separator = File.separator;
System.out.println(separator);// \
}
}
File类的构造方法1
File(String pathname)
传递路径名,可以写到文件,也可以写到文件夹
但是退一步讲就算路径是错误的,File对象也会生成,但是可以用File对象的exist()方法来判断对象文件或者目录是否生成
public class stuctor {
public static void main(String[] args) {
//File(String pathname)
//传递路径名,可以写到文件,也可以写到文件夹
File f1 = new File("E:"+File.separator+"下载");//写到文件夹
System.out.println(f1);//E:\下载
File f2 = new File("D:"+File.separator+"p12_2.py");//写到文件
System.out.println(f2);//D:\p12_2.py
}
}
File类的构造方法2
File(String parent, String child)
灵活性更加好一点,可以单独操作父路径和子路径
public class structor2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//File(String parent, String child)
File f = new File("D:\\test\\test2","test3");
System.out.println(f);
}
}
File类的构造方法3
File(File parent, String child)
好处是父路径可以调用file类的方法
public class Structer {
public static void main(String[] args) {
// TODO Auto-generated method stub
//
File f = new File("D:\\test\\test2");
File f1 = new File(f,"test3");
System.out.println(f1);
}
}
绝对路径和相对路径
绝对路径:
在系统中有唯一性
例如:C:\Program Files (x86)\Adobe
www.baidu.com
相对路径:
表示的是路径之间的关系
就是子目录和父目录
File类的创建和删除
创建文件或者目录
创建文件
注意是创建文件,不是创建文件夹
** boolean createNewFile()**
当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件
public class createfile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f1 = new File("D:\\sunweiyuan.txt");
f1.createNewFile();
}
}
删除文件,或者文件夹
boolean delete()
删除方法不走回收站,直接从硬盘中删掉
删除此抽象路径名表示的文件或目录
public class createfile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f1 = new File("D:\\sunweiyuan.txt");
f1.createNewFile();
f1.delete();
}
}
创建文件夹
boolean mkdir(),mkdirs()
所以我们一般都用mkdirs()
public class createfile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f1 = new File("D:\\wenjianjia");
File f2 = new File("D:\\wenjianjia\\1\\22\\33");
//创建单级文件夹
f1.mkdir();
//创建多级文件夹
f2.mkdirs();
}
}
获取文件对象名字
String getName() 返回由此抽象路径名表示的文件或目录的名称
通俗来说,就是返回文件对象路径最后的名字
你这个文件对象存不存在无所谓
public class createfile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f2 = new File("D:\\wenjianjia\\1\\22\\33");
//String getName() 返回由此抽象路径名表示的文件或目录的名称
System.out.println(f2.getName());//33
System.out.println(f2.getPath());//D:\wenjianjia\1\22\33
System.out.println(f2.toString());//D:\wenjianjia\1\22\33
}
}
返回文件对象对所指的文件的长度
String length()
public class createfile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f2 = new File("D:\\wenjianjia\\1\\22\\33");
//String getName() 返回由此抽象路径名表示的文件或目录的名称
System.out.println(f2.length());//0
}
}
获取抽象路径的绝对路径
String getAbsolutePath()
File getAbsoluteFile()
都行,推荐使用getAbsoluteFile()
public class createfile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f2 = new File("D:\\p12_2.py");
System.out.println(f2.getAbsolutePath());
System.out.println(f2.getAbsoluteFile());
}
}
获取相对路径的绝对路径
public class createfile {
public static void main(String[] args) throws IOException {
//在eclipse中,写的是相对路径,则获取到的绝对路径是,eclipse的工作环境中的该工程下的路径
//E:\eclipse-workspace\io
File f2 = new File("secc");
System.out.println(f2.getAbsoluteFile());//E:\eclipse-workspace\io\secc
}
}
获得父路径
File getParentFile()
返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null
public class createfile {
public static void main(String[] args) throws IOException {
File f2 = new File("D:\\搜狗输入法\\SogouInput");
System.out.println(f2.getParentFile());//D:\搜狗输入法
}
}
测试此抽象路径名表示的文件或目录是否存在
boolean exist()
public class createfile {
public static void main(String[] args) throws IOException {
File f2 = new File("D:\\搜狗输入法\\SogouInput");
System.out.println(f2.exists());//true
}
}
public class createfile {
public static void main(String[] args) throws IOException {
File f2 = new File("src");
//因为io工程下有这个文件夹src
System.out.println(f2.exists());//true
}
}
判断File构造方法中封装的路径是文件夹还是文件
判断File构造方法中封装的路径是不是文件夹
boolean isDirectory()
判断File构造方法中封装的路径是不是文件
boolean isFile()
public class createfile {
public static void main(String[] args) throws IOException {
File f2 = new File("D:\\搜狗输入法\\SogouInput\\9.4.0.3336");
if(f2.exists())
{
System.out.println(f2.isDirectory());
}
}
}
返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录
String[] list()
只返回文件夹和文件的名字,不返回其所在路径
但是不算则子文件夹内的
public class createfile {
public static void main(String[] args) throws IOException {
//f2文件对象只能是文件的对象
File f2 = new File("F:\\i4Tools7\\App");
if(f2.exists())
{
String[] list = f2.list();
for(String str :list)
{
System.out.println(str);
}
}
}
}
也可以用
File[] listFiles()
输出的文件和文件夹带有路径
public class createfile {
public static void main(String[] args) throws IOException {
File f2 = new File("F:\\i4Tools7\\App");
if(f2.exists())
{
File[] list = f2.listFiles();
for(File str :list)
{
System.out.println(str);
}
}
}
}
文件过滤器
只输出.ipa后缀的文件
public class createfile {
public static void main(String[] args) throws IOException {
File f2 = new File("F:\\i4Tools7\\App");
if(f2.exists())
{
File[] list = f2.listFiles(new myfilter());
for(File fil:list)
{
System.out.println(fil);
}
}
}
}
重写accept方法
File[] list = f2.listFiles(new myfilter());
中的f2对象的listFiles方法有两个作用
第一个作用是得到f2对象的文件名和文件夹名
第二个作用是调用myfilter对象的方法accept并将文件名和文件夹名传入accept方法中
如果返回true,则将此pathname放入list中
public class myfilter implements FileFilter{
public boolean accept(File pathname) {
String name = pathname.getName();
//splite要用正则表达式,不能只是"."要用到转义
String a = "\\.";
String[] subsentence = name.split(a);
//==是判断两个字符串地址是否相等
//equalsIgnoreCase将此 String 与另一个 String 比较,不考虑大小写
if(subsentence[subsentence.length-1].equalsIgnoreCase("ipa"))
{
return true;
}
else
return false;
}
}
遍历目录
使用递归
public class ergodic {
public static void main(String[] args) {
File f = new File("E:\\demo");
getAllDir(f);
}
public static void getAllDir(File f)
{
if(!f.isDirectory())
{
return;
}
File[] file= f.listFiles();
for(File fil:file)
{
System.out.println(fil.toString());
getAllDir(fil);
}
}
}
递归求和
public class ergodicsum {
public static void main(String[] args) {
int Sum = sum(100);
System.out.println(Sum);
}
public static int sum(int n)
{
int a=0;
if(n==0)
{
return a;
}
else
{
return n + sum(n-1);
}
}
}
递归求阶乘
public class ergodicjc {
public static void main(String[] args) {
int JC = jc(5);
System.out.println(JC);
}
public static int jc(int n)
{
int a=1;
if(n==1)
{
return a;
}
else
{
return n *jc(n-1);
}
}
}
递归求斐波那契数列
从第三项开始,每项是前两项的和
1 1 2 3 5
public class fbnq {
public static void main(String[] args) {
int FB = fb(12);
System.out.println(FB);
}
public static int fb(int n)
{
if(n==1||n==2)
{
return 1;
}
else
{
return fb(n-2)+fb(n-1);
}
}
}
递归遍历目录,获取指定后缀的文件
public class filefilter implements FileFilter{
@Override
public boolean accept(File pathname) {
return pathname.toString().endsWith("txt");
}
}
public class ergodicfile {
public static void main(String[] args) {
File f = new File("E:\\demo");
getAllfile(f);
}
public static void getAllfile(File f)
{
if(f.isFile())
{
return;
}
else
{
File[] file = f.listFiles(new filefilter());
for(File i:file)
{
System.out.println(i);
}
File[] f1 = f.listFiles();
for(File fil:f1)
{
getAllfile(fil);
}
}
}
}