IO流
File类
java.io.File
文件类,提供了用于操作文件、创建文件、获取文件信息等各种文件相关的方法。
构造方法
// 从父抽象路径名和子路径名字符串创建新的 File实例
File(File parent, String child)
// 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例
File(String pathname) (常用)
pathname 绝对路径
File file1 = new File("C:\\User\\XXX\\Desktop\\xxx.txt")
方法
exists()
作用:判断文件或目录是否存在。
参数:无
返回值:布尔值
示例:
// 创建File对象
File file1 = new File("D:\\fileTest.txt");
// 文件是否存在
boolean exists = file1.exists();
System.out.println("文件是否存在 " + exists); // true
isFile()
作用:判断是否是文件形式。
参数:无
返回值:布尔值
示例:
// 创建File对象
File file1 = new File("D:\\fileTest.txt");
// 文件是否是文件形式
boolean file = file1.isFile();
System.out.println("是否是文件格式 " + file); // true
isDirectory()
作用:判断是否是目录。
参数:无
返回值:布尔值
示例:
// 是否是目录格式
boolean directory = file1.isDirectory();
System.out.println("是否是目录格式 " + directory); // false
getPath()
作用:返回此对象表示的文件的相对路径名。
参数:无
返回值:String类型
示例:
// 创建File对象
File file1 = new File("D:\\fileTest.txt");
// 返回文件对象的相对目录
String path = file1.getPath();
System.out.println("文件对象的相对路径 " + path); // D:\fileTest.txt
getAbsolutePath()
作用:返回此对象表示的文件的绝对路径名。
参数:无
返回值:String类型
示例:
// 创建File对象
File file1 = new File("D:\\fileTest.txt");
// 返回文件对象的绝对目录
String path = file1.getAbsolutePath();
System.out.println("文件对象的绝对路径 " + path); // D:\fileTest.txt
getName()
作用:返回此对象表示的文件或目录的名称。
参数:无
返回值:String类型
示例:
// 创建File对象
File file1 = new File("D:\\fileTest.txt");
// 获取对象的文件名
String name = file1.getName();
System.out.println("文件名 " + name); // fileTest.txt
delete()
作用:删除此对象指定的文件或目录。
参数:
返回值:
示例:
createNewFile()
作用:创建名称的空文件,不创建文件夹。
参数:无
返回值:布尔值
示例:
File file2 = new File("D:\\fileTest2.txt");
// 创建文件
boolean newFile = file2.createNewFile();
System.out.println("文件是否成功创建 " + newFile); // 文件是否成功创建 true
length()
作用:返回文件的长度,单位为字节, 如果文件不存在,则返回 0L。
参数:无
返回值:long类型
示例:
// 创建文件
File file2 = new File("D:\\fileTest2.txt");
boolean newFile = file2.createNewFile();
long length = file2.length ();
System.out.println("文件的长度为 " + length); // 18
System.out.println("文件是否成功创建 " + newFile); // 文件是否成功创建 true
字节流
通过流来读写数据。字节流每次读取8位(一个字节),字符流每次读取16位(一个char的长度)。
一个汉字占用的字节:
-
UTF-16编码,每个char占用2个字节;
-
UTF-8编码,每个char占用3个字节;
-
GBK/GB2312编码,每个char占用2个字节;
InputStream类
java.io.InputStream包名,这个抽象类是表示输入字节流的所有类的超类,是抽象类。
构造方法
抽象类
方法
read()
作用:读取文件内容。抽象方法
参数:
参数为空时表示每次读取一个字节大小的内容;
参数为byte类型的数组时,表示每次读取数组长度个字节大小的内容;
返回值:
当参数为空时,返回值为读取的文件中的内容(单个字符)的ASCII码,每调用一次read方法,就会往后读取下一个字符(有一个指针,每一次读取指针就会指向下一个字符);
当参数为byte类型的数组时,返回值为每次读取到数据内容的个数,读取到的内容保存在byte类型的数组参数中。
示例:
close()
作用:关闭输入流资源(此方法属于静态方法,使用类名访问),需要先做非空判断。抽象方法
参数:无
返回值:无
示例:
available()
作用:关闭输入流资源(此方法属于静态方法,使用 类名访问),需要先做非空判断。抽象方法
参数:无
返回值:无
示例:
FileInputStream类
构造方法
// 创建文件读取流对象,参数是一个文件对象
FileInputStream(File file)
// 创建文件读取流对象,参数是文件的路径
FileInputStream(String name)
方法
read()
作用:读取文件内容。抽象方法
参数:
参数为空时表示每次读取一个字节大小的内容;
参数为byte类型的数组时,表示 每次读取数组长度个字节大小的内容;
返回值:
当参数为空时,返回值为读取的文件中的内容(单个字符)的ASCII码,每调用一次read方法,就会往后读取下一个字符(有一个指针,每一次读取指针就会指向下一个字符);
当参数为byte类型的数组时,返回值为每次读取到数据内容的个数,读取到的内容保存在byte类型的数组参数中。
示例:
// 每次读取一个 字节长度
// 创建文件对象
File file1 = new File("D:\\Text file.txt");
// 创建文件读取流对象
FileInputStream inputStream = new FileInputStream(file1);
// 读取文件 每次读取一个字节大小内容
int read = inputStream.read(); // 返回一个字节大小的内容的ASCII表示
System.out.println(read);// 73
System.out.println((char) read);// 强制类型转换
// 每次读取一个字节数组大小内容,返回值为读取到的内容的个数,读取的内容保存在byte数组中
// 创建文件对象
File file1 = new File("D:\\Text file.txt");
// 创建文件读取流对象
FileInputStream inputStream = new FileInputStream(file1);
// 读取文件 每次读取一个字节数组大小,返回值为读取到内容的长度
byte[] data = new byte[5];
// 设置初始值
int readedLength = -1;
// 遍历 读取到的数据
while((readedLength = inputStream.read(data)) != -1){ // 当读取到文件内容的末尾时,返回值为-1
// 使用String类中的构造方法将 字节数组转为 String类型
// 第一个参数 byte数组,第二个参数 从0开始转换,第三个参数 转换的数量(即读取到几个 就转换几个字节)
String readedContent = new String(data,0,readedLength);
System.out.println(readedContent); // 打印每次读取到的内容
}
// 读取中文内容
// 读取中文内容时,需要注意文件内容的编码格式
// GBK编码时,一个字符占2个字节;
// UTF-8编码时,一 个字符占3个字节;
public static void main(String[] args) throws IOException {
// 创建文件对象
File file1 = new File("D:\\Text file.txt");
// 创建文件读取流对象
FileInputStream inputStream = null;
try{
// 创建输入流对象
inputStream = new FileInputStream(file1);
// 调用 available 方法获取可读内容长度
int availableNum = inputStream.available();
System.out.println("获取可读取内容长度: " + availableNum);
// 创建字节数组,长度是获取到的可读内容长度(一次读完)
byte[] data = new byte[availableNum];
// 初始化已读取的内容长度
int readedLength = -1;
// 循环遍历
while((readedLength = inputStream.read(data)) != -1){ // 当读取到文件内容的末尾时,返回值为-1
// 使用String类中的构造方法将 字节数组转为 String类型
// 第一个参数 byte数组,第二个参数 从0开始转换,第三个参数 转换的数量(即读取到几个 就转换几个字节)
System.out.println("已读取内容长度:" + readedLength);
String readedContent = new String(data,0,readedLength);
System.out.println("读取到的内容:" + readedContent); // 打印每次读取到的内容
}
}catch (IOException e){
// 打印异常堆 栈信息
e.printStackTrace();
}finally {
// 关闭资源
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
close()
作用:关闭输入流资源,需要先做非空判断。
参数:无
返回值:无
示例:
// 创建文件读取流对象
FileInputStream inputStream = null;
try{
// 创建输入流对象
inputStream = new FileInputStream(file1);
...
}catch (IOException e){
// 打印异常堆栈信息
e.printStackTrace();
}finally {
// 关闭资源
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
available()
作用:获取可读内容长度。
参数:无
返回值:int类型,输入流可读长度
示例:
File file1 = new File("D:\\Text file.txt");
FileInputStream inputStream = null;
// 创建输入流对象
inputStream = new FileInputStream(file1);
// 调用 available 方法获取可读内容长度
int availableNum = inputStream.available();
OutputStream类
java.io.OutputStream包名,这个抽象类是表示字节输出流的所有类的超类,输出流接收输出字节并将其发送到某个接收器。是抽象类。
构造方法
无
方法
write()
详见 FileOutputStream类(子类)中同名方法
close()
详见 FileOutputStream类(子类)中同名方法
FileOutputStream类
String类中的 实例方法中把 字符串转为 byte类型的数组(字节数组)
构造方法
// 直接写一个对应的文件的对象作为参数传入
// 创建文件对象
File file = new File("D:\\Text file.txt");
// 创建文件输入流对象
FileOutputStream OutputStream = new FileOutputStream(file);
// 可以传入写入文件的路径
FileOutputStream OutputStream1 = new FileOutputStream("D:\\Text file.txt");
// 传入文件的路径,同时设置一个布尔值表示是否可以在原有文件中追加内容,否则默认则是覆盖之前的内容。
FileOutputStream OutputStream2 = new FileOutputStream("D:\\Text file.txt",true);
// 如果文件不存在,则会自动创建这个文件。
方法
write()
作用:写入文件内容。
参数:
当参数为 int类型时,表示每次写入一个字节(的ASCII码);
当参数为 byte[] (字节数组)时,表示每次写入一个字节数组(数组元素的ASCII码组成的字符串);
返回值:无
示例:
// 写入中文,需要把先把汉字转为字节数组
// 创建文件输入流对象
FileOutputStream outputStream = new FileOutputStream("D:\\Text file1.txt",true);
// 创建字符传
String str = "争渡,争渡,惊起一滩鸥鹭";
// 调用 字符串实例的 getBytes方法 将字符串转为 字节数组
byte[] data = str.getBytes();
// 写入
outputStream.write(data);
// 关闭流
outputStream.close();
// 写一个字节
// 创建文件输入流对象
FileOutputStream outputStream = new FileOutputStream("D:\\Text file1.txt",true);
// 写入一个字节
outputStream.write(97); // a
// 写入一个字节数组
// 创建文件输入流对象
FileOutputStream outputStream = new FileOutputStream("D:\\Text file1.txt",true);
// 创建一个字节数组
byte [] data = {102,65,97};
// 写入
outputStream.write(data);
// 关闭流
outputStream.close();
close()
作用:关闭文件写入流资源。
参数:无
返回值:无
示例:
// 创建文件输入流对象
FileOutputStream outputStream = new FileOutputStream("D:\\Text file1.txt",true);
// 关闭流
outputStream.close();
字符流
Reader类
用于读取字符流的抽象类
构造方法
无
方法
read()
详见 InputStreamReader类(子类)同名方法使用
close()
详见 InputStreamReader类(子类)同名方法使用
InputStreamReader类
构造方法
// 可以指定编码格式读取文件,第一个参数是 inputStream对象,第二个参数是 字符编码
InputStreamReader(fileInputStream,"GBK"); // 常用于解决编码不一致,乱码问题
// 解决读取中文乱码问题(编码不一致)
inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");
// IDEA默认读取文件的格式为 UTF-8 格式,源文件的格式为ANSI(GBK),就会出现乱码。
方法
read()
作用:读取字符。
参数:
当参数为一个字节流输入(读取)对象时,表示每次读取一个字符;
当参数为一个char[] char数组时,表示每次读取一个char数组;
返回值:
当参数为一个字节流输入(读取)对象时,每次读取一个字符,返回值Int类型(字符的ASCII码);
当每次读取一个char数组,返回值为int类型,char数组的长度,读取的数据存储在char参数中;
示例:
// 读取一个字符
// 初始化 文件输入流对象
FileInputStream fileInputStream = null;
// 初始化 字符读取流对象
InputStreamReader inputStreamReader = null;
try {
// 创建对象
fileInputStream = new FileInputStream("D:\\Text file.txt");
inputStreamReader = new InputStreamReader(fileInputStream);
// 每次读取一个字符
int read = inputStreamReader.read();
// 读取字符的 ASCII 码表示
System.out.println("read = " + read); // 字母I:73;汉字常:24120
}catch (IOException e){
// 捕获异常
e.printStackTrace();
} finally {
}
// 每次读取char 数组长度
// 创建对象
fileInputStream = new FileInputStream("D:\\Text file.txt");
inputStreamReader = new InputStreamReader(fileInputStream);
// 每次读取一个字符数组
char [] data = new char[5];
// 初始化 读取到内容的长度
int readedLength = -1;
// 遍历读取
while ((readedLength = inputStreamReader.read(data)) != -1){
// 方式1:直接使用 println方法中默认对char数组的遍历方法
System.out.println(data);
// 方式2:使用String类中的构造方法,将char数组转为String类型
String strFragment = new String(data, 0, readedLength);
System.out.println(strFragment);// 字符片段,每5个字符遍历一次打印一行
}
close():在多个资源需要关闭的时候,需要遵循的原则是先用后关。
作用:关闭字符读取流。
参数:无
返回值:无
示例:
package com.inputStreamPart;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestInputStreamReader {
public static void main(String[] args){
// 初始化 文件输入流对象
FileInputStream fileInputStream = null;
// 初始化 字符读取流对象
InputStreamReader inputStreamReader = null;
try {
// 创建对象
fileInputStream = new FileInputStream("D:\\Text file.txt");
inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");
// 每次读取一个字符数组
char [] data = new char[5];
// 初始化 读取到内容的长度
int readedLength = -1;
// 遍历读取
while ((readedLength = inputStreamReader.read(data)) != -1){
// 方式1:直接使用 println方法中默认对char数组的遍历方法
System.out.println(data);
// 方式2:使用String类中的构造方法,将char数组转为String类型
String strFragment = new String(data, 0, readedLength);
System.out.println(strFragment);// 字符片段,每5个字符遍历一次打印一行
}
}catch (IOException e){
// 捕获异常
e.printStackTrace();
} finally {
// 关闭资源
try{
// 在关闭之前做非空判断 因为如果为空 调用close方法 将出现空指针异常
if(inputStreamReader != null){
inputStreamReader.close();
}
if(fileInputStream != null){
fileInputStream.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
Write类
构造方法
方法
write()
close()