IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)
1.File类
File类可以在程序中 操作文件和目录。File类是通过建立File类对象,在调用File类的对象来进行相关操作的。
示例:
- public class Demo01 {
- public static void main(String[] args) {
- File f = new File("f:/我的歌声里.txt");
- //访问文件名相关
- String name = f.getName();
- System.out.println("文件名:" + name);
- String absolutePath = f.getAbsolutePath();
- System.out.println("绝对路径:" + absolutePath);
- String parent = f.getParent();
- System.out.println("父目录:" + parent);
- //检测相关
- System.out.println("是否存在:" + f.exists());
- System.out.println("是否可读" + f.canRead());
- System.out.println("是否可写:" + f.canWrite());
- //获取文件信息
- System.out.println("文件的大小: " + f.length());
- //以当前路径创建File对象
- File file = new File(".");
- String[] list = file.list();
- //遍历目录下的文件
- System.out.println();
- System.out.println("当前目录下有文件:");
- for(String name1:list){
- System.out.println(name1);
- }
- }
- }
public class Demo01 {
public static void main(String[] args) {
File f = new File("f:/我的歌声里.txt");
//访问文件名相关
String name = f.getName();
System.out.println("文件名:" + name);
String absolutePath = f.getAbsolutePath();
System.out.println("绝对路径:" + absolutePath);
String parent = f.getParent();
System.out.println("父目录:" + parent);
//检测相关
System.out.println("是否存在:" + f.exists());
System.out.println("是否可读" + f.canRead());
System.out.println("是否可写:" + f.canWrite());
//获取文件信息
System.out.println("文件的大小: " + f.length());
//以当前路径创建File对象
File file = new File(".");
String[] list = file.list();
//遍历目录下的文件
System.out.println();
System.out.println("当前目录下有文件:");
for(String name1:list){
System.out.println(name1);
}
}
}
运行结果:
2.IO流的分类
按照方向:输入流和输出流
按照流的大小:字节流和字符流
按照流的角色:节点流和处理流
流的类关系图如下:
3.字节流和字符流
字节流:FileInputStream 和 FileOutputStream
示例:把文件复制成另外的文件
- public class Demo02 {
- public static void main(String[] args) throws IOException {
- /*
- * 需求把文件“我的歌声里.txt” 复制并改文件名为 “我的歌声里.java”
- *
- */
- //创建输入流
- InputStream is = new FileInputStream(new File("f:/我的歌声里.txt"));
- //创建输出流
- OutputStream os = new FileOutputStream(new File("f:/我的歌声里.java"));
- //创建接收字节数组
- byte[] bytes = new byte[1024];
- int len = 0;
- //循环输入输出
- while((len = is.read(bytes)) != -1 ){
- os.write(bytes, 0, len);
- }
- //关闭资源
- os.close();
- is.close();
- }
- }
public class Demo02 {
public static void main(String[] args) throws IOException {
/*
* 需求把文件“我的歌声里.txt” 复制并改文件名为 “我的歌声里.java”
*
*/
//创建输入流
InputStream is = new FileInputStream(new File("f:/我的歌声里.txt"));
//创建输出流
OutputStream os = new FileOutputStream(new File("f:/我的歌声里.java"));
//创建接收字节数组
byte[] bytes = new byte[1024];
int len = 0;
//循环输入输出
while((len = is.read(bytes)) != -1 ){
os.write(bytes, 0, len);
}
//关闭资源
os.close();
is.close();
}
}
运行结果:
字符流:FileReader和FileWriter
示例:切割文件
- public class Demo03 {
- public static void main(String[] args) {
- FileReader r = null;
- FileWriter w = null;
- try {
- int count = 0;//定义一个标记
- int flag = 0;//文件名标记
- r = new FileReader("f:/我的歌声里.txt");
- w = new FileWriter("f:/我的歌声里" + flag +".txt");
- char[] chars = new char[10];
- int len = 0;
- while((len = r.read(chars)) != -1){
- System.out.println(new String(chars, 0, len));
- w.write(chars, 0, len);
- w.flush();
- count++;
- //定义切割的条件
- if(count >10 ){
- flag++;
- w = new FileWriter("f:/我的歌声里" + flag +".txt");
- count = 0;
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- try {
- w.close();
- r.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
public class Demo03 {
public static void main(String[] args) {
FileReader r = null;
FileWriter w = null;
try {
int count = 0;//定义一个标记
int flag = 0;//文件名标记
r = new FileReader("f:/我的歌声里.txt");
w = new FileWriter("f:/我的歌声里" + flag +".txt");
char[] chars = new char[10];
int len = 0;
while((len = r.read(chars)) != -1){
System.out.println(new String(chars, 0, len));
w.write(chars, 0, len);
w.flush();
count++;
//定义切割的条件
if(count >10 ){
flag++;
w = new FileWriter("f:/我的歌声里" + flag +".txt");
count = 0;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
w.close();
r.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
运行结果
4.转换流
转换流:把字节流转换为字符流,一次来实现性能优化
InputStreamReader 和 OutputStreamWriter
示例:
- public class Demo04 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- //键盘输入到文件
- InputStreamReader isr = new InputStreamReader(System.in);
- char[] chars = new char[1024];
- int len = 0;
- OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f:/我的文件.txt"));
- int count = 0;
- while((len = isr.read(chars)) != -1){
- osw.write(chars, 0, len);
- osw.flush();
- count++;
- if(count == 10){
- break;
- }
- }
- isr.close();
- osw.close();
- }
- }
public class Demo04 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//键盘输入到文件
InputStreamReader isr = new InputStreamReader(System.in);
char[] chars = new char[1024];
int len = 0;
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f:/我的文件.txt"));
int count = 0;
while((len = isr.read(chars)) != -1){
osw.write(chars, 0, len);
osw.flush();
count++;
if(count == 10){
break;
}
}
isr.close();
osw.close();
}
}
运行结果:
5.缓冲流
把流读到缓冲区,然后再一次读到内存中来,以此来提高性能
BuffererInputStream 和BufferedOutputStream
BufferedReader 和 BufferedWriter
示例:
- public static void main(String[] args) throws IOException {
- //读取并复制保存图片
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("f:/qq.jpg")));
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("f:/qqq.png")));
- String line = null;
- while((line = br.readLine()) != null){
- bw.write(line);
- bw.flush();
- }
- bw.close();
- br.close();
- }
public static void main(String[] args) throws IOException {
//读取并复制保存图片
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("f:/qq.jpg")));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("f:/qqq.png")));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.flush();
}
bw.close();
br.close();
}</pre>
运行结果:

