Java IO(IO流)-1

时间:2024-01-17 23:24:26

IO流 第一部分 (outputStream/InputStream Writer/Redaer)

IO流对象中输入和输出是相辅相成的,输出什么,就可以输入什么.

IO的命名方式为前半部分能干什么,后半部分是父类的名字. (FileOutputStream 文件输出流)

java->JVM->OS

流对象使用的基本步骤

  1. 创建流的子类对象,绑定数据目的地(文件路径)

  2. 使用write方法写或read方法读

  3. close()方法关闭流对象 , 释放资源

输出流的异常处理

  • try catch finally 因为最后要关闭资源,所以要有finally来执行关闭资源的选项
                 FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
}catch (IOException e) {
System.out.println(e.toString() + "----");
} finally {
//一定要判断fos是否为null,只有不为null时,才可以关闭资源
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("");
}
}
}

输出流

OutputStream

  • OutputStream此抽象类,是表示输出字节流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法。

    • write(int b) 将指定的字节写入输出流,一次写入一个,传递的ASCII码(负数是汉字)

    • write(byte[] b) 将b.length个字节写入输出流

    • write(byte b[],int off,int len) 将b数组中从off开始的len个字节写入输出流

    • close() 关闭流对象,并且释放流对象的资源 , (流对象操作的是操作系统中的资源,释放这里资源)

FileOutputStram

  • 文件输出流,是用于将数据写入 File的输出流。

构造方法

流的构造方法可以创建文件 默认如果存在相同的文件,会覆盖, 可以设置 boolean append来修改位追加内容

  • FileOutputStram(File file) 传递一个File FileOutputStream(File file, boolean append) // 是否追加内容

  • FileOutputStram(String filepath) FileOutputStream(String name, boolean append)

写入文件方式(换行符的方法 /r/n)

					FileOutputStream fos = new FileOutputStream("c:\\a.txt");
//流对象的方法write写数据
//写1个字节
fos.write(97);
//关闭资源
fos.close(); byte[] bytes = {65,66,67,68};
fos.write(bytes); //写字节数组的一部分,开始索引,写几个
fos.write(bytes, 1, 2); //写入字节数组的简便方式
//写字符串
fos.write("hello".getBytes()); //关闭资源
fos.close();

输入流 (InputStream)

InputStream

所有输入流的超类, 一次读取一个字节

  • int read() // 读一个字节,返回读取到的字节 当读取到文件的结尾的时候,将会返回-1

  • read(byte[] b) // 从输入流中读入一定量的字节,并将其存储到缓存区数组byte[] b 中 , 使用数组来提高读取效率,返回的为读取到的字符数量

  • read(byte[] ,int ) // 将输入流中最多 len 个数据字节读入 byte 数组。

数组读取内容的方式

  1. 从文件中读取一个字符到数组中的初始位置,然后继续读,只到数组长度满了或者到了文件结尾

  2. 当第二次读的时候会将读取到的字符依次放入数组中

Java IO(IO流)-1

FileInputStream

  • FileInputStream(File file)

  • FileInputStream(String filename)

循环的方式来读取文件内容

					FileInputStream fis = new FileInputStream("c:\\a.txt");
//创建字节数组
byte[] b = new byte[1024];
int len = 0 ;
while( (len = fis.read(b)) !=-1){ // 将字符读到了byte数组中,如果到了文件结尾会返回-1
System.out.print(new String(b,0,len));
}
fis.close();

文件的移动(赋值/移动/上传)

创建一个输入流,创建一个输出流,从输入流读取字符同时将字符写入到目标文件

						FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("c:\\t.zip");
fos = new FileOutputStream("d:\\t.zip");
//定义字节数组,缓冲
byte[] bytes = new byte[1024*10];
//读取数组,写入数组
int len = 0 ;
while((len = fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
}
}catch(IOException ex){
throw new RuntimeException("文件复制失败");
}finally{
try{ // 需要将两个流关闭的方式全部用try/catch因为如果一下流出现异常,那么另一个流也需要关闭
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException("释放资源失败");
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException ex){
throw new RuntimeException("释放资源失败");
}
}
}

字符流(主要用来读文本文件)

Writer 字符输出流

字符输出数组的超类

  • write(int c) : 写1个字符

  • write(char[] c) : 写字符数组

  • write(char[] c,int,int) : 字符数组一部分,开始索引,写几个

  • write(String s) : 写入字符串

FileWriter (Writer的子类)

  • FileWriter(String path) FileWriter(File file)

写入字符流以后必须进行flush()刷新 Close()会自动刷新(不提倡)

    					FileWriter fw = new FileWriter("c:\\1.txt");

					//写1个字符
fw.write(100);
fw.flush(); //写1个字符数组
char[] c = {'a','b','c','d','e'};
fw.write(c);
fw.flush(); //写字符数组一部分
fw.write(c, 2, 2);
fw.flush(); //写如字符串
fw.write("hello");
fw.flush(); fw.close();

Reader (字符输入流)

所有字符输入流的超类

  • int read() : 读取单个字符

  • int read(char[] cbuf) : 将字符读入数组

  • abstract int read(char[] cbuf, int off, int len) : 将字符读入数组的某一部分。

FileReader

					FileReader fr = new FileReader("c:\\1.txt");
/*int len = 0 ;
while((len = fr.read())!=-1){
System.out.print((char)len);
}*/
char[] ch = new char[1024];
int len = 0 ;
while((len = fr.read(ch))!=-1){
System.out.print(new String(ch,0,len));
} fr.close();

字符流中flush() 和close() 的区别

  • flush() 刷新该流的缓冲,刷新完以后可以继续写

  • close() 在关闭以前会刷新缓冲,但是刷新完会关闭流,不可以再继续进行操作.