Day20 1:File类 2:IO流

学习目标

1:File类
2:IO流

学习内容

一:File类

File类简介
  在 Java 中,File 类是 java.io 包中唯一代表磁盘文件本身的对象。File 类定义了一些与平台无关的方法来操作文件,File类主要用来获取或处理与磁盘文件相关的信息,像文件名、 文件路径、访问权限和修改日期等,还可以浏览子目录层次结构。
  File 类表示处理文件和文件系统的相关信息。也就是说,File 类不具有从文件读取信息和向文件写入信息的功能,它仅描述文件本身的属性。

File类的相关方法
1、构造方法
File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String parent,String child) 根据指定的父路径和文件路径创建一个新File对象实例
File(File parent,String child) 根据指定的父路径对象和文件路径创建一个新的File对象实例
2常用方法
获取功能的方法

publc String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串
publc String getPath() 将此抽象路径名转换为路径名字符串
publc String getName() 返回由此抽象路径名表示的文件或目录的名称
public long length() 返回由File表示的文件的长度

判断功能的方法

publc boolean isDirectory()测试此抽象路径名表示的File是否为目录
publc boolean isFile()测试此抽象路径名表示的File是否为文件
publc boolean exists()测试此抽象路径名表示的File是否存在

创建功能的方法

publiC boolean CreateNewFile() 当且仅当具有该名称的文件尚不存在时,创建一个新的文件夹。
public boolean mkdir() 创建由此抽象路径名命名的目录
public boolean mkdirs() 创建田此抽象路径名命名的目录,包括任何必需但不存在的父目录。

删除功能的方法

publiC boolean delete() 删除田此抽象路径名表示的文件或目录
例如:

package Demo01;
/**
 * 
 * 
 * file类
 * 
 * 1:创建一个我呢见,
 * 2删除一个文件或者文件夹
 * 3获取文件
 * 4判断文件
 * 5对文件进行遍历
 * 6获取文件大小
 * 
 * 
 * file    与操作系统无关
 * file是文件,directory 目录文件夹,path是路径**/
import java.io.File;
import java.io.IOException;

public class Demo01File {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
       String   pathSeparator=  File.pathSeparator;//路径分隔符
		 System.out.println( pathSeparator);//win 是分号  ,
	       String   Separator=  File.separator;
			 System.out.println( Separator);//文件名分隔符\  ,
         //绝对路径
			 //相对路径
   /**  //   show01();
		private static void show01() {
		File f1=new File("D:\\rxp\\java\\test");
		 System.out.println(f1); 
		 //仅仅创建了File这样的
		File f2=new File("D:\\\\rxp\\\\java\\\\test\\a.txt");
		 System.out.println(f2); 
			 
**/
			 
		/**		show03();	 
	private static void show03() {
	File parent =new File("D:\\\\rxp\\\\java\\\\test");
			File  f1=new File(parent,"hellow.java");
			System.out.println(f1); **/
		/**	 show04();
		}
    private static void show04() {
    	File f1 =new File("D:\\rxp\\java\\test\\hellow.java");
		String absolutepath1=f1.getAbsolutePath();
		System.out.println(absolutepath1);**/
		/**	 show05();
	}
private static void show05() {
	File f1 =new File("D:\\rxp\\java\\20\\Day20_code\\hellow.java");
	File f2 =new File("a.txt");
	String absolutepath1=f1.getAbsolutePath();
	System.out.println(absolutepath1); 
	 show06();
	}
private static void show06() {
	File f1 =new File("D:\\rxp\\java\\20\\Day20_code\\hellow.java");
	File f2 =new File("D:\\\\rxp\\\\java\\\\20\\\\Day20_code\\\\");
	System.out.println(f1.getName()); 
	System.out.println(f2.getName()); 
	/**show07();
}
private static void show07() throws Exception {

     File f1 =new File("D:\\\\rxp\\\\java\\\\20\\\\Day20_code\\\\");
     System.out.println(f1.length()); **/
   /**show08();
     }
     private static void show08() throws IOException {
         File f1 =new File("E:\\JAVA\\Day19\\.settings");
         System.out.println(f1.exists()); 
         File f2 =new File("E:\\JAVA\\Day1\\.settings");
         System.out.println(f2.exists());**/
        /**dv try {
			show10();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
         

	}
	private static void show10() throws IOException {
       
        File f1 =new File("E:\\\\JAVA\\\\Day19\\\\.settings\\hello.java");
        boolean b1=f1.createNewFile();
        System.out.println(b1);**/
        show11();
        

	}
	private static void show11() {
	
		
	}

	}



