头歌实践教学平台——Java程序设计之高级特性IO流

目录

Java高级特性 - IO流

什么是I0流        BC        C

字节流-输入输出

字符流-输入输出

复制文件

如果此文章对你有帮助,麻烦点个赞哦~~~

点个关注,追新不迷路~,下一篇——Java集合框架


Java高级特性 - IO流

什么是I0流        BC        C

字节流-输入输出

package step2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Task {
    public void task() throws IOException{
        /********* Begin *********/
        FileInputStream fs = new FileInputStream("src/step2/input/task.txt");
        byte[] b = new byte[8];
        fs.read(b);
        String str = new String(b);
        System.out.println(str);
        File dir = new File("src/step2/output/");
        if(!dir.exists()){
            dir.mkdir();
        }
        FileOutputStream fos = new FileOutputStream("src/step2/output/output.txt");
        String out = "learning practice";
        byte[] outByte = out.getBytes();    //将字符串转换成字节
        fos.write(outByte);                    //写数据
        //释放资源
        fs.close();
        fos.close();
        /********* End *********/
    }
}

字符流-输入输出

package step3;

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

public class Task {
	
	public void task() throws IOException{
		/********* Begin *********/
		String file1 = "src/step3/input/input.txt";   //创建文件
		FileReader fr = new FileReader(file1);   //实例化
		char[] ch = new char[8];  //创建数组
		fr.read(ch);              //将文件的数据读入到数组中(从前到后)

		String file2="src/step3/output/output.txt";//创建文件
        FileWriter fw = new FileWriter(file2); // 实例化
        fw.write(ch); // 读入数组中的数据到文件中(从后到前)

        fr.close();   //关闭流
        fw.flush();   //刷新流
        fw.close();   //关闭流
		/********* End *********/		
	}
}

复制文件

package step4;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Task {
	
	public void task() throws IOException{
		/********* Begin *********/
		FileReader fr = new FileReader("src/step4/input/input.txt");
		FileWriter fw = new FileWriter("src/step4/output/output.txt");
		int len = 0;
		char[] cha = new char[1024];
		while ( (len = fr.read(cha) ) != -1) 
		{
			fw.write(cha , 0 , len);
		}
		fr.close();
		fw.flush();
		fw.close();
		
		FileInputStream fs = new FileInputStream("src/step4/input/input.jpg");
		FileOutputStream fo = new FileOutputStream("src/step4/output/output.jpg");
		int le = 0;
		byte[] bt = new byte[1024];
		while ( (le = fs.read(bt) ) != -1)
		{
			fo.write (bt , 0 , le);
		}
		fs.close();
		fo.flush();
		fo.close();		
		/********* End *********/		
	}
}

如果此文章对你有帮助,麻烦点个赞哦~~~

点个关注,追新不迷路~,下一篇——Java集合框架

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

萌新发文啦~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值