day10

一、字符流
Reader:字符输入流
Writer:字符输出流
二、字符流的基本操作
(1)文件的读:
package Day20;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
public class ReaderFile { public static void main(String[] args) throws Exception {
Reader reader = new FileReader(“d://a/a.txt”);
char[] b = new char[1024];
int i = reader.read(b);
String s = “”;
while(i>0){
s += new String(b,0,i);
i = reader.read(b);
}
System.out.println(s);
}
}
(2)文件的写
package Day20;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriteFile {
public static void main(String[] args) throws Exception {
Writer write = new FileWriter(“d://a/b.txt”);
String s =“我爱中国”;
write.write(s);
write.flush();
}
}
(3)文件的复制
package Day20;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
public class CopyFile {
public static void main(String[] args) throws Exception {
Reader reader = new FileReader(“d://a/a.txt”);
Writer writer = new FileWriter(“d://a/d.txt”);
char[] b = new char[1024];
int i = reader.read(b);
while(i>0){
writer.write(b,0,i);
i = reader.read(b);
}
writer.flush();
reader.close();
writer.close();
}
}
三、缓冲流:
分类:字节型缓冲流和字符型缓冲流。
优点:节省运行的时间。
如何使用:
(1)使用字节型缓冲流进行文件的复制。
package Day20;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class huanChongLiu {
public static void main(String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(“d://a/a.txt”));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(“d://a/e.txt”));
byte[] b = new byte[1024];
int i = bis.read(b);
while(i>0){
bos.write(b,0,i);
i = bis.read(b);
}
bos.flush();
}
}
(2)使用字符型缓冲流进行文件的复制。
package Day20;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
public class huanChongLiu { public static void main(String[] args) throws Exception {
BufferedReader bis = new BufferedReader(new FileReader(“d://a/a.txt”));
BufferedWriter bos = new BufferedWriter(new FileWriter(“d://a/e.txt”));
char[] b = new char[1024];
int i = bis.read(b);
while(i>0){
bos.write(b,0,i);
i = bis.read(b);
}
bos.flush();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值