字节流/字符流
IO流常用基类
- 字节流的抽象基类:InputStream (输入流),OutputStream (输出流)。
- 字符流的抽象基类:Reader , Writer。
FileOutputStream 文件输出流
private static final Object LINE_SEPARATOR = System.getProperty("line.separator");
public static void main(String[] args) {
File file = new File("D:\\gggg.txt");//往此文件写入
System.out.println();
FileOutputStream fos = null;
try {
//定义输出流对象
fos = new FileOutputStream(file, true); //可以传入true实现续写功能..如果路径文件不存在则自动创建,如果存在,则覆盖
String str = LINE_SEPARATOR + "sss"; //换行续写打印
fos.write(str.getBytes()); //输出流 的写入功能,需要转换成字节
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) { //如果fos为空调用close 则出现空指针异常
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("" + e);
}}}}
FileInputStream 文件输入流
FileInputStream fis = null;
try {
File file = new File("D:\\aaa.txt");
if (!file.exists()) {
throw new RuntimeException("文件不存在"); //判断文件是否存在
}
fis = new FileInputStream(file); //定义输入流对象
byte[] buff = new byte[1024];//缓冲区大小一般为 1024* ?
int num = 0;
while ((num = fis.read(buff)) != -1) { //num:表示往数组中存放字节的个数,-1则表示没存
String s = new String(buff, 0, num);
System.out.println(num + " " + s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
}
}
复制----字节流的缓冲区对象
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//定义流对象
fis = new FileInputStream("E:\\1.jpg");
fos = new FileOutputStream("E:\\a\\abc.jpg");
//定义缓冲区,对流进行缓冲(缓冲功能对象)
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buff = new byte[1024];//缓冲区 字节数组大小一般为 1024* ?
int num = 0;
while ((num = bis.read(buff)) != -1) { // In缓冲区中读取字节存入数组
bos.write(buff,0,num); // Out缓冲区中从数组中写出字节
bos.flush(); // 对Out缓冲区进行刷新,将已写出字节输出到底层输出流中
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}}}}
字符流=字节流+编码表
InputStreamReader 字符输入流------
FileInputStream fis = new FileInputStream("E:\\a.txt");
//InputStreamReader 是字节流通向字符流的桥梁, 自动解码
InputStreamReader isr = new InputStreamReader(fis, "GBK");
char[] chars = new char[1024];
int num = 0;
while ((num = isr.read(chars)) != -1) { // 读取到的字节根据编码表转换成字符传入字符数组
String s = new String(chars, 0, num); //字符数组转换成字符串
System.out.println(num + "---" + s);
}
isr.close();
OutputStreamWriter 字符输出字符流-----
FileOutputStream fos = new FileOutputStream("E:\\aaaaa.txt");
//OutputStreamReader 是字符流通向字节流的桥梁 ,自动编码
OutputStreamWriter osw = new OutputStreamWriter(fos);
//write 方法直接写中文字符,写入数据时,都会存储到缓冲区中因为要查表
osw.write("风6吹龙笛7击鼍鼓8皓齿歌细腰舞况是青春日将暮");
osw.flush(); //需要刷新缓冲区,将数据存到目的地,流可以继续使用
osw.close(); //close中也有flush();,关闭后流不能使用
FileReader/FileWrite
- 用于操作字符文件的便捷类,只能使用默认文本
FileReader fr = new FileReader("E:\\a.txt");
FileWriter fw = new FileWriter("E:\\b.txt");
/*等效于以下
FileOutputStream fos = new FileOutputStream("E:\\aaaaa.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos); */
BufferedReader bfr = new BufferedReader(fr);//创建缓冲区对象,明确缓冲对象
BufferedWriter bfw = new BufferedWriter(fw);
char[] chars = new char[1024];
int num = 0;
while ((num=bfr.read(chars))!=-1) {
bfw.write(chars);
bfw.newLine(); //换行写入
bfw.write("我去");
}
bfr.close();
bfw.close();
练习
将文件夹指定类型的文件绝对路径写出
File file = new File("E:\\IntelliJ IDEA 2017.3.4");
List<File> al = getList(file);
FileWriter fw = new FileWriter("E:\\s.txt");
BufferedWriter bfw = new BufferedWriter(fw);
for (File f : al) { //遍历集合
System.out.println(f.getAbsolutePath());
bfw.write(f.getAbsolutePath());
bfw.newLine();
bfw.flush();
}
bfw.close();
将键盘录入写入到文件TXT
public class Main {
public static void main(String[] args) throws IOException {
File file=new File("E:\\a.txt");
Comparator<Student> com= Collections.reverseOrder();
Set<Student> set=getStudent(com);
writeInfo(set,file);
}
//获取学生对象
public static Set<Student> getStudent() throws IOException {
return getStudent(null);
}
public static Set<Student> getStudent(Comparator<Student> com) throws IOException {
//键盘录入
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
Set<Student> set = null;//创建一个集合存储学生对象
if (com!=null){ //如果有比较器存在就创建带比较器的集合
set=new TreeSet<>(com);
}else {
set=new TreeSet<>();
}
//获取键盘录入信息
String line = null;
while ((line = bfr.readLine()) != null) {
//设置键盘录入结束标记
if (line.equals("over")){
break;
}
//录入的信息是用","隔开,所以用","分割
String[] strs = line.split(",");
//创建学生对象
Student student = new Student(strs[0], Integer.parseInt(strs[1]),
Integer.parseInt(strs[2]), Integer.parseInt(strs[3]));
set.add(student);
}
bfr.close();
return set;
}
//将集合中的学生信息写入到文件中
public static void writeInfo(Set<Student> set, File file) throws IOException {
BufferedWriter bfw = null;
try {
bfw = new BufferedWriter(new FileWriter(file));
for (Student s : set) {
bfw.write(s.getName() + " " + s.getSun());
bfw.newLine();
bfw.flush();
}
} finally {
if (bfw != null) {
try {
bfw.close();
} catch (IOException e) {
e.printStackTrace();
}}} }}
IO流总结
-
File 对象,对文件的基本操作,创建 删除 获取 隐藏…
-
明确要使用的IO体系 :
字节流:InputStream OutputStream
字符流:Reader Writer -
操作的数据如果是纯文本就用字符流,否则使用字节流.如,图片音频使用字节流
-
明确源设备: 硬盘(file) . 键盘(System.in) . 内存(数组) . 网络(socket流) .
-
都存在缓冲区功能对象
-
字节流的writer一次只写出一个字节,也就是讲一个整数的最低8位写出
writer(353);最低8位是97,写出a;