JavaIO流

1.文件

什么是输入流什么是输出流

从Java程序到文件是输出流,从文件到Java程序是输入流

打个比方,你和喝水的话,水流到你的肚子里面,是输入,你把水吐出来是输出

2.常用的文件操作

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

    }
    @Test
    public void fileCreate01(){
        String FileChat = "e:\\news1.txt";
        File file = new File(FileChat);
        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void fileCreate02(){
        File file = new File("e:");
        String filechat = "news2.txt";
        File file1 = new File(file,filechat);
        try {
            file1.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

第三种方式

3.获取文件的相关信息

4.目录的操作

import org.junit.Test;

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

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

    }
    @Test
    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("删除失败");
            }
        }else {
            System.out.println(FilePath+"不存在");
        }
    }
    @Test
    public void m3(){
        String FilePath = "d:\\demo2.txt";
        File file = new File(FilePath);
        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void m2(){
        String FilePath = "d:\\demo2.txt";
        File file = new File(FilePath);
        if (file.exists()){
            if (file.exists()){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        }else {
            System.out.println(FilePath+"不存在");
        }
    }
    @Test
    public void m4(){
        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("创建成功");
            }else {
                System.out.println(filePath+"创建失败");
            }
        }
    }

}

5.流与文件的关系

刚刚在上面我们不是说了输入流和输出流是什么嘛

接下来我们说一下流与文件的关系,比如说我买了个快递,是不是快递小哥要送到我手里,我要退货是不是快递小哥退到快递中心,这个快递小哥就是流,而我和快递中心就是文件和Java程序

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

I指的就是inputstream

o指的就是outputstream

我们来看一下他们的类图

6.1input

6.1.1FileInputStream

