IO流之字符流(下)

1.1 文件操作字符流

1.1.1 字符流特征

字符流 = 字节流 + 解码过程
字节组合操作 == 》 对应当前环境编码集的一个字符,如果字符找不到,该数据无效,需要被删除

1.1.2 文件操作输入字符流 FileReader

constructor 构造方法

FileReader(File file) 根据File类对象创建对应的FileReader字符流输入对象
FileReader(String pathName) 根据String类型文件路径创建对应的FileReader字符输入流对象,如果文件不在,抛出异常FileNotFoundException

Method 成员方法

int read0 读取文件中的一个字符数据,通过返回值返回,返回值类型是int类型,但是在int类型中有且只有低16位数据有效
int read(char[] arr) 读取文件中的数据保存到字符数组中,返回值类型是读取到的字符个数
int read(char[] arrant off, int length) 读取到文件中的数据保存到字符数组中,要求从数组下标off开始到长度为length结束,返回值类类型是读取到的字符个数

FileReader 代码演示:

package bkDay14.IO;

import java.io.FileReader;
import java.io.IOException;

/*
 * 文件操作输入字符流【不是字节流】
 */
public class Demo4 {
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		//test1();
		test2();
		long end = System.currentTimeMillis();
		System.out.println("时间:" + (end - start));
	}
	
	public static void test2() {
		FileReader fr = null;
		try {
			fr = new FileReader("/Users/xiao.shuo./Desktop/a.txt");
			char[] buf = new char[1024 * 8];
			int length = -1;
			while((length = fr.read(buf)) != -1) {
				System.out.println(new String(buf, 0, length));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void test1() {
		FileReader fr = null;
		try {
			//创建文件输入字符流
			fr = new FileReader("/Users/xiao.shuo./Desktop/a.txt");
			int content = -1;
			//读取文件
			while((content = fr.read()) != -1) {
				System.out.println((char)content);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

1.1.3 文件操作输出字符流 FileWriter

Constructor 构造方法

FileWriter(File file) 根据File类对象创建对应文件的文件操作输出自如流
FileWriter(String pathName) 根据String类型文件路径创建对应文件的文件操作输出字符流
FileWriter(String pathName, boolean append) 根据String类型文件路径创建对应文件的文件操作输出字符流,并要求追加写
FileWriter(File file) 根据File类对象创建对应文件的文件操作才输出字符流,并要求追加写

如果创建FileWriter对象时,文件不存在,但是路径合法,这里会创建对应的操作文件。如果路径不合法,则会抛出异常FileNotFoundException

Method 成员方法

void write(int ch) 写入一个char类型数据到文件中
void write(char[] arr) 写入一个char类型数组到文件中
void write(char[] arrant offing length) 写入一个char类型数组到文件中,要求从off下标位置开始,到长度为lenth结束

FileWriter 代码演示;

package bkDay15.Io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 文件操作字符输出流
 */
public class Demo1_filewriter {
	public static void main(String[] args) {
		//test1();
		test2();
	}
	
	public static void test2() {
		FileWriter fw = null;
		try {
			fw = new FileWriter("/Users/xiao.shuo./Desktop/b.txt",true);
			char[] arr = "武汉加油".toCharArray();
			fw.write(arr);
			fw.write("\n\r");
			String str = "中国加油";
			fw.write(str, 0, str.length());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void test1() {
		FileWriter fw = null;
		try {
			fw = new FileWriter(new File("/Users/xiao.shuo./Desktop/a.txt"),true);
			fw.write('武');
			fw.write('汉');
			fw.write('加');
			fw.write('油');
			fw.write(',');
			fw.write('中');
			fw.write('国');
			fw.write('加');
			fw.write('油');
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

1.1.4 字符流实现文件的拷贝

代码如下:

package bkDay15.Io;

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

/*
 * 使用文件操作字符流拷贝非文本文件问题
 */
public class Demo2_frfw_copy {
	public static void main(String[] args) {
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fr = new FileReader("/Users/xiao.shuo./Desktop/a.txt");
			fw = new FileWriter("/Users/xiao.shuo./Desktop/ab.txt");
			
			char[] buff = new char[1024 * 4];
			int length = -1;
			while((length = fr.read(buff)) != -1) {
				fw.write(buff,0,length);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

发布了6 篇原创文章 · 获赞 3 · 访问量 641

猜你喜欢

转载自blog.csdn.net/weixin_42451402/article/details/104577183