目录
一、文件和目录
1、文件
2.目录
二、流
1.分类
2.字节流
1.1 文件流(节点流)
1.2 转换字符流
1.3 缓冲流
1.4 对象流(后边必须用 )
2.字符流
2.1 文件流
2.2 缓冲流(后边必须用 )
2.3 文本输出流
三.try-with-resources 自动资源释放
一、文件和目录
二、流
1.分类
1)按照流的数据类型:分为字节流和字符流
2)按照方向:输入和输出
输入: (读) 磁盘 -> 内存
输出:(写) 内存 -> 磁盘
3)按照功能:节点流和处理流
节点流: 直接接触数据源。
处理流(过滤,包装): 提供更多的功能,提高效率。
2.字节流
1.1 文件流(节点流)
FileInputStream(输入)
//字节输入流 FileInputSteram
FileInputStream fs = new FileInputStream("E:/testdate/yao.txt");
//基本的读取
int hand;
fs.read();
while((hand=fs.read())!=-1){
System.out.print((char)(hand));
运行结果:
a bb cc dd ouyang wangyoa nihhao nihaowoshiwangyao
ä½ å¥½ æå¾å¥½ï¼æä¼æåãä½æ¯æéè¦æ¶é´ã
上述的运行结果有乱码,这是因为无法读取汉字,一次只读一个字节。
FileOutputStream(输出)
//字节输入流 FileInputSteram
FileOutputStream fs = new FileOutputStream("E:/testdate/yao.txt");
String str ="你好 nihao haha";
byte [] b = str.getBytes();
fs.write(b);
fs.close();
处理中文:
方式一: 把 字节流 转换成 字符流 InputStreamReader 转换字符流
1.2 转换字符流
// 把 字节流 转换成 字符流 InputStreamReader 转换字符流
FileInputStream fis = new FileInputStream("E:/testdate/yao.txt");
InputStreamReader isr = new InputStreamReader(fis);
//使用char数组 转化为字符
char [] c = new char[1024];
isr.read(c);
System.out.println(c);
//使用while 循环 转为(char)l类型
// int hand;
// while((hand=isr.read())!=-1){
// System.out.print((char)(hand));
// }
isr.close();
方式二: read(byte[])
// 字节流输出汉字
FileInputStream fis = new FileInputStream("E:/testdate/yao.txt");
//将读取的文件内容缓存到 byte数组
byte [] b = new byte[fis.available()];
//读取数组里面的类容
fis.read(b);
//用String将byte数组封装
String str = new String(b);
System.out.println(str);
fis.close();
1.3 缓冲流
BufferedInputStream
// 把 字节流 转换成 字符流 InputStreamReader 转换字符流
FileInputStream fis = new FileInputStream("E:/testdate/yao.txt");
//使用缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
byte [] c = new byte[1024];
bis.read(c);
String s = new String(c);
System.out.println(s);
// int hand;
// while((hand=bis.read())!=-1){
// System.out.print((char)(hand));
// }
bis.close();
fis.close();
BufferedOutputStream
// 输出流
FileOutputStream fos = new FileOutputStream("E:/testdate/output.txt");
//输出缓冲流
BufferedOutputStream bos = new BufferedOutputStream(fos);
String str = "你好呀我是妖君";
byte[] b = str.getBytes();
bos.write(b);
bos.flush();
fos.close();
bos.close();
System.in 控制台输入流
//实现了在控制台输入的内容,传输到文件中
//System 控制台输入流
Scanner sc = new Scanner(System.in);
FileOutputStream fos = new FileOutputStream("E:/testdate/yao.txt");
String n ;
while(true){
n= sc.next();
byte[] b = n.getBytes();
fos.write(b);
}
System.out 控制台输出流 PrintStream 打印输出字节流
//System 控制台输入流
PrintStream ps = new PrintStream("E:/testdate/systemOut.txt");
ps.print("你好呀我是王哟啊和共发生开发发生的能否打开来反抗螺丝钉今年考上的概率");
ps.close();
1.4 对象流(后边必须用 )
ObjectInputStream: 反序列化
把磁盘文件中的对象读出来。
Student yaojun = new Student(18, "妖君", 1);
Student xiaoyao = new Student(24, "逍遥", 5);
FileOutputStream fos = new FileOutputStream("E:/testdate/object.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(yaojun);
oos.writeObject(xiaoyao);
oos.close();
上面一行看不懂的就是我写到磁盘里面的对象,下面我再把它读出来,验证一下。
ObjectOutputStream:序列化
把对象写入到磁盘文件中。
FileInputStream fis = new FileInputStream("E:/testdate/object.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object ro = (Student)ois.readObject();
System.out.println(ro.toString());
ois.close();
fis.close();
结果验证。
2.字符流
文本文件。
2.1 文件流
FileReader
FileWriter
读:
// 字符流
FileReader fr = new FileReader("E:/testdate/yao.txt");
char [] c = new char[1024];
fr.read(c);
System.out.println(c);
写:
// 字符流
FileWriter fw = new FileWriter("E:/testdate/jun.txt");
String str = "niahowdskfjfhfklfj上帝就发四年发丝发货阿斯发生的激发i发生发客户萨佛哈萨克老夫撒谎";
fw.write(str);
fw.close();
2.2 缓冲流(后边必须用 )
读:
FileReader fr = new FileReader("E:/testdate/yao.txt");
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String hand;
while((hand=br.readLine())!=null){
sb.append(hand);
}
System.out.println(sb);
br.close();
写:
FileWriter fw = new FileWriter("E:/testdate/yao.txt");
BufferedWriter bw = new BufferedWriter(fw);
String s ="nsdiasofh是东方家的覅大家是否及时发哈是否阿萨是芬兰卡死的回复覅沙发上哈士大夫";
bw.write(s);
bw.flush();
fw.close();
2.3 文本输出流
PrintWriter: 打印输出字符流
PrintWriter pw = new PrintWriter("E:/testdate/yao.txt");
// 2.
for(int i = 1; i <= 5; i++) {
pw.println("数字是:" + i);
// System.out.println("数字是:" + i);
}
// 3.
pw.close();
三.try-with-resources 自动资源释放
只要流 实现了 AutoCloseable ,就可以进行自动资源释放。
try(PrintWriter pw = new PrintWriter("f:/data/number.txt");){
for(int i = 1; i <= 5; i++) {
pw.println("数字是:" + i);
}
}catch(FileNotFoundException e) {
}catch(IOException e) {
}