字符流
单做读操作
注意点:
1.用int类型读时,输出需要强转为char数据类型输出
用char[]数组
2.用字节数组读时,需要强转为String类型输出
// An highlighted block
//读取一个字符
private static void test1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("a.txt");
System.out.println((char)fis.read());
}
// An highlighted block
//读取多个字符
private static void test2() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("a.txt");
int i;
while((i = fis.read())!=-1) {
System.out.print((char)i);
}
// 这里一定要用i来读,因为每调用一次read()方法,指针就会后移一位
// while((i = fis.read())!=-1) {
// System.out.print((char)fis.read());
// }
}
// An highlighted block
//用byte数组来读
private static void test3() throws IOException {
FileInputStream fis = new FileInputStream("a.txt");
byte [] c = new byte[2]; //每次读2字节
int length;
while((length = fis.read(c))!=-1) {
System.out.println(new String(c,0,length));
}
}
// An highlighted block
//使用字节流读取包含文字和标点符号
private static void test4() throws IOException {
FileInputStream fis = new FileInputStream("a.txt");
int lenth;
while((lenth = fis.read()) != -1) {
byte b1 = (byte)lenth;
if(b1>=0) { //当是特殊字符时,转为byte的值小于零
System.out.print((char)lenth);
}else {
byte b2 = (byte)fis.read(); //再读取下一个字节
byte[] b = {b1,b2}; //字符由两个字节组成,用字节数组存起来
System.out.print(new String(b));//然后再把包含两个字节的数组转为String输出
}
}
fis.close();
}
单做写操作
注意点:
1.写入单个字符时,可以直接写入,写入方法可以用字节数组一次写入多个,也可一个一个写入
2.写入字符串时,必须先转成字节数组然后再写入,否则全是乱码
// An highlighted block
//利用字节数组写入
private static void test1() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("c.txt");
byte[] c = new byte[]{'a','2','3','4'};
fos.write(c);
fos.flush();
fos.close();
}
// An highlighted block
//读入字符串时
private static void test3() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("c.txt");
fos.write("abc".getBytes());
fos.write("中国".getBytes());
fos.flush();
fos.close();
}
这里需要注意:当你要写入int数据类型时可以先转成String类型,然后再用.getBytes()方法去写入
字符流
单做写操作
注意点:
单写入数字时,不能直接写入!必须转成字符类型才能写入
例:
// An highlighted block
private static void test11() throws IOException {
FileWriter fw = new FileWriter("a.txt");
fw.write("1");//写入后打开文件就是乱码,
//因为不能直接写入非字符类型数据,必须转为字符类型再写入
fw.close();
}
解决方法:
fw.write(1+""); 加一个空串转为String类型进行写入
单做读操作
因为读出来是字符类型,直接输出就行
三种基本方式和字节流的写法相同,高效缓冲流会多一个特殊方法readLine()一次读一行,以这种方式为例
// An highlighted block
public static void test() throws IOException{
FileReader fr = new FileReader("a.txt");
BufferedReader br = new BufferedReader(fr);
String len;
while((len=br.readLine())!=null) {
System.out.print(len);
}
br.close();
}
边读边写即(文件拷贝)
用字节流拷贝
字节流可以拷贝文本文件及非文本文件
注意:拷贝过程就不像单读或单写那样麻烦,还有进行转换什么的。
这里讲解三种方法,各有特点
// An highlighted block
//一个字节一个字节的复制,及其不提倡!
private static void test1() throws FileNotFoundException, IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("1.mp4");
FileOutputStream fos = new FileOutputStream("2.mp4");
int i;
while((i=fis.read())!=-1) {
fos.write(i);
}
fis.close();
fos.close();
long end = System.currentTimeMillis();
System.out.println(end-start+"ms");
}
上面这种方式及其不推荐,一个一个访问效率超级慢
// An highlighted block
//利用字节数组读
private static void test2() throws FileNotFoundException, IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("1.mp4");
FileOutputStream fos = new FileOutputStream("2.mp4");
byte[] arr = new byte[1024]; //也不能太大,会占用太多内存
int len;
while((len=fis.read(arr))!=-1) {
fos.write(arr,0,len);
}
fis.close();
fos.close();
long end = System.currentTimeMillis();
System.out.println(end-start+"ms");
}
上述方式也不推荐,因为需要事先规定好每次读取多少个字节,规定大了占内存,规定小了效率慢
// An highlighted block
//利用缓冲流读
private static void test3() throws FileNotFoundException, IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("b.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
while((len=bis.read())!=-1) {
bos.write(len);
}
bis.close();
bos.close();
long end = System.currentTimeMillis();
System.out.println(end-start+"ms");
}
推荐使用高效缓冲流的方式进行拷贝,不用规定每次读取多少个字节,因为Buffered内部已经设置好了,效率快
用字符流拷贝
只能拷贝文本文件
这里讲解三种方式
// An highlighted block
//按行拷贝
private static void test1() throws IOException, FileNotFoundException {
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("a1.txt");
BufferedWriter bw = new BufferedWriter(fw);
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
}
上述方式是按行拷贝,一行一行的进行拷贝,注意while的结束条件是为null时
// An highlighted block
//按字符数组大小拷贝
private static void test2() throws IOException {
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("a2.txt");
BufferedWriter bw = new BufferedWriter(fw);
BufferedReader br = new BufferedReader(fr);
char[] c = new char[1024*8];
int len;
while((len = br.read(c))!=-1) {
bw.write(c,0,len);
}
br.close();
bw.close();
}
上述方式是按数组拷贝,while的结束方式是值为-1时,read()方法的返回值是int数据类型,当为空时返回-1
// An highlighted block
//使用高效缓冲流拷贝
private static void test3() throws IOException{
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("a3.txt");
BufferedWriter bw = new BufferedWriter(fw);
BufferedReader br = new BufferedReader(fr);
int len;
while((len=br.read())!=-1) {
bw.write(len);
}
br.close();
bw.close();
}
推荐使用上述方式,高效方便