结果为:
在这里插入图片描述

递归

递归:指在当前方法内调用自己的这种现象。
递归的分类:递归分为两种,直接递归和间接递归。
直接递归称为方法自身调用自己。
间接递归可以A方法调用B方法,B方法调用C方法,C方法调用A方法。
注意事项:
递归一定要有条件限定,保证递归能够停止下来,否则会发生栈内存溢出。
在递归中虽然有限定条件,但是递归次数不能太多,否则也会发生栈内存溢出。
构造方法,禁止递归。

package Demo01;

import java.io.File;
import java.io.IOException;

/*
file类
创建一个文件/文件夹
删除一个文件/文件夹
获取文件/文件夹
判断文件/文件夹
对文件进行遍历
获取文件大小

file 是一个与操作系统无关的类
记住三个单词:
    file:是文件
    directory:目录、文件夹
    path:路径
        绝对路径
        相对路径
 */
public class Demo0file {
    public static void main(String[] args) throws IOException {
        String pathSeparator = File.pathSeparator;//文件路径分割符
//        System.out.println(pathSeparator);//win 是分号;Linux中是 :冒号
        String separator = File.separator;
//        System.out.println(separator);//文件名称分割符 Linux:/root/home/

        //绝对路径D:\代码\java
        //相对路径  ../../

        //构造方法演示
        show01();
        show02("D:\\代码\\java","test");
        show03();

//        常用方法演示
        show04();
        show05();
        show06();
        show07();
        show08();
        show09();
        //show10();
        show11();
        show12();


    }

    private static void show12(){
        //如果路径不存在,直接返回false
        File f1 = new File("D:\\java\\hello.java");
        boolean b1=f1.delete();
        System.out.println(b1);

        File f2 = new File("1\\2\\33\\44");//相对路径  但是会自动补齐 D:\代码\java\hello
        boolean b2=f2.delete();//直接从硬盘上删除  不会放入回收站
        System.out.println(b2);
    }
    private static void show11(){
        File f1 = new File("D:\\java\\hello.java");
        boolean b1=f1.mkdir();//只能创建一个文件夹
        System.out.println(b1);

        File f2 = new File("D:\\java\\1\\2\\33\\44\\hello.java");
        boolean b2=f1.mkdirs();//递归创建文件夹
        System.out.println(b1);
    }


private static void show10() throws IOException {
        File f1 = new File("D:\\hello.java");
        boolean b1 = f1.createNewFile();
        System.out.println(b1);
        //第二次执行的时候返回false,因为文件已经存在,他只创建未创建的文件

        File f2 = new File("D:\\java\\新建文件夹");//只能创建文件  不要看文件文字  看类型
        boolean b2 = f2.createNewFile();
        System.out.println(b2);
    }
    private static void show09(){
        File f1 = new File("D:\\java");//存在
        if(f1.exists()) {//返回TRUE,进入if语句
            System.out.println(f1.isDirectory());//true
            System.out.println(f1.isFile());//false
        }

        File f2 = new File("D:\\day20");//存在
        if(f2.exists()) {//不存在返回FALSE,所以不会进入if语句
            System.out.println(f2.isDirectory());
            System.out.println(f2.isFile());
        }

        File f3 = new File("D:\\java\\代码.iml");
        if(f3.exists()){ //判断是否为文件夹
            System.out.println(f2.isDirectory());//false 是个文件
            System.out.println(f2.isFile());//false
        }

    }
    private static void show08(){
        File f1 = new File("D:\\java");
        System.out.println(f1.exists());//文件存在打印true,不存在答应false
    }

