JAVA流写文件内容丢失_Java基础-IO流-文件内容读写

本文介绍了JAVA中字节流、字符流以及缓冲流在文件读写操作中的应用。通过实例展示了FileInputStream、FileOutputStream、FileReader、FileWriter以及对应的缓冲流在读取和写入文件时的方法,如read()、write()、read(byte[])、write(byte[])等,强调了不同流类型适用于不同文件类型的读写,并提及在处理小文件时使用缓冲流的优势。
摘要由CSDN通过智能技术生成

字节流与字符流操作文件的读写的方式

字节流文件操作:主要用于读取图片,MP3,AVI视频等文件。

字符流文件操作:主要用于读取处理纯文本数据。

缓冲流操作文件的读取和写入

字节流操作文件的读取和写入:

字节流文件操作:主要用于读取图片,MP3,AVI视频等文件。

使用InputStream与OutputStream的子类实现为:FileInputStream与FileOutputStream。

字节流向文件中写入数据(字节流FileOutputStream )

public static void testFileOutputStream() {

try (FileOutputStream fileOutputStream = new FileOutputStream("E:\\document\\io\\d.txt");

){

String str = "FileOutputStream测试数据";

fileOutputStream.write(str.getBytes());

String str2 = "\r\n";

fileOutputStream.write(str2.getBytes());

String str3 = "FileOutputStream测试再次添加数据";

fileOutputStream.write(str3.getBytes());

} catch (IOException e) {

e.printStackTrace();

}

}

字节流从文件的读取(字节流FileInputStream)

// read()

public static void testFileInputStream() {

try (FileInputStream fileOutputStream = new FileInputStream("E:\\document\\io\\d.txt");// 内容:FileOutputStream测试数据\r\nFileOutputStream测试再次添加数据

){

// read()

int read1 = fileOutputStream.read();

System.out.println((char) read1);

int read2 = fileOutputStream.read();

System.out.println((char) read2);

} catch (IOException e) {

e.printStackTrace();

}

}

// read(byte b[]) new String(a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值