提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
IO流的学习其中的方法大多数可以依靠API中的方法去查询。所以本章将就所学的IO流基础进行笔记记录。
`
一、什么是IO流?
个人理解:JAVA中的IO流就是对系统中的文件或者文本进行操作。例如:我们想从IDEA(编写java代码的软件)获取系统某.txt文件的内容时,JAVA中就称为输入流。当我们想从IDEA输入内容到.txt内容中时,JAVA中就称为输出流。
JAVA提供了许多方法对IO流进行操作,下面我就简单总结几种操作。
二、IO流的操作
1.File的使用
示例代码如下:
public class File_Class_01 {
public static void main(String[] args) throws IOException {
File file1 = new File("D:\\学习");
System.out.println(file1);
//创建一个新的文件夹
System.out.println(new File(file1,"HH").mkdir());
//从父路径名字字符和子路径名字字符串创建一个新的file实例
File file2 = new File("D:\\学习","student.txt");
System.out.println(file2);
//当且仅当具有此名称的文件尚不存在时,以原子方式创建以此抽象路径名命名的新的空文件。
//需要抛异常
//不能创建word文件
File file3 = new File("D://学习/student.txt");
//createNewFile()判断file创建是否成功
System.out.println(file3.createNewFile());
}
}
输出结果如下:
Tips:IO流不能创建word类型的文档
2.输入流
IO流的使用可以大致分为以下几个步骤:
- 建立目的地(与源文件的联系)
- 构建流(构建通道)
- 使用
- 关闭
示例代码如下:
dpublic class IO_Class_01 {
public static void main(String[] args) throws IOException {
//建立IO流
File f = new File("D://学习/student.txt");
//构建输入流
FileInputStream is = new FileInputStream(f);
//使用
System.out.println((char)(is.read()));
System.out.println((char)(is.read()));
System.out.println((char)(is.read()));
//关闭
is.close();
}
}
输出结果如下:
student.txt中存储内容为:123
上述代码中我们可以看到输出语句有三条,已经重复了。我们可以采用循环去优化上面代码,并且使用IO流的方法**read()**方法。
示例代码如下:
public class IO_Class_02 {
public static void main(String[] args) throws IOException {
//建立联系
File file = new File("D://学习/student.txt");
//构建输入流 使用多态
InputStream is = new FileInputStream(file);
//读入
int i = -1;
while ((i = is.read()) != -1){
System.out.println((char) i);
}
//关闭
is.close();
}
}
因为InputStream的父类为FileInputStream且没有新增方法。
输出结果:
3.输入字符串
当我们想输入字符串时,我们就需要借助byte数组去实现它。
示例代码如下:
public class IO_Class_03 {
public static void main(String[] args) throws IOException {
//建立与数据源文件的联系
//构建输入流
InputStream is = new FileInputStream("D://学习/student.txt");
//读入
// byte[] bytes = new byte[1024];
// int len = -1;
// is.read(bytes);
// System.out.println(Arrays.toString(bytes));
// while ((len = is.read(bytes))!=-1){
// System.out.println(new String(bytes,0,len));
// }
// System.out.println(is.read(bytes));
byte[] bytes = is.readAllBytes();//读数组中所有的字节
System.out.println(new String(bytes));
is.close();
}
}
输出结果:
4.输出流
输出流使用的主要方法就是write()。
示例代码如下:
public class IO_Class_04 {
public static void main(String[] args) throws IOException, InterruptedException {
//构建联系
File file = new File("D://学习/outman.txt");
//创建通道
OutputStream s = new FileOutputStream(file,true);
//使用
s.write(49);
s.write(50);
byte[] bytes = "迪迦".getBytes();
s.write(bytes);
//刷新
s.flush();
//关闭
s.close();
}
}
输出结果:
5.文件的拷贝
文件的拷贝流程:数据源----文件字节输入流---->程序-----文件字节输出流---->目的地
通俗的说文件的拷贝就是需要是实现输入流和输出流两种。
当存在两种流的时候,关闭的顺序需要后进先关
拷贝文件的时候如果输出流要保存的文件不存在将会主动创建该文件。FileOutputStream(pathname,boolean)此方法的boolean值代表的意思为:是否覆盖。若为ture则不覆盖,在后续字符串添加。false则覆盖之前的字符串。
示例代码如下:
public class IO_Class_05 {
public static void main(String[] args) {
InputStream i = null;
OutputStream o = null;
try {
i = new FileInputStream("D://学习/outman.txt");
o = new FileOutputStream("D://学习/student.txt",true);
//读写
byte[] car = new byte[1024];
int len = -1;
while ((len = i.read(car)) != -1) {
o.write(car, 0, len);
o.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (o != null) {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (i != null) {
try {
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
6.try…with…catch
当上述资源需要关闭的太多的时候,我们可以采用try…with…catch去简化代码。
示例代码如下:
public class IO_Class_07 {
public static void main(String[] args) throws IOException {
Reader r = new FileReader("D://学习/outman.txt");
Writer w = new FileWriter("D://学习/student.txt", true);
try (r; w) {
char[] car = new char[1024];
int len = -1;
while ((len = r.read(car)) != -1) {
w.write(car, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
总结
这里对IO流中的一些操作进行简单演示。IO操作中一定要记得最基本的顺序。