java day030 IO流
IO流概述
class InputStream 字节输入流基类
--| class FileInputStream 文件操作字节输入流
class OutputStream 字节输出流基类
--| class FileOutputStream 文件操作字节输出流
class Reader 字符输入流基类
--| class FileReader 文件操作字符输入流
class Writer 字符输出流基类
--| class FileWriter 文件操作字符输出流
缓冲流
BufferedInputStream
字节输入缓冲流
BufferedOutputStream
字节输出缓冲流
BufferedReader
字符输入缓冲流
BufferedWriter
字符输出缓冲流
FileInputStream文件操作字节输入流
constructor构造方法
FileInputStream(String filePath);
根据用户指定的路径
若文件不存在,则抛出异常FileNotFoundException
FileInputStream(File file);
根据用户指定的File类对象创建
若文件不存在,则抛出异常FileNotFoundException
Method成员方法
int read();
从文件中读取一个数据返回。如果读到文件末尾返回-1 EOF
int read(byte[] buf);【重点】
从文件读取数据到缓冲数组buf中,返回值是读取到的字节个数
读取的过程中如果出现问题,抛出IOException
操作流程:
1.明确文件路径
2.创建流,打开管道
3.用管子的方法读取数据
4.关闭管子
public class TestFileInputStream {
public static void main(String[] args) throws IOException {
//1.确定文件
File file = new File("D:/test/1.txt");
//创建流。也可以看作是一根管子,连接这个文件
FileInputStream fis = new FileInputStream(file);
//读取第一个字符,并以转为字符类型
System.out.println((char)fis.read());
//使用单一数组读取整个文件
// int count = -1;
// while ((count = fis.read()) != -1) {
// System.out.print((char)count);
// }
//使用缓冲数组
byte[] buf = new byte[1024 * 8];
int length = -1;
while ((length = fis.read(buf)) != -1) {
String s = new String(buf, 0 , length);
System.out.println(s);
}
//关闭通道
fis.close();
}
}
FileOutputStream文件操作字节输出流
constructor构造方法
FileOutputStream(String filePath);
根据用户指定的路径,创建对应的FileOutputStream输出流,
若路径不合法,则抛出异常FileNotFoundException
采用【删除写】
FileInputStream(File file);
根据用户指定的File类对象创建
若路径不合法,则抛出异常FileNotFoundException
采用【删除写】
FileOutputStream(String filePath, boolean append);
根据用户指定的路径,创建对应的FileOutputStream输出流,
若路径不合法,则抛出异常FileNotFoundException
采用【追加写】 写在文件末尾
FileOutputStream(File file, boolean append);
根据用户指定的路径,创建对应的FileOutputStream输出流,
若路径不合法,则抛出异常FileNotFoundException
采用【追加写】 写在文件末尾
Method成员方法
void write(int b);
写入一个字节数据到文件中
void write(byte[] buf);
写入一个字节数组到文件中
void write(byte[] buf, int off, int count);
写入一个字节数组到文件中,从off下标开始,计数count个
【注意】
1.FileOutputStream拥有创建文件的能力
2.区分删除写和追加写
public class TestFileOutputStream {
public static void main(String[] args) throws IOException {
//1.确定文件
File file = new File("D:/test/2.txt");
//2.连接管子
FileOutputStream fos = new FileOutputStream(file);
//3.写入一个字符,这里数字1编码集对应的字符为49
fos.write((char)49);
//写入一个字符数组
fos.write("12345465".getBytes());
fos.write("ajhdnkash".getBytes(), 0, 2);
//4.关闭管子
fos.close();
}
}
FileReader 文件操作字符输入流
constructor构造方法
FileReader(String filePath);
根据用户指定的路径
若文件不存在,则抛出异常FileNotFoundException
FileReader(File file);
根据用户指定的File类对象创建
若文件不存在,则抛出异常FileNotFoundException
Method成员方法
int read();
从文件中读取一个字符数据返回。如果读到文件末尾返回-1 EOF
int read(char[] buf);【重点】
从文件读取数据到缓冲数组buf中,返回值是读取到的字符个数
读取的过程中如果出现问题,抛出IOException
public class TestFileReader {
public static void main(String[] args) throws IOException {
File file = new File("D:/test/1.txt");
FileReader fr = new FileReader(file);
System.out.println((char)fr.read());
// int count = -1;
// while ((count = fr.read()) != -1) {
// System.out.print((char)count);
// }
int length = -1;
char[] buf = new char[1024 * 8];
while ((length = fr.read(buf)) != -1) {
System.out.print(new String(buf, 0, length));
}
fr.close();
}
}
FileWriter文件操作字符输出流
constructor构造方法
FileWriter(String filePath);
根据用户指定的路径,创建对应的FileOutputStream输出流,
若路径不合法,则抛出异常FileNotFoundException
采用【删除写】
FileWriter(File file);
根据用户指定的File类对象创建
若路径不合法,则抛出异常FileNotFoundException
采用【删除写】
FileWriter(String filePath, boolean append);
根据用户指定的路径,创建对应的FileOutputStream输出流,
若路径不合法,则抛出异常FileNotFoundException
采用【追加写】 写在文件末尾
FileWriter(File file, boolean append);
根据用户指定的路径,创建对应的FileOutputStream输出流,
若路径不合法,则抛出异常FileNotFoundException
采用【追加写】 写在文件末尾
Method成员方法
void write(int ch);
写入一个字节数据到文件中
void write(char[] buf);
写入一个字节数组到文件中
void write(char[] buf, int off, int count);
写入一个字节数组到文件中,从off下标开始,计数count个
【注意】
1.FileWriter拥有创建文件的能力
2.区分删除写和追加写
public class TestFileWriter {
public static void main(String[] args) throws IOException {
File file = new File("D:/test/1.txt");
FileWriter fw = new FileWriter(file, true);
fw.write(49);
fw.write("1243532".toCharArray());
fw.write("1876823".toCharArray(), 0, 6);
fw.close();
}
}
文件拷贝
字节流
public class TestCopy {
public static void main(String[] args) throws IOException {
//确定源文件
File srcFile = new File("D:/test/1.txt");
//确定目的地
File deskFile = new File("D:/test/byteStream.txt");
//建好输入管子
FileInputStream fis = new FileInputStream(srcFile);
//建好输出管子
FileOutputStream fos = new FileOutputStream(deskFile);
byte[] buf = new byte[1024 * 16];
int length = -1;
while ((length = fis.read(buf)) != -1) {
fos.write(buf, 0, length);
}
//关闭管道
fos.close();
fis.close();
}
}
字符流
public class TestCharStream {
public static void main(String[] args) throws IOException {
// 确定源文件
File srcFile = new File("D:/test/2.txt");
// 确定目的地
File deskFile = new File("D:/test/charStream.txt");
// 建好输入管子
FileReader fr = new FileReader(srcFile);
// 建好输出管子
FileWriter fw = new FileWriter(deskFile);
char[] buf = new char[1024 * 8];
int length = -1;
while ((length = fr.read(buf)) != -1) {
fw.write(buf);
}
fw.close();
fr.close();
}
}
注意
缓冲数组形式操作比单字节或者单字符操作要有效率得多