文件基础知识

输入输出主要是针对内存而言的。 

举例 说明:

package com.file;

import org.junit.Test;

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


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

    }
    @Test
    public void creat01()  {
        String filePath = "E:\\news.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();//打印出信息异常
        }

    }
    //方式二
    @Test
    public void create02(){
        File parentFile = new File("e:\\");
        String filename = "news02";
        File file = new File(filename);//这里的java程序只是个对象,只有执行了createNewFile才是真正的创建文件的过程
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式三
    public void create03(){
        String parentPath = "e:\\";
        String fileName = "news03";
        File file = new File(parentPath, fileName);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 常见的方法:



目录的操作和文件删除

byte流 

package com.file;

import org.junit.Test;

import java.io.File;

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


    }

    public void m1(){
        String filePath = "E:\\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("改文件不存在 ");
        }

    }
@Test
    public void m2(){
        String directoryPath = "E:\\news1";
        File file = new File(directoryPath);
        if (file.exists()){
            System.out.println(directoryPath + "文件已存在");
        }else{
            if (file.mkdir()){//创建一级目录,用mkdir,创建多级目录用mkdirs
                System.out.println("创建成功");
            }else{
                System.out.println("创建失败");
            }
        }

    }
}

IO原理

 

package InputStream;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 演示字节输入流的文件(读取到内存)
 * 获取字节流的对象就相当于获得了一个外卖小哥,运输指定的文件的外卖小哥

 */
public class FileInput_ {
    public static void main(String[] args) {

    }
    @Test
    public void readFile01(){
        String filePath = "e:\\hello.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            //创建了FileInputStream对象用于读取文件
            fileInputStream = new FileInputStream(filePath);

            try {
                while((readData = fileInputStream.read()) != -1){
                    System.out.print((char)readData);//转成char显示
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            ;//读取一个字节的数据,如果返回-1,表示读取完毕

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {//一定要关闭这个流,不关闭就会造成资源浪费,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

 字节输入流的改进:(一次改进读取多个字节)

package InputStream;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 演示字节输入流的文件(读取到内存)
 * 获取字节流的对象就相当于获得了一个外卖小哥,运输指定的文件的外卖小哥
 * 单个字节的读取比较慢,进行优化:  read(byte[] b)
 */
public class FileInput_ {
    public static void main(String[] args) {

    }
    @Test
    public void readFile01(){
        String filePath = "e:\\hello.txt";
        int readData = 0;
//        Byte[] bytes = new Byte[8];
        byte[] bytes = new byte[8];
        int readlen = 0;
        FileInputStream fileInputStream = null;
        try {
            //创建了FileInputStream对象用于读取文件
            fileInputStream = new FileInputStream(filePath);

            try {
                while((readlen = fileInputStream.read(bytes)) != -1){
                    //读取byte.length,长度的字符到bytes中
                    //如果读取正常的话,则返回实际读取的字节数
                    //返回-1表示读取完毕
                    System.out.print(new String(bytes,0,readlen));//转成char显示
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            ;//读取一个字节的数据,如果返回-1,表示读取完毕

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {//一定要关闭这个流,不关闭就会造成资源浪费,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




    }
}

文件的拷贝:

package filecopy;

import org.junit.Test;

import java.io.*;

public class FileCopy {
    public static void main(String[] args) {
        //完成文件拷贝
        //思路分析
        //1.创建文件的输入流,将文件读入程序
        //2.创建文件的输出流,将读取到的文件数据写入到指定的文件夹中
    }
    @Test
    public void m1(){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        String srcFilePath = "E:\\by.jpg";//源文件
        String destFilePath = "F:\\by.jpg";//目标盘

        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(destFilePath);
            //定义一个自己数组,提高读取的效率
            byte[] buf = new byte[1024];
            int readLen = 0;
            while((readLen = fileInputStream.read(buf)) != -1){
                //这个时候已经读取到了buf中去了,现在将buf写到F盘中去
                fileOutputStream.write(buf,0,readLen);

            }
            System.out.println("拷贝成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //前面是已经读取完毕了,现在进行检测输出流输入流是否还在启动状态,要是没关闭就把他关闭
                //相当于就是:外卖已经拿到了,这个时间段就不在需要外卖小哥了。让他休息一会吧。
                if (fileInputStream != null){
                    fileInputStream.close();
                }
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void m2(){

    }

}

字符流:以数组的形式进行改进

package reader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Reader {
    public static void main(String[] args) {
        String filePath = "e:\\hello.txt";
        FileReader fileReader = null;
        try {
           fileReader = new FileReader(filePath);
            char[] chars = new char[1024];
            int readLen = 0;
            while(( readLen = fileReader.read(chars)) != -1){
               System.out.println(new String(chars,0,readLen));
           }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (fileReader != null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

字符流:写入

package writer;

import java.io.FileWriter;
import java.io.IOException;

public class Writer {
    public static void main(String[] args) {
        String filePath = "e:\\nite.txt";
        FileWriter fileWriter = null;
        try {
             fileWriter = new FileWriter(filePath);
            fileWriter.write(new String("我爱你").toCharArray());
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


节点流(看不同的数据源选择不同的流)和处理流

距离:

package writer;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public interface BufferWriter_ {
    public static void main(String[] args) {
        String filePath = "E:\\ok.txt";
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
            bufferedWriter.write("我们要微笑,我们要坚强,因为无论我们怎样");
            bufferedWriter.newLine();//插入一个与系统相关的换行符。
//            new FileWriter(filePath,true),以追加的方式写入
            bufferedWriter.close();//关闭外层流即可,传入的流就会自动关闭。
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

用包装流实现复制视频文件:

package OutPutStream;

import java.io.*;

/**
 * 演示包装流 copy 图片文件
 * 图片什么的都底层 要用字节流
 *字节流可以操作文本文件和二进制文件。字节流牛逼,字节--->比特
 * 本家通信。这点不要忘记了。
 */
public class BufferedCopy02_ {
    public static void main(String[] args) {
        String scrFilePath = "E:\\by.jpg";
        String destFilePath = "F:\\by.jpg";
        BufferedInputStream bis = null;//这样的目的就是作用域的问题,要是定义在{}里面的话,作用域就会减少很多了。
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(scrFilePath));
            bos = new BufferedOutputStream(new FileOutputStream(destFilePath));

            byte[] bytes = new byte[1024];
            int readLen = 0;
            //当返回-1时,文件读取完毕

            while ((readLen = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, readLen);
            }

            System.out.println("问价拷贝完毕");
            System.out.println("无论怎样我们都要好好的");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                //关闭外层的流就可以了,底层会去关闭节点流
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


处理对象的节点流和处理流:

 如果需要序列化,一定要实现接口。

字节流转换成字符流,最后再将其转换成包装流进行转换:

package test;

import java.io.*;

/**
 *演示使用InputStreamReader  转换流解决中文乱码的问题
 * 将字节流FileInputStream  转换成字符流InputStreamReader  ,指定编码gbk
 */

public class A {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\a.txt";
        //其实就是将FileInputStream转换成了InputStreamReader
        InputStreamReader isk = new InputStreamReader(new FileInputStream(filePath),"gbk");
        //也可以进行套娃,一步完成。
        //我不比他们差在哪里哦
        //第三步:将InputStreamReader  转换成  包装流
        BufferedReader br = new BufferedReader(isk);
        String s = br.readLine();
        System.out.println("s = " + s );
        br.close();//一定要关闭流哦

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值