IO-文件&字节流
目录
一、File
1.1.定义&构造器
File 可以表示文件,也可以表示文件夹,联的路径可以存在,也可以不存在 创建对象 File(String pathname) 根据指定路径创建File对象 File(String parent,String child) 跟进指定参数拼接的路径创建File对象 File(File parent,String child) 跟进指定参数拼接的路径创建File对象 文件路径分隔符写法 1、/可以单独写 2、|需要转义 3、File.separator根据系统自动匹配分隔符 路径的分类 绝对路径:从盘符开始的路径 相对路径:不带盘符,默认直接到当前工程下的目录寻找文件。
代码如下(示例):
public static void main(String[] args) throws IOException {
//1. 单参数创建文件
File file = new File("D:\\a.txt");
File file1 = new File("D:\\b.txt");
System.out.println(file);
System.out.println(file1);
//2. 多参数创建文件
File file2 = new File("D:\\", "a.txt");
File file3 = new File("D:\\", "b.txt");
System.out.println(file2);
System.out.println(file3);
//3. 文件分隔符
File file6 = new File("D:/a.txt");
//4. 决定路径 和 相对路径
File file4 = new File("D:\\a.txt");
File file5 = new File("b.txt");
1.2.常用方法
常用方法:判断文件类型、获取文件信息 boolean exists() 判断文件路径是否存在 boolean isFile() 判断是否是文件(不存在的都是false) boolean isDirectory() 判断是否是文件夹(不存在的都是false) String getName() 获取文件/文件名,包含后缀 long length() 获取文件大小,返回字节个数 long lastModified() 获取最后修改时间 string getPath() 获取创建对象时的路径 String getAbsolutePath() 获取对象绝对路名
代码如下(示例):
public static void main(String[] args) {
File f1 = new File("day08-code/test1");//文件夹-已存在
File f2 = new File("day08-code/test1/1.txt");//文件-已存在
File f3 = new File("day08-code/test2");//文件夹-不存在
File f4 = new File("day08-code/test2/2.txt");//文件-不存在
//boolean exists() 判断文件路径是否存在
boolean exists = f1.exists();
System.out.println(exists);
System.out.println(f2.exists());
boolean exists1 = f3.exists();
System.out.println(exists1);
System.out.println(f4.exists());
System.out.println("======================");
//boolean isFile() 判断是否是文件
System.out.println(f1.isFile());
System.out.println(f2.isFile());
System.out.println(f3.isFile());
System.out.println(f4.isFile());
System.out.println("======================");
//boolean isDirectory() 判断是否是文件夹
System.out.println(f1.isDirectory());
System.out.println(f2.isDirectory());
System.out.println(f3.isDirectory());
System.out.println(f4.isDirectory());
System.out.println("======================");
//String getName() 获取文件/文件名,包含后缀
System.out.println(f1.getName());
System.out.println(f2.getName());
System.out.println(f3.getName());
System.out.println(f4.getName());
System.out.println("======================");
//long length() 获取文件大小,返回字节个数
System.out.println(f1.length());
System.out.println(f2.length());
System.out.println(f3.length());
System.out.println(f4.length());
System.out.println("======================");
//long lastModified() 获取最后修改时间
System.out.println(f1.lastModified());
System.out.println(f2.lastModified());
System.out.println(f3.lastModified());
System.out.println(f4.lastModified());
System.out.println("======================");
//string getPath() 获取创建对象时的路径
System.out.println(f1.getPath());
System.out.println(f2.getPath());
System.out.println(f3.getPath());
System.out.println(f4.getPath());
System.out.println("======================");
//String getAbsolutePath() 获取对象绝对路径
System.out.println(f1.getAbsolutePath());
System.out.println(f2.getAbsolutePath());
System.out.println(f3.getAbsolutePath());
System.out.println(f4.getAbsolutePath());
}
1.3.创建和删除方法
File创建和删除方法 boolean mkdir() 创建单级文件夹,创建失败返回false boolean mkdirs() 创建多级文件夹 (常用) boolean createNewFile() 创建文件,文件存在返回false boolean delete() 删除文件或空文件夹,删除失败返回false (注意: 删除方法不走回收站,填用)
代码如下(示例):
public static void main(String[] args) throws IOException {
File f1 = new File("day08-code/test2");
File f2 = new File("day08-code/test2/test3");
File f3 = new File("day08-code/test2/2.txt");
//boolean mkdir() 创建单级文件夹,创建失败返回false
boolean mkdir = f1.mkdir();
//boolean mkdirs() 创建多级文件夹 (常用)
boolean mkdirs = f2.mkdirs();
//boolean createNewFile() 创建文件,文件存在返回false
boolean newFile = f3.createNewFile();
//boolean delete() 删除文件或空文件夹,删除失败返回false (注意: 删除方法不走回收站,填用)
boolean delete = f3.delete();
boolean delete1 = f2.delete();
}
1.4.遍历文件夹
查看目录中的内容 String[] list() 获取当前目录下所有的"一级文件名称"到一个字符串数组中去返回。 File[] listFiles() 获取当前目录下所有的"一级文件对象"到一个文件对象数组中去返回,包含隐藏文件(重点) 注意事项 当主调是文件,或者路径不存在时,返回null 当主调是空文件夹时,返回一个长度为0的数组 当主调是一个非空文件夹,但是没有权限访问该文件夹时,返回null
代码如下(示例):
public static void main(String[] args) {
//String[] list() 返回文件名数组
File file = new File("");
//String[] strs = file.list();
//System.out.println(Arrays.toString(strs));
//File[] listFiles() 返回文件数组
File[] files = file.listFiles();
for (File file1 : files) {
System.out.println(file1);
}
}
1.5.案例
需求: 从D:盘中,搜索“QQ.exe” 这个文件,找到后直接输出其位置。 分析: 先找出D:盘下的所有一级文件对象 遍历全部一级文件对象,判断是否是文件 如果是文件,判断是否是自己想要的 如果是文件夹,需要继续进入到该文件夹,重复上述过程
代码如下(示例):
public static void main(String[] args) {
File file = new File("E:\\");
sarech(file,"pycharm64.exe");
}
public static void sarech(File file, String name){
File[] files = file.listFiles();
if(files == null || files.length == 0){
return;
}
for (File file1 : files) {
if(file1.isFile()){
if(file1.getName().equals(name)){
System.out.println(file1.getAbsolutePath());
}
}else {
sarech(file1,name);
}
}
}
二、字符集
2.1.定义
字符集 ASCII字符集:只有英文、数字、符号等,占1个字节。 GBK字符集:汉字占2个字节,英文、数字占1个字节。 UTF-8字符集:汉字占3个字节,英文、数字占1个字节。 注意 技术人员在开发时都应该使用UTF-8编码!
2.2.字符集的编码、解码操作
编码: 字符-->字节 byte[] getBytes() 使用平台的默认字符集将该String编码为一系列字节,将结果存储到新的字节数组中 byte[] getBytes(String charsetName) 使用指定的字符集将该String编码为一系列字节,将结果存储到新的字节数组中 解码: 字节-->字符 String(byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组来构造新的String String(byte[] bytes, String charsetName) 通过指定的字符集解码指定的字节数组来构造新的String
代码如下(示例):
public static void main(String[] args) throws UnsupportedEncodingException {
//编码: 字符-->字节
String s = "我as4你";
byte[] bytes = s.getBytes();
System.out.println(Arrays.toString(bytes));
byte[] bytes1 = s.getBytes("GBK");
System.out.println(Arrays.toString(bytes1));
String string = new String(bytes);
System.out.println(string);
String string1 = new String(bytes1,"GBK");
System.out.println(string1);
}
三、IO流概述
3.1.作业&语法&方法
文件字节输入流 作用: 读取文件中的内容到程序中,结果是字节 语法: FileInputStream(File file) 创建字节输入流管道与源文件接通 FileInputStream(String pathname) 创建字节输入流管道与源文件接通 方法(1次读1个字节) int read() 每次读取一个字节返回, 如果发现没有数据可读会返回-1
代码如下(示例):
public static void main(String[] args) throws IOException {
//1. 创建文件字节输入流(c_demo1.txt)
FileInputStream fileInputStream = new FileInputStream("day08-code\\c_demo1.txt");
//2. 读取文件中的内容
int len;
while ((len = fileInputStream.read()) != -1){
System.out.println((char) len);
}
//3. 如果流操作完毕, 应该主动关闭流
fileInputStream.close();
}
3.2.字节输入流1
文件字节输入流(1次读多个字节) int read(byte[] buffer) 每次用一个字节数组去读取数据, 返回字节数组读取了多少个字节,如果发现没有数据可读会返回-1.
代码如下(示例):
public static void main(String[] args) throws IOException {
//1. 创建文件字节输入流(c_demo2.txt)
FileInputStream fis = new FileInputStream("day08-code\\c_demo2.txt");
//2. 读取文件中的内容
byte[] bytes = new byte[3];
int read;
while ((read = fis.read(bytes)) != -1){
System.out.println(new String(bytes, 0, read));
}
//3. 如果流操作完毕, 应该主动关闭流
fis.close();
}
3.3.字节输入流2
文件字节输入流(1次读所有字节) 方式1:自己定义一个字节数组与被读取的文件大小一样大,然后使用该字节数组,一次读完文件的全部字节。 public int read(byte[] buffer) 每次用一个字节数组去读取,返回字节数组读取了多少个字节,如果发现没有数据可读会返回-1. 方式2: Java官方为InputStream提供了如下方法,可以直接把文件的全部字节读取到一个字节数组中返回。 public byte[] readAllBytes() 直接将当前字节输入流对应的文件对象的字节数据装到一个字节数组返回
代码如下(示例):
public static void main(String[] args) throws IOException {
//1. 创建文件字节输入流(c_demo3.txt)
File file = new File("day08-code/c_demo3.txt");
FileInputStream fis = new FileInputStream("day08-code/c_demo3.txt");
//2. 读取文件中的内容
//方式1: 定义一个跟文件同样大小的数组,一次接收所有
// byte[] bytes = new byte[(int) file.length()];
// int read = fis.read(bytes);
// System.out.println(new String(bytes));
//方式2: 直接使用readAllBytes将文件中所有内容读取到数组
byte[] bytes1 = fis.readAllBytes();
System.out.println(new String(bytes1));
//3. 如果流操作完毕, 应该主动关闭流
fis.close();
}
3.4.字节输出流
文件字节输出流 作用: 向文件中写入字节内容 语法: FileOutputStream(String filepath) 创建字节输出流管道与源文件路径接通 FileOutputStream(String filepath, boolean append) 创建字节输出流管道与源文件路径接通,可追加数据 方法 void write(int a) 写一个字节出去 void write(byte[] buffer) 写一个字节数组出去 void write(byte[] buffer, int pos, int len) 写一个字节数组的一部分出去 void close() 关闭流 注意: 如果要写出换行, 需要写"\r\n"的字节数组表示形式
代码如下(示例):
public static void main(String[] args) throws IOException {
//创建文件字节输出流(c_demo4.txt)
FileOutputStream fos = new FileOutputStream("day08-code/c_demo4.txt",true);
byte[] buffer = "hello".getBytes();
//fos.write(97);
//fos.write(buffer);
byte[] buffer2 = "\r\n".getBytes();
fos.write(buffer2);
fos.write(buffer,0,3);
//关闭流
fos.close();
}
3.5.文件复制案例
文件复制 源文件-->输入流-->程序-->输出流-->目标文件 步骤: 1. 创建文件的输入流,对应到源文件; 创建文件输出流,对应到目标文件 2. 使用输入流读取数据,使用输出流写出数据(边读边写) 3. 释放资源(关闭流)
代码如下(示例):
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("");
FileOutputStream fos = new FileOutputStream("");
byte[] bytes = new byte[1024*1024];
int len;
while ((len = fis.read(bytes)) != -1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
总结
以上就是今天学习的内容。