3.28 作业

package com.qiku.yrc.work21;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public class FileWriterAndReader {
//    分别使用字符输入输出流FileWriter/FileReader
//    文件输入输出流FileInputStream/FileOutputStream
//    文件缓冲流BufferedInputStream/BufferedOutputStream
//    字符缓冲流BufferedWriter/BufferedReader
//     种方式实现

    public static void main(String[] args) throws Exception {
        String path="F:\\idea\\IntelliJ IDEA 2021.2.1\\Qikujavase\\src";
        FileWriter fileWriter=new FileWriter("b.TXT",true);
        System.out.println(fileWriter);

        File file1 = new File("F:\\idea\\IntelliJ IDEA 2021.2.1\\Qikujavase\\src","a.TXT");
        System.out.println(file1);
        //创建文件
//        File file2= new File(file1,"src\\a.doc");
//        while (!file2.exists()){
//            file2.createNewFile();
//        }
//        System.out.println(file2.exists());

        FileReader reader = new FileReader("src\\b.TXT");
        int read = reader.read();
        System.out.println(read);
        read = reader.read();
        System.out.println(read);
        read = reader.read();
        System.out.println(read);read = reader.read();
        System.out.println(read);
        read = reader.read();
        System.out.println(read);
        read = reader.read();
        System.out.println(read);// 依次读取一个 字符

        char[] cs = new char[6];
        System.out.println(reader.read(cs));
        System.out.println(Arrays.toString(cs));
        System.out.println(reader.read(cs));
        System.out.println(Arrays.toString(cs));
        System.out.println(reader.read(cs));
        System.out.println(Arrays.toString(cs));
        //reader读取了两个字符a,b ,然后将其放在了数组cs下标为1,2,3的位置
        reader.read(cs,1,3);
        System.out.println(Arrays.toString(cs));

        reader.close();//关闭流  释放资源
    }
}
package com.qiku.yrc.work21;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;


public class FileInputAndOutputStream {
    public static void main(String[] args) throws Exception {
        FileInputStream fis= new FileInputStream("b.TXT");
        for (int i = 0; i < 6; i++) {//一次只能读一个
            System.out.print(fis.read()+" ");
        }
        System.out.println();
        System.out.println("=======");
        byte[] bytes = new byte[3];
        System.out.println(fis.read(bytes,1,2));
        System.out.println(Arrays.toString(bytes));

        System.out.println(fis.available());
        fis.close();

        FileOutputStream out = new FileOutputStream("d.txt",true);
        out.write(97);
        String str="我欲乘风归去";
        byte[] bytes2 = str.getBytes();
        out.write(bytes2);

        System.out.println(bytes.length);
        out.write(bytes2,0,3);//两个bytes
        out.flush();
        out.close();
        //flush()这个函数是清空的意思close()方法关闭读写流


    }
}
package com.qiku.yrc.work21;

import java.io.*;

public class BufferWriterAndReader {
    public static void main(String[] args) throws Exception {
        FileReader fr= new FileReader("dd.txt");
        BufferedReader br = new BufferedReader(fr);

        FileWriter  fw=new FileWriter("dddd.txt");
        BufferedWriter bw=new BufferedWriter(fw);


        bw.write("偶像练习生 蔡徐坤申请出战");
        bw.newLine();//换行符
        String s=br.readLine();
        System.out.println(s);
        bw.close();
        br.close();
    }
}
package com.qiku.yrc.work21;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class BufferedInputAndOutputStream {
    public static void main(String[] args) throws Exception {
        FileInputStream fis= new FileInputStream("F:\\idea\\IntelliJ IDEA 2021.2.1\\Qikujavase\\src\\1234.mp4");
        BufferedInputStream bis= new BufferedInputStream(fis);//输入缓冲流

        FileOutputStream fos= new FileOutputStream("F:\\idea\\IntelliJ IDEA 2021.2.1\\Qikujavase\\src\\1235.mp4");
        BufferedOutputStream bos= new BufferedOutputStream(fos);//输出缓冲流

        while(true){
            int r= bis.read();//读
            if (r==-1){
                break;
            }else {
                bos.write(r);//写
            }



        }
        System.out.println("复制成功!");
        bis.close();
        bos.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值