I/O流
主要内容
java.io.File类的使用(计算机操作系统中的文件和文件夹)
IO原理及流的分类(input output)
文件流:(数据流的读写都是基于文件的操作)
FileInputStream / FileOutputStream / FileReader / FileWriter
缓冲流:(数据流的读写都是基于内存的操作)
BufferedInputStream / BufferedOutputStream /
BufferedReader / BufferedWriter
转换流
InputStreamReader / OutputStreamWriter
标准输入/输出流
打印流(了解)
PrintStream / PrintWriter
数据流(了解)
DataInputStream / DataOutputStream
对象流 ----涉及序列化、反序列化
ObjectInputStream / ObjectOutputStream
随机存取文件流
RandomAccessFile
一、File 类
File类能操作文件本身但不能操作文件内容
java.io.File类:文件和目录路径名的抽象表示形式,与平台无关
File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。
File对象可以作为参数传递给流的构造函数
File类的常见构造方法:
public File(String pathname)
以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
1.访问文件名
- getName( ) 获取文件名
- getPath( ) 获取文件或者文件夹的路径,就是new file时候写的路径
- getAbsoluteFile( ) 返回一个用当前的文件的绝对路径构建的file对象
- getAbsolutePath( ) 获取当前文件的绝对路径
- getParent( ) 返回当前文件或者文件夹的父级路径
- renameTo(Flie newName)给文件夹重命名
2.文件检测
- exists( ) 判断文件或者文件夹是否存在
- canWrite( ) 判断文件是否可写
- canRead( ) 判断文件是否可读
- isFile( ) 判断当前的file对象是不是文件
- isDirectory( ) 判断房前的file对象是不是文件夹或者目录
3.获取常规文件信息
- lastModify( )获取文件的最后修改时间,返回的是一个毫秒数
- Length( )反回文件的长度,单位是字节数
4.文件操作相关
- createNewFile( )创建新的文件
- delete( ) 删除文件
5.目录操作相关
- mkDir( ) 创建单层目录
- list( ) 返回的是当前文件夹的子集的名称,包括目录和文件
- listFiles( )创建多层目录
import java.io.File;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
File file = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\tt.txt");
File file1 = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a");
System.out.println(file.exists());
System.out.println(file1.exists());
System.out.println(file.getName());//获取文件名
System.out.println(file1.getName());
System.out.println(file.getPath());//获取相对路径
System.out.println(file1.getAbsolutePath());//获取绝对路径
File f5 = new File("src/day12/Test.java");//使用相对路径来创建file对象
System.out.println(f5.getPath());//获取文件或者文件夹的路径,就是new file时候写的路径
System.out.println(f5.getAbsolutePath());//获取当前文件的绝对路径
System.out.println(f5);
System.out.println(f5.getAbsoluteFile());//返回一个用当前的文件的绝对路径构建的file对象
file.renameTo(new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\tt1.txt"));//给文件重命名
File file2 = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\tt1.txt");
System.out.println(file2.exists());
File file3 = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\tt1.txt");
System.out.println(file3.exists());
System.out.println(file2.canRead());//判断是否可写
System.out.println(file2.canWrite());//判断是否可读
File file4 = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\tt1.txt");
File file5 = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a");
System.out.println(file4.isFile());//判断当前的file对象是否为文件
System.out.println(file5.isDirectory());//判断当前的file对象是否为文件夹或者目录
System.out.println(file4.lastModified());//获取文件的最后修改时间,返回的是一个毫秒数
System.out.println(file4.length());//返回文件的长度,单位是字节数
File file6 = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\tt2.txt");
System.out.println(file6.exists());
if (!file6.exists()){
file6.createNewFile();创建新的文件
}
file6.delete();//删除文件
File file7 = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\b");
file7.mkdir();//创建单层目录
File file8 = new File("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\b\\c\\d");
file8.mkdirs();//创建多层目录
File file9 = new File("D:\\JavaProjects\\b站草稿\\IO控制流");
String[] str = file9.list();//返回的是当前文件夹的子集的名称,包括目录和文件
for (String s : str){
System.out.println(s);
}
File[] fs = file9.listFiles();//返回的是当前文件夹的子集的file对象,包括目录和文件
for(File ff : fs){
System.out.println(ff);
}
}
}
递归遍历文件
import java.io.File;
public class test1 {
public static void main(String[] args) {
//遍历d盘下的test文件,把test文件夹下所有的目录与文件全部遍历出来,不论层级有多深,要全部遍历出来
File f = new File("D:\\JavaProjects\\b站草稿\\IO控制流");
new test1().test(f);//直接new本类test1中的test方法
}
public void test(File file){
if (file.isFile()){
System.out.println(file.getAbsoluteFile() + "是文件");
}else {
System.out.println(file.getAbsoluteFile() + "是文件夹");
File[] ff = file.listFiles();//返回当前目录的子文件和目录
if (ff != null && ff.length >1 ){
for (File fs : ff){
test(fs);
}
}
}
}
}
//
D:\JavaProjects\b站草稿\IO控制流是文件夹
D:\JavaProjects\b站草稿\IO控制流\a是文件夹
D:\JavaProjects\b站草稿\IO控制流\a\b是文件夹
D:\JavaProjects\b站草稿\IO控制流\a\tt1.txt是文件
D:\JavaProjects\b站草稿\IO控制流\IO控制流.iml是文件
D:\JavaProjects\b站草稿\IO控制流\src是文件夹
D:\JavaProjects\b站草稿\IO控制流\src\test.java是文件
D:\JavaProjects\b站草稿\IO控制流\src\test1.java是文件
二、流的分类
按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
按数据流的流向不同分为:输入流,输出流
按流的角色的不同分为:节点流,处理流
Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。
文件字节输入流
/**
* 文件字节输入流FileInputStream
* 。
* 在读取文件时,必须保证该文件已存在,否则出异常
*/
public static void testFileInputStream()throws IOException{
FileInputStream in = new FileInputStream("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\tt1.txt");
byte[] b = new byte[10];//设置一个byte数组接收读取的文件的内容
//fileInputStream.read(b);//XX.read方法有一个返回值,返回值是读取的数据的长度,如果读取到最后一个数据,还会向后读一个,这个时候返回值就是-1
//也就意味着当in.read的返回值是-1的时候整个文件就读取完毕了
//System.out.println(new String(b));
int len = 0;//设置一个读取数据的长度
while ((len=in.read(b)) != -1){
System.out.println(new String(b,0,len));
//new String(b,0,len),参数1是缓冲数据的数组,参数2是从数组的那个位置开始转化字符串,参数3是总共转化几个字节
}
in.close();//注意。流在使用完毕之后一段要关闭
}
文件字节输出流
/**
* 文件字节输出流FileOutputStream
* 在写入一个文件时,如果目录下有同名文件将被覆盖
*/
public static void testFileOutputStream() throws IOException {
FileOutputStream out = new FileOutputStream("D:\\JavaProjects\\b站草稿\\IO控制流\\a\\tt1.txt");
String str = "qwertyu";
out.write(str.getBytes());//把数据写到内存
out.flush();//把内存中的数据刷写到硬盘
out.close();//关闭流
}
字节流文件复制
/**
* 复制文件到指定位置
* @param inPath 源文件路径
* @param outPanth 复制到的文件夹位置
*/
public static void copyFile(String inpath , String outpath) throws Exception {
FileInputStream in = new FileInputStream(inpath);
FileOutputStream out = new FileOutputStream(outpath);
byte[] b = new byte[100];
int len = 0;
while ((len = in.read(b)) != -1){
out.write(b,0,len);
//out.write(b);效果一样
}
//直接写
// in.read(b);
// out.write(b);
//也可以
out.flush();
out.close();
in.close();
}
文件字符输出输出流和字符流完成拷贝文件
读取文件操作步骤:
与字节输入输出的区别1.名字不同 这里是FileReader 2.创建的存放数据的数组不同这里是char
1.建立一个流对象,将已存在的一个文件加载进流。
FileReader fr = new FileReader(“Test.txt”);
2.创建一个临时存放数据的数组。
char[] ch = new char[1024];
3.调用流对象的读取方法将流中的数据读入到数组中。
fr.read(ch);
写入文件步骤:
与字节仅仅名字不同这里是FileWriter
1.创建流对象,建立数据存放文件
FileWriter fw = new FileWriter(“Test.txt”);
2.调用流对象的写入方法,将数据写入流
fw.write(“text”);
2.1 输出流关闭之前需要清空缓存
fw.flush();
3.关闭流资源,并将流中的数据清空到文件中。
fw.close();
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
Test2.testFileReader("D:/test/abc/tt1.txt");
//在写入一个文件时,如果目录下有同名文件将被覆盖。
// Test2.testFileWriter("!!!!!!!", "D:/test/abc/tt5.txt");
// Test2.copyFile("D:/test/abc/tt5.txt", "D:/test/abc/cc/tt5.txt");
}
/**
* 文件字符输入流FileReader
* 在读取文件时,必须保证该文件已存在,否则出异常
* @param inPath
*/
public static void testFileReader(String inPath){
try {
FileReader fr = new FileReader(inPath);//创建文件字符输入流的对象
char[] c = new char[10];//创建临时存数据的字符数组
int len = 0;//定义一个输入流的读取长度
while((len = fr.read(c)) != -1){
System.out.println(new String(c, 0, len));
}
fr.close();//关闭流
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件字符输出流FileWriter
* 在写入一个文件时,如果目录下有同名文件将被覆盖
* @param text 输出的内容
* @param outPath 输出的文件
*/
public static void testFileWriter(String text,String outPath){
try {
FileWriter fw = new FileWriter(outPath);
fw.write(text);//写到内存中
fw.flush();//把内存的数据刷到硬盘
fw.close();//关闭流
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 字符流完成拷贝文件,字符流只适合操作内容是字符文件
* @param inPaht
* @param outPath
*/
public static void copyFile(String inPaht, String outPath){
try {
FileReader fr = new FileReader(inPaht);
FileWriter fw = new FileWriter(outPath);
char[] c = new char[100];
int len = 0;
while((len = fr.read(c)) != -1){//读取数据
fw.write(c,0,len);//写数据到内存
}
fw.flush();
fw.close();
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
缓冲流
为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组
根据数据操作单位可以把缓冲流分为:
BufferedInputStream 和 BufferedOutputStream
BufferedReader 和 BufferedWriter
对标
FileInputStream | FileOutputStream |
---|---|
FileReader | FileWriter |
这些都是计算机与硬盘之间的I/O操作,基于硬盘的读写是比较慢的,而速度受限于硬盘的速度,为了提高速度,且绕开硬盘的限制,java提供了一种缓冲流来实现
缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法
对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出
**注意注意!!**缓冲流是把数据缓冲到内存中
缓冲字节流
import java.io.*;
public class test3 {
public static void main(String[] args) throws Exception {
test3.tsetBufferedInputStream("D:\\JavaProjects\\b站草稿\\IO控制流\\src\\t1.txt");
test3.teatBufferedOutputStream("hellow world" , "D:\\JavaProjects\\b站草稿\\IO控制流\\src\\t2.txt");
test3.copyFile("D:\\JavaProjects\\b站草稿\\IO控制流\\src\\t1.txt","D:\\JavaProjects\\b站草稿\\IO控制流\\src\\t2.txt");
}
/**
* 缓冲字节输入流
* BufferedInputStream
* @throws Exception
*/
public static void tsetBufferedInputStream(String path) throws Exception {
BufferedInputStream br = new BufferedInputStream(new FileInputStream(path));
//使用BufferedInputStream就需要先newFileInputStream对象
byte[] b = new byte[1024];
int len = 0;
while ((len = br.read(b)) != -1){
System.out.println(new String(b,0,len));
}
br.close();
}
/**
* 缓冲字节输出流
* BufferedOutputStream
*/
public static void teatBufferedOutputStream(String out ,String path) throws Exception {
BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(path));
bo.write(out.getBytes());
bo.flush();
bo.close();
}
/**
* 缓冲流实现文件的复制
*/
public static void copyFile(String inPath,String outPath) throws Exception {
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(inPath));
BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(outPath));
byte[] b = new byte[1024];
int len = 0;
while ((len = bi.read(b)) != -1){
bo.write(b ,0 ,len);
}
bo.flush();
bo.close();
bi.close();
}
}
缓冲字符流
import java.io.*;
public class test4 {
public static void main(String[] args) throws Exception {
test4.testBufferedReader("D:\\JavaProjects\\b站草稿\\IO控制流\\src\\t1.txt");
test4.testBufferedWriter("hello world" , "D:\\JavaProjects\\b站草稿\\IO控制流\\src\\t3.txt");
test4.CopyFile("D:\\JavaProjects\\b站草稿\\IO控制流\\src\\t1.txt" , "D:\\JavaProjects\\b站草稿\\IO控制流\\src\\t4.txt");
}
/**
* 缓冲字符输入流
* BufferedReader
*/
public static void testBufferedReader(String path) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(path));
char[] c = new char[1024];
br.read(c);
System.out.println(c);
br.close();
}
/**
* 缓冲字符输出流
* BufferedWriter
*/
public static void testBufferedWriter(String out , String path) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(path));
bw.write(out);
bw.flush();
bw.close();
}
/**
* 缓冲字符流复制文件
*/
public static void CopyFile(String InPath , String OutPath) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(InPath));
BufferedWriter bw = new BufferedWriter(new FileWriter(OutPath));
char[] c = new char[1024];
// int len = 0;
//
// while ((len = br.read(c)) != -1){
// bw.write(c);
// }
br.read(c);
bw.write(c);
bw.flush();
bw.close();
br.close();
}
}