JAVA SE(基础 第10篇) IO流

一、文件

1.  文件就是保存数据的地方。

2. 文件流: 文件在程序中是以流的形式来操作的。

3.  常用的文件操作:

4.  创建文件:3种方式

package com.File;/*

 **@verson  1.0
 */

import org.junit.Test;

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

public class File_ {
    public static void main(String[] args) {

    }
    //方式1. new File(String  pathname)
    @Test
    public void create01(){
        String filePath="D:\\news1.txt";
        File file=new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @Test
    //2.new File(File pareant, String child)//根据父目录文件+子路径构建
    public void create02(){
        File parentFile = new File("D:\\");
        String fileName="news2.txt";
        //这里只是一个java对象
        File file=new File(parentFile,fileName);
        try {
            //真正的在磁盘创建
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    //3.new File(File pareant, String child)//根据父目录文件+子路径构建
public void create03(){
        String parentPath="D:\\";
        String fileName="News3.txt";
        File file=new File(parentPath,fileName);
    try {
        file.createNewFile();
        System.out.println("创建成功");
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

5.  获取文件信息:

6.  目录的操作和文件删除:

   @Test
    public  void  m1(){
        String filePath="D:\\news1.txt";
        File file = new File(filePath);
        if (file.exists()){
            if (file.delete()){
                System.out.println(filePath+"删除成功");

            }else {
                System.out.println(filePath+"删除失败");

            }
        }else {
            System.out.println("改文件不存在");
        }
    }


D:\news1.txt删除成功
  @Test
    public  void  m3(){
        String filePath="D:\\demo\\a\\b\\c";
        File file = new File(filePath);
        if (file.exists()){
            System.out.println(filePath+"存在");


        }else {
         if (file.mkdirs()){
             System.out.println(filePath+"创建成功");
         }else {
             System.out.println(filePath+"创建失败");
         }
        }

二、

1.  IO流原理及流的分类:

2.  IO流体系图-常用的类

 

3.   FileInputStream:  

4. FileOutputStream:

5. 文件拷贝:

   String srcFilePath="C:\\Users\\wxj\\Pictures\\Camera Roll\\1.jpg";
        String destFilePath="D:\\1.jpg";
 FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;
        try {
            fileInputStream=new FileInputStream(srcFilePath);
            fileOutputStream=new FileOutputStream(destFilePath);
            //定义字节数组
            byte [] buf=new byte[1024];
            int readLen=0;
            while ((readLen=fileInputStream.read(buf))!= -1){
            fileOutputStream.write(buf,0,readLen);

            }
            System.out.println("拷贝ok");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream !=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream !=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



"拷贝ok

三、

 

1.  FileReader 和 FileWriter

2.   FileReader :

3. FileWriter:

4.节点流和处理流:

5.处理流设计模式:

6.  BufferedReader;BufferedWriter;Buffered拷贝。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java面相对象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值