import org.junit.Test;

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

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

    }
    @Test
    public void FileInputStream(){
        int temp = 0;
        String filepath = "d:\\hello.txt";
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filepath);
            while ((temp = fileInputStream.read())!=-1) {
                System.out.print((char) temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
//用read(byte)
    @Test
    public void FileInputStream02(){
        String filepath = "d:\\hello.txt";
        byte[] buf = new byte[8];
        int readLen = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filepath);
            while ((readLen = fileInputStream.read(buf))!=-1) {
                System.out.print(new String(buf,0,readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
6.2output
6.2.1FileoutputStream

一种是和上面一样写一个字节

import java.io.FileOutputStream;
import java.io.IOException;
@SuppressWarnings({"all"})
public class FileOutInputStream {
    public static void main(String[] args) {

    }
    @Test
    public void FileOutInputStream(){
        String FilePath = "d:\\a.txt";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(FilePath);
            fileOutputStream.write('h');
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

另一种是用数组,多个字节

@Test
    public void FileOutInputStream(){
        String FilePath = "d:\\a.txt";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(FilePath);
            String str = "hello,world!";
            fileOutputStream.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

getBytes转为数组

还可以这样

就存入hel

还有就是构造器

我们这样的话就是覆盖,原有内容

后面加个true就是追加

比较简单不难

6.3练习题

public class FileCopy {
    public static void main(String[] args) {
        String scrFilepath = "d:\\pz.jpg";
        String destFilepath = "d:\\zi.jpg";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(scrFilepath);
            fileOutputStream = new FileOutputStream(destFilepath);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes))!= -1 ){
                fileOutputStream.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }
                if (fileInputStream != null){
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

6.4文件字符流
6.4.1FileReader

课堂练习

//        要求:
//        1)使用filereader 从 story.txt 读取内容,并显示
public class FileReader {
    public static void main(String[] args) {
        String FilePath = "d:\\story.txt";
        java.io.FileReader fileReader = null;
        int data =0;
        try {
            fileReader = new java.io.FileReader(FilePath);
            while ((data = fileReader.read())!=-1){
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

还有一种是用数组

import java.io.IOException;

//        要求:
//        1)使用filereader 从 story.txt 读取内容,并显示
public class FileReader {
    public static void main(String[] args) {
        String FilePath = "d:\\story.txt";
        java.io.FileReader fileReader = null;
        int data =0;
        char [] buf = new char[8];
        try {
            fileReader = new java.io.FileReader(FilePath);
            while ((data = fileReader.read(buf))!=-1){
                System.out.print(new String(buf,0,data));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

6.4.2FileWriter

public class FileWriter {
    public static void main(String[] args) {
        String FilePath = "d:\\note.txt";
        java.io.FileWriter fileWriter =null;
        try {
            fileWriter = new java.io.FileWriter(FilePath);
            fileWriter.write("风雨之后,定见彩虹");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

7.节点流和处理流

7.1BufferReader
public class BufferReader {
    public static void main(String[] args) throws Exception {
        String FilePath = "d:\\story.txt";
            BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
            String line;
        while ((line = bufferedReader.readLine())!=null){
            System.out.println(line);
        }
        bufferedReader.close();
    }
}

7.2BufferWriter


//使用bufferedwriter 将""hello,韩顺平教育",写入到文件中
public class Bufferwriter {
    public static void main(String[] args)throws IOException {


        String FilePath = "d:\\note.txt";
        BufferedWriter bufferedWriter = new BufferedWriter(new  FileWriter(FilePath));
        bufferedWriter.write("韩顺平教育");
        bufferedWriter.close();
    }
}
7.3练习题

public class test {
    public static void main(String[] args) throws IOException {
        String FilePath = "d:\\story.txt";
        String FileCopy = "d:\\story2.txt";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(FilePath));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FileCopy));
        String readLine ;
        while ((readLine = bufferedReader.readLine()) != null){
            bufferedWriter.write(readLine);
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
}
7.4BufferedOutputStream和BufferedInputStream

public class tsest {
    public static void main(String[] args) throws IOException {
        String FilePath = "d:\\pz.jpg";
        String FileCopy = "d:\\pz2.jpg";
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        bufferedInputStream = new BufferedInputStream(new FileInputStream(FilePath));
        bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(FileCopy));
        //开始写入
        byte[] buf = new byte[1024];
        int readline = 0;
        while ((readline = bufferedInputStream.read(buf)) != -1){
            bufferedOutputStream.write(buf,0,readline);
        }
        //关闭
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
    }
}

7.5对象流,ObjectOutputStream和ObjectInputStream

public class study {
    public static void main(String[] args) throws IOException {

        String FilePath = "d:\\data.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FilePath));
        oos.write(100);
        oos.writeBoolean(true);
        oos.writeChar('a');
        oos.writeDouble(9.5);
        oos.writeUTF("hsp");
        oos.writeObject(new dog("小明",15));
        oos.close();
        System.out.println("完毕");
    }
}
class dog implements Serializable{
    private String name;
    private int age;

    public dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class study {
    public static void main(String[] args) throws Exception {
        String FilePath = "d:\\data.dat";
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(FilePath));
        System.out.println(objectInputStream.readInt());
        System.out.println(objectInputStream.readBoolean());
        System.out.println(objectInputStream.readChar());
        System.out.println(objectInputStream.readDouble());
        System.out.println(objectInputStream.readUTF());
        System.out.println(objectInputStream.readObject());
        objectInputStream.close();

    }
}
7.6注意事项

8.标准输出输入流

9.转换流

import java.io.*;

public class study {
    public static void main(String[] args) throws IOException {
        String FilePath = "d:\\note.txt";
        InputStreamReader isr = new InputStreamReader(new FileInputStream(FilePath),"gbk");
        BufferedReader bufferedReader = new BufferedReader(isr);
        bufferedReader.readLine();
        bufferedReader.close();
    }
}
public class study {
    public static void main(String[] args) throws IOException {
        String FilePath = "d:\\demo.txt";
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(FilePath));
        BufferedWriter bufferedWriter = new BufferedWriter(osw);
        bufferedWriter.write(100);
        bufferedWriter.close();
    }
}

10.打印流printStream

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值