6.对象序列化
对象流:ObjectInStream和 ObjectOutputStream
Serialiazable关键字:标记接口可序列化
transient关键字:标记瞬态实例变量
示例:
- public class Demo06 {
- /**
- * @param args
- * @throws IOException
- * @throws FileNotFoundException
- * @throws ClassNotFoundException
- */
- public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
- Student s1 = new Student("小红", 19);
- Student s2 = new Student("小白", 18);
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f:/序列化.txt"));
- oos.writeObject(s1);
- oos.writeObject(s2);
- s2.setName("小白白");
- oos.writeObject(s2);//更改变量的属性,即使重新序列化也不会改变原属性值
- oos.close();
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f:/序列化.txt"));
- Student rs1 = (Student) ois.readObject();
- Student rs2 = (Student) ois.readObject();
- System.out.println(rs1);
- System.out.println(rs2);
- }
- }
- class Student implements Serializable{
- private String name;//学生姓名
- private transient int age;//年龄设置为瞬时变量,将不被序列化
- public Student(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "该学生的名字为:" + name + ",年龄为:" + age;
- }
- }
public class Demo06 {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Student s1 = new Student("小红", 19);
Student s2 = new Student("小白", 18);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f:/序列化.txt"));
oos.writeObject(s1);
oos.writeObject(s2);
s2.setName("小白白");
oos.writeObject(s2);//更改变量的属性,即使重新序列化也不会改变原属性值
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f:/序列化.txt"));
Student rs1 = (Student) ois.readObject();
Student rs2 = (Student) ois.readObject();
System.out.println(rs1);
System.out.println(rs2);
}
}
class Student implements Serializable{
private String name;//学生姓名
private transient int age;//年龄设置为瞬时变量,将不被序列化
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "该学生的名字为:" + name + ",年龄为:" + age;
}
}
运行结果:

IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)的更多相关文章
- 09、IO流—File类与IO流
目录 一.File类 基本认识 实用方法 获取功能 重命名功能(包含剪切) 判断功能 创建.删除文件 实际小案例 二.IO流 1.认识IO流 2.IO流基类介绍 字节流基类介绍 字符流基类介绍 三.节 ...
- Java—IO流 File类的常用API
File类 1.只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. package cn.test; import java.io.File; import java.io.IOE ...
- java io包File类
1.java io包File类, Java.io.File(File用于管理文件或目录: 所属套件:java.io)1)File对象,你只需在代码层次创建File对象,而不必关心计算机上真正是否存在对 ...
- Java IO之File和IO
本系列我们主要总结一下Java中的IO.NIO以及NIO2. java.io.File 学习Java IO,首先让我们来了解File类吧,它是文件和目录路径名的抽象表示形式.因此你千万别误会File类 ...
- java IO之 File类+字节流 (输入输出 缓冲流 异常处理)
1. File类
- Java基础---IO(二)--File类、Properties类、打印流、序列流(合并流)
第一讲 File类 一.概述 1.File类:文件和目录路径名的抽象表现形式 2.特点: 1)用来将文件或文件夹封装成对象 2)方便于对文件与文件夹的属性信息进行操作 3)File类的实例是不 ...
- 021.1 IO流——File类
########################################IO流: IO:用于处理设备上的数据的技术.设备:内存,硬盘,光盘 流:系统资源,Windows系统本身就可 ...
- IO流——File类(文件流类)
java语言的输入输出操作是借助于输入输出包java.io来实现的,按传输方向分为输入流与输出流,从外设传递到应用程序的流为输入流,将数据从应用程序输入到外设的流为输出流. File类的构造方法: 1 ...
- JAVA:IO流——File类
1.掌握File 类的作用 2.可以使用File 类中的方法对文件进行操作 所有的 io 操作都保存在 java.io 包中. 构造方法:public File (String pathname) 直 ...
- Java IO流-File类
2017-10-24 23:50:22 File类 File类:文件和目录路径名的抽象表示形式.该文件或者目录不一定真实存在. * 构造方法 File类有四种构造方法,主要采用的构造方法师第二种,也就 ...
随机推荐
- c#关键字及ref和out
最近在写程序时遇到ref,out 参数问题.回头有自习看了看MSDN,才有巩固了基础.我把我的测试程序贴出来,大家分享一下. ref 关键字使参数按引用传递.其效果是,当控制权传递回调用方法时, ...
- HTML基础--position 绝对定位 相对定位 锚点链接
position 定位属性,检索对象的定位方式 一.语法:position:static /absolute/relative/fixed 取值: 1.static:默认值,无特殊定位,对象遵循HTM ...
- MySQL相关的书籍
http://item.jd.com/11389754.htmlhttp://item.jd.com/11390423.html http://item.jd.com/11398721.html
- 用MVC导入导出
导入导出对于刚做的人一脸懵逼,但是明白思路之后就感觉非常容易,我也是研究了好久,才总算做了出来,放在这里给大家分享一下 一.先看下导出 视图脚本 <script type ="text ...
- sort学习 - LeetCode #406 Queue Reconstruction by Height
用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和rev ...
- Python入门 - 环境搭建
因为本人用的mac系统,所以这里只演示mac系统下python环境的搭建,至于windows和linux系统有类似安装过程,可以参考官方文档. 第一步: 安装python3.6 # Mac OS X ...
- linux磁盘管理系列-软RAID的实现
1 什么是RAID RAID全称是独立磁盘冗余阵列(Redundant Array of Independent Disks),基本思想是把多个磁盘组合起来,组合一个磁盘阵列组,使得性能大幅提高. R ...
- 【17-06-16】Java入门测试题,测测你基础知识掌握程度(附答案及个人解析)
描述 前几天在知乎里看到一份这样的题,当时只是随便做了一下,对了一下答案.昨天又有了一份进阶的题,里面有些还是需要记录一下,于是就从这个入门的题开始. 题目和答案来自阿里云大学 - 知乎专栏 题目 现 ...
- [PHP基础]有关isset empty 函数的面试题
用isset()和empty()判断下面的变量. $str = ''; $int = 0 ; $arr = array(); isset($str) 返回的是 true 还是 false empty( ...
- phpstorm快捷键记录
快捷键记录 Ctrl + N 按类名查找Ctrl + Shift + N 按文件名查找,快速查找文件Ctrl + Shift+Alt+N 根据函数名查找Ctrl + F 当前文件查找Ctrl + Sh ...