    //注意 文件夹没有大小概念,不能获取文件夹的大小
    private static void show07() {
        File f1 = new File("D:\\day20");
        System.out.println(f1.length());//文件夹的大小0

        File f2 = new File("D:\\day20\\classfive");
        System.out.println(f2.length());//不存在的文件夹,打印的大小为0

        File f3 = new File("D:\\java\\代码.iml");//存在
        System.out.println(f3.length());
    }
    private static void show06(){
        File f1 = new File("D:\\day20\\hello.java");
        File f2 = new File("D:\\day20");
        //获取的构造方法传递路径结尾部分
        System.out.println(f1.getName());
        System.out.println(f2.getName());
    }
    private static void show05() {
        File f1 = new File("D:\\day20\\hello.java");
        File f2 = new File("a.txt");

        String path1 =f1.getPath();
        System.out.println(path1);

        System.out.println(f2.getPath());

        System.out.println(f1);
        System.out.println(f1.toString());
    }
    private static void show04(){
        File f1 = new File("D:\\java.b.txt");
        String absolutepath = f1.getAbsolutePath();//获取绝对路径
        System.out.println(absolutepath);

        File f2 = new File("hello.java");//相对路径创建,获取绝对路径
        //创建的时候,是放在项目路径下面
        String absolutepath2 = f2.getAbsolutePath();
        System.out.println(absolutepath2);
    }
    private static void show03(){
        File parent = new File("D:\\java");
        File f1 = new File(parent,"hello,java");
        System.out.println(f1);
    }
    private static void show02(String parent,String child) {
        File f1 = new File(parent,child);
        System.out.println(f1);
    }
    private static void show01(){
        File f1 = new File("D:\\java");
        System.out.println(f1);
        //仅仅是创建了File这样的类,并不会去检验他的正确性
        File f2 = new File("D:\\java\\b.txt");
        System.out.println(f2);

        File f3 = new File("b.txt");
        System.out.println(f3);
    }
}

结果:
在这里插入图片描述

二:IO流

1、流的概念

流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备、外部文件等。

一个流,必有源端和目的端,它们可以是计算机内存的某些区域,也可以是磁盘文件,甚至可以是Internet上的某个URL。

流的方向是重要的,根据流的方向,流可分为两类:输入流和输出流。用户可以从输入流中读取信息,但不能写它。相反,对输出流,只能往输入流写,而不能读它。 实际上,流的源端和目的端可简单地看成是字节的生产者和消费者,对输入流,可不必关心它的源端是什么,只要简单地从流中读数据,而对输出流,也可不知道它的目的端,只是简单地往流中写数据。 形象的比喻——水流 ,文件======程序 ,文件和程序之间连接一个管道,水流就在之间形成了,自然也就出现了方向:可以流进,也可以流出.

便于理解,这么定义流: 流就是一个管道里面有流水,这个管道连接了文件和程序。

2、Java流的分类

Java中的流,可以从不同的角度进行分类。

按照数据流的方向不同可以分为:输入流和输出流。

按照处理数据单位不同可以分为:字节流和字符流。

按照实现功能不同可以分为:节点流和处理流。

package Demo02;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/*
构造方法的作用
    1、创建了一个FileOutputStream对象
    2、根据构造方法中传递的文件/文件路径,创建一个空的文件
    3、会把FileOutputStream对象指向创建的文件

写入过程:
    Java程序 -->(java 虚拟机)--> os(操作系统上) --> 会调用对应的驱动 --> 把数据写入文件
 */
public class Demo02OutputStream {
    public static void main(String[] args) throws IOException {
        //创建一个FileOutputStream 对象 ,构造方法中传入数据目的地
        FileOutputStream fos=new FileOutputStream("a.txt");

        //调用write方法,将数据写入到文件中
        fos.write(97);//
        fos.write(98);
        fos.write(99);
        //释放资源

        fos.write(49);
        fos.write(48);
        fos.write(48);
        //100
//        byte[] bytes={65,66,67,68};
//        byte[] bytes={-65,-66,-67,-68};//中文
        byte[] bytes={65,66,67,68,69};
        fos.write(bytes,2,3);


        fos.close();
    }


		
	}



2020080605041

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值