Java工具类:Java.IO
Java的IO类中最基本的工具类有四种:InputStream
、OutputStream
、Reader
、Writer
。
输入流 | 输出流 | |
---|---|---|
字节流 | 字节输入流 InputStream | 字节输出流 OutputStream |
字符流 | 字符输入流 Reader | 字符输出流 Writer |
字节流
流:数据(字节,字符),1字符=2字节,1字节=8个二进制位
一切文件数据,皆为二进制数据
public void close()
public void flush()
public void write(byte[] b)
public void write(byte[] b, int off, int len)
public abstract void write(int b)
java.io.FileOutputStream extends OutputStream
FileOutputStream
:文件字节输出流
作用:把内存中的数据写入到硬盘的文件中
字节输出流的使用步骤(重点):
1.创建一个FileOutputStream
对象,构造方法中传递写入数据的目的地
2.调用FileOutputStream
对象中的方法write
,把数据写入到文件中
3.释放资源(流使用会占用一定的内存,使用完毕要把内存清空,提高程序的效率)
字节输出流的续写和换行
FileOutputStream(String name, boolean append)
FileOutputStream(File file, boolean append)
参数:
String name,File file:写入数据的目的地
boolean append:追加写开关
若为true,创建对象不会覆盖源文件,继续在文件的末尾追加写数据
若为false,创建一个新文件,覆盖源文件
public class Demo03outputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("day02-code\\others\\c.txt");
for (int i = 0; i < 10; i++) {
fos.write("你好".getBytes());
fos.write("\r\n".getBytes());
}
fos.close();
}
}
字节输入流
java.io.InputStream
:此抽象类是表示字节输入流的所有类的超类
定义了所有子类共性的方法:
int read()
:从输入流中读取数据的下一个字节
int read(byte[] b)
:从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
java.io.FileInputStream extends InputStream
FileInputStream
:文件字节输入流
作用:把硬盘文件中的数据,读取到内存中使用
字节输入流的使用步骤(重点):
1.创建FileInputStream
对象,构造方法中绑定要读取的数据源
2.使用FileInputStream
对象中的方法read
,读取文件
字节输入流一次读取多个字节的方法:
int read(byte[] b)
从输入流读取一定数量的字节,并将其存储在缓冲区数组b中
String
类的构造方法:
String(byte[] bytes)
: 把字节数组转换为字符串
String(byte[] bytes, int offset, int length)
: 把字节数组的一部分转换为字符串
offset:数组的开始索引
length:转换的字节个数
public class Demo01inputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("day02-code\\others\\a.txt");
int len = fis.read();
System.out.println(len);
len = fis.read();
System.out.println(len);
fis.close();
}
}
字符流
java.io.Reader
:字符输入流,是字符输入流的最顶层的父类,定义了一些共性的成员方法,是一个抽象类
成员方法:
int read()
读取单个字符并返回。
int read(char[] cbuf)
一次读取多个字符,将字符读入数组
void close()
关闭该流并释放与之关联的所有资源
fileReader
:文件字符输入流
作用:把硬盘文件中的数据以字符的方式读取到内存中
构造方法:
FileReader(String filename)
FileReader(File file)
参数:读取文件的数据源
String filename: 文件的路径
File file: 一个文件
FileReader
构造方法的作用:
1.创建一个FileReader
对象
2.把FileReader
对象指向要读取的文件
字符输入流的使用步骤(重点):
1.创建FileReader
对象,构造方法中绑定要读取的数据源
2.使用FileReader
对象中的方法read
读取文件
3.释放资源
public class Demo02Reader {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("day02-code\\others\\c.txt");
int len = 0;
while((len = fr.read()) != -1) {
System.out.print((char)len);
}
fr.close();
}
}
java.io.Writer
: 字符输出流:
共性的成员方法:
void write(int c)
:写入单个字符
void write(char[] cbuf)
:写入字符数组
abstract void write(char[] cbuf, int off, int len)
:写入字符数组的某一部分
off:数组的开始索引
len:写的字符个数
void write(String str)
:写入字符串
void write(String str, int off, int len)
:写入字符串的某一部分
off:字符串的开始索引
len:写的字符个数
void flush()
:刷新该流的缓冲
void close()
:关闭此流。但要先刷新它
java.io.FileWriter extends OutputStreamWriter extends Writer
FileWriter
:文件字符输出流
作用:把文件中的字符数据写入到文件中
构造方法:
FileWriter(File file)
:根据给定的File
对象构造一个FileWriter
对象
FileWriter(String filename)
:根据给定的文件名构造一个FileWriter
对象
参数:写入数据的目的地
String filename
:文件的路径
FIile file
:一个文件
构造方法的作用:
1.创建一个FileWriter
对象
2.会根据构造方法中传递的文件/文件的路径,创建一个文件
3.把FileWriter
对象指向创建好的文件
字符输出流的使用步骤(重点):
1.创建FileWriter
对象,构造方法中绑定要写入数据的目的地
2.使用FileWriter
中的方法write
,把数据写入到内存缓冲区中(字符转换为字节的过程)
3.使用FileWriter
中的方法flush
,把内存缓冲区中的数据,刷新到文件中
4.释放资源(会先把内存缓冲区中的数据刷新到文件中)
public class Demo01Writer {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("day02-code\\others\\d.txt");
fw.write(97);
fw.flush();
fw.close();
}
}
flush
方法和close
方法的区别:
flush
:刷新缓冲区,流对象可以继续使用
close
:先刷新缓冲区,然后通知系统释放资源,流对象不可以再被使用
字符输出流写数据的其他方法:
void write(char[] cbuf)
:写入字符数组
abstract void write(char[] cbuf,int off, int len)
:写入字符数组的某一部分
off:数组的开始索引
len:写的字符个数
void write(String str)
:写入字符串
void write(String str, int off, int len)
:写入字符串的某一部分
off:字符串的开始索引
len:写的字符个数
续写和换行:
续写、追加写:使用两个参数的构造方法
FileWriter(String filename, boolean append)
FileWriter(File file, boolean append)
参数:
String filename
,File file
:写入数据的目的地
boolean append
:续写开关
若为true:不会创建新的文件覆盖源文件
若为false:创建新的文件覆盖源文件
换行:换行符号
windows:\r\n
linux:/n
mac:/r