IO流
| 字节流 | 字符流 | |
|---|---|---|
| 写出 | OutputStream | Writer |
| 读入 | InputStream | Reader |
File文件类
构造方法
new File(String path)
new File(File file)
new File(String parentPath,String childPath)
new File(File parentFile,String childPath)
注意:参数位路径信息 可以是错误的 父级路径 保证正确
常用方法
| 方法名 | 返回类型 | 功能 |
|---|---|---|
| mkdir() | boolean | 创建一个文件夹(保证父级路径正确 否则返回false) |
| mkdirs() | boolean | 创建一串文件夹 从不正确的地方开始创建文件夹 |
| createNewFile() | boolean | 创建一个文件(1.抛一个编译期异常2.保证父级路径正确3.注意重名忽略大小写) |
| delete() | boolean | 删除最后一个文件\文件夹 |
| isFile() | boolean | 判断路径是否指向的是一个文件对象 |
| isDirectory() | boolean | 判断路径是否指向的是一个文件夹 |
| exists() | boolean | 判断路径是否是真实路径 |
| long | length() | 获取某个文件的字节数目 |
| renameTo(File newFile) | void | 将文件名称修改 |
| toString() | String | 返回完整路径 |
| getPath() | String | 返回完整路径 |
| getParent() | String | 返回父级路径 File getParentFile() |
| getName() | String | 返回文件名称 |
| list() | String[] | 返回该路径下所有文件\文件夹的名字构成的数组 |
| listFiles() | File[] | 返回该路径下所有文件\文件夹的对象构成的数组 |
修改文件名
File f0 = new File("路径");
File f1 = new File(f0,"users.txt");
//先创建一个新的路径 最后一个位置放修改后的新名称
//通过旧路径对象.renameTo(新路径对象)
File f2 = new File(f0,"Dog.txt");
f1.renameTo(f2);
字节流读入写出流
File fin = new File("D:\\路径\\a\\2026_03_16_编译运行.mp4");
File fto = new File("路径\\a\\b",fin.getName());
//字节读入流
FileInputStream fis = new FileInputStream(fin);
//字节写出流
FileOutputStream fos = new FileOutputStream(fto);
long l1 = System.currentTimeMillis();
byte[] bs = new byte[1024];//缓冲
int l = -1;//记录每次实际读入的字节数木
while((l = fis.read(bs))!=-1){
fos.write(bs,0,l);
fos.flush();
}
字符流读入
//1.数据来源
File file = new File("D:\\路径\\name.txt");
//2.创建字符读入流对象
FileReader fr = new FileReader(file);
//3.调用读入方法
// int num = fr.read();
// System.out.println((char)num);
//缓冲读入
int count = 0 ;
int num = -1;
while((num = fr.read())!=-1){
System.out.print((char)num);
count++;
}
字符流读出
//1.先准备写出目的地
File file = new File("D:\\路径\\name.txt");
//2.创建字符写出流对象
FileWriter fw = new FileWriter(file);
//3.调用写出方法
fw.write("中国你好");
fw.write(10);//换行
字节流边写入边读出
public static void main(String[] args) throws IOException {
//使用字节缓冲流来复制同样的视频
//1.数据来源和去处
File f1 = new File("D:\\files\\一阶段\\class131\\test.txt\\a\\2026_03_16_编译运行.mp4");
File f2 = new File("D:\\files\\一阶段\\class131\\test.txt\\a\\b",f1.getName());
//2.创建普通字节读入 写出流
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
//3.根据普通流创建缓冲流
//字节读入缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
//字节写出缓冲流
BufferedOutputStream bos = new BufferedOutputStream(fos);
long l1 = System.currentTimeMillis();
//4.调用方法复制
//自备缓冲区
byte[] bs = new byte[1024];
int l = -1;
while((l = bis.read(bs))!=-1){
bos.write(bs,0,l);
bos.flush();
}
long l2 = System.currentTimeMillis();
bos.close();
bis.close();
System.out.println(l2-l1);
}
字符流边读入边写出
public static void main(String[] args) throws IOException {
//使用字符缓冲流 复制文本
File f1 = new File("D:\\files\\一阶段\\class131\\4.14作业.txt");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
// BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\files\\一阶段\\class131\\test.txt\\a",f1.getName())));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\files\\一阶段\\class131\\test.txt\\a\\"+f1.getName()));
// char[] cs = new char[10];
// int l = -1;
// while((l = br.read(cs))!=-1){
// bw.write(cs,0,l);
// bw.flush();
// }
String str = null;
while((str = br.readLine())!=null){
bw.write(str);
//加一个换行
// bw.write(10);
bw.newLine();//开启新行
}
bw.close();
br.close();
}
评论