系列文章目录
前言
烦恼来自想的太多读书太少
提示:以下是本篇文章正文内容,下面案例可供参考
一、File类
1.1File类概述和构造方法[应用]
1)File类介绍
。它是文件和目录路径名的抽象表示
。文件和目录是可以通过File封装成对象的
。对于File而言 ,其封装的并不是一个真正存在的文件 ,仅仅是一个路径名而已
。它可以是存在的,也可以是不存在的。将来是要通过具体的操作把这个路径的内容转换为具体存在的
2)File类的构造方法
package Demo01;
import java.io.File;
import java.io.IOException;
/*
* file类
*
* 创建一个文件/文件夹
* 删除一个文件、文件夹
* 获取文件、文件夹
* 判断文件或者文件夹
* 对文件进行遍历
* 获取文件大小
*
* File 是一个与操作系统无关的类
*
* 记住三个单词
* file: 文件
* directory:目录、文件夹
* path:路径
*/
public class Demo01File {
public static void main(String[] args) throws IOException {
//路径分割符
String pathSeparator=File.pathSeparator;
System.out.println(pathSeparator);//win 是分号;linux :冒号
String separator=File.separator;
System.out.println(separator);//文件名称分割符 \ linux:/root/home
// 绝对路径 D:\学习
//相对路径../../
//show01();
//show02("D:\\java学习\\测试");
show03();
//常用方法演示
//show04();
//show05();
//show06();
//show07();
//show08();
//show09();
show10();
show11();
//show12();
}
private static void show12() {
//如果路径不存在直接返回false
File f1=new File("E:\\eclipse");
boolean b1=f1.delete();
System.out.println(b1);
//直接从硬盘删除
File f2=new File("rxp\\1\\2\\3\\4\\hello.java");//相对路径,但是会补齐
boolean b2=f2.delete();
System.out.println(b2);
}
private static void show11() {
File f1=new File("E:\\eclipse");
boolean b1=f1.mkdir();
System.out.println(b1);
File f2=new File("E:\\eclipse\\rxp\\1\\2\\3\\4\\hello.java");
boolean b2=f2.mkdirs();//递归创建文件
System.out.println(b2);
}
private static void show10() throws IOException {
File f1=new File("E:\\eclipse\\hello.java");
boolean b1=f1.createNewFile();
System.out.println(b1);
//第二次执行的时候返回true 因为文件 存在
File f2=new File("E:\\eclipse\\新建文件夹");
//不要看文件的名字, 必须看类型
boolean b2=f2.createNewFile();
System.out.println(b2);
}
private static void show09() {
File f1=new File("E:\\eclipse");
if(f1.exists()) {//返回true,进入if语句
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
}
System.out.println("===============");
File f2=new File("E:\\eclipse");
if(f2.exists()) {//不存在返回false所以不会进入if语句
System.out.println(f2.isDirectory());
System.out.println(f2.isFile());
}
System.out.println("===============");
File f3=new File("E:\\eclipse");
if(f3.exists()) {
System.out.println(f3.isDirectory());
System.out.println(f3.isFile());
}
}
private static void show08() {
File f1=new File("E:\\eclipse");
System.out.println(f1.exists());//ture
File f2=new File("E:\\eclipse\\word");
System.out.println(f2.exists());//false
}
//注意 文件夹没有大小的概念,不能获取文件夹的大小
private static void show07() {
File f1=new File("E:\\eclipse");
System.out.println(f1.length());//文件夹的大小是0
File f2=new File("E:\\eclipse\\word");
System.out.println(f2.length());//不存在的文件夹,打印大小为0
File f3=new File("E:\\eclipse\\123.txt");
System.out.println(f3.length());
}
private static void show06() {
File f1=new File("E:\\eclipse\\hello");
File f2=new File("E:\\eclipse");
//获取的是构造方法传递路径结尾部分
System.out.println(f1.getName());
System.out.println(f2.getName());
}
private static void show05() {
File f1=new File("E:\\eclipse");
File f2=new File("a.txt");
String path1=f1.getPath();
System.out.println(path1);
System.out.println(f2.getPath());
System.out.println(f1);
System.out.println(f1.toString());
}
private static void show04() {
File f1=new File("E:\\eclipse");
String absolutepath1=f1.getAbsolutePath();
System.out.println(absolutepath1);
File f2=new File("hell.java");
String absolutepath2=f2.getAbsolutePath();
System.out.println(absolutepath2);
}
private static void show03() {
File parent=new File("E:\\eclipse");
File f1=new File(parent,"hello.java");
System.out.println(f1);
}
private static void show02(String parent,String child) {
File f1= new File("E:\\eclipse");
System.out.println(f1);
//仅仅是创建了File这样的对象,并不会真去检验路径的正确与否
File f2= new File("E:\\eclipse");
System.out.println(f2);
File f3= new File("b.txt");
System.out.println(f3);
}
private static void show01() {
File f1= new File("E:\\eclipse");
System.out.println(f1);
//仅仅是创建了File这样的对象,并不会真去检验路径的正确与否
File f2= new File("E:\\eclipse");
System.out.println(f2);
File f3= new File("b.txt");
System.out.println(f3);
}
}
二、IO流概述和分类
1.I0流介绍
。I0 :输入/输出(Input/Output)
0流:是一种抽象概念,是对数据传输的总称。也就是说数据在设备间的传输称为流,流的本质是数据传
输
。I0流就是用来处理设备间数据传输问题的。常见的应用:文件复制;文件上传;文件下载
●10流的分类
。按照数据的流向
■输入流:读数据
■输出流:写数据
。按照数据类型来分
■字节流
■字节输入流
1字节输出流
■字符流
■字符输入流
■字符输出流
●. I0流的使用场景
。如果操作的是纯文本文件,优先使用字符流
。如果操作的是图片、视频、音频等二进制文件。 优先使用字节流
。如果不确定文件类型,优先使用字节流。字节流是万能的流
2.字节流写数据
●字节流抽象基类
。InputStream :这个抽象类是表示字节输入流的所有类的超类
。OutputStream :这个抽象类是表示字节输出流的所有类的超类
。子类名特点:子类名称都是以其父类名作为子类名的后缀
●字节输出流
0 FileOutputStream(String name) :创建文件输出流以指定的名称写入文件
●使用字节输出流写数据的步骤
。创建字节输出流对象(调用系统功能创建了文件,创建字节输出流对象,让字节输出流对象指向文件)
。调用字节输出流对象的写数据方法
。释放资源(关闭此文件输出流并释放与此流相关联的任何系统资源)
代码如下(示例):
package Demo02;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 构造方法的作用:
* 1、创建了一个FileOutputStream对象
* 2、根据构造方法中传递的文件/文件路径,创建一个空的文件
* 会把FileOutStream对象指向创建的文件
*
* 写入过程 -->jvm(java虚拟机) -->os(操作系统上) -->会调用相应的驱动 -- >把数据写入文件
*
*
*
*/
public class Demo02OutoutStream {
public static void main(String[] args) throws IOException {
//创建一个FileOutputStream 对象,构造方法中传入数据目的地
FileOutputStream fos=new FileOutputStream("a.txt");
//调用write 方法,将数据写入到文件中
//fos.write(97);
//fos.write(98);
//fos.write(99);
//fos.write(49);
//fos.write(48);
//fos.write(48);
//byte[] bytes= {65,66,67,68};
//byte[] bytes= {-65,66,-67,68};//会被当成中文来解释
byte[] bytes= {65,66,67,68,69,70};
fos.write(bytes ,2,3);
//释放资源
fos.close();
}
}