一、File
1、文件和目录路径名的抽象表示形式。如果我们需要需要得到系统的一个文件或者目录,就需要用到这个类
2、重要构造方法:
File file = null;
// 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例
file = new File("C:/Users/Eric/Desktop/Test.txt");
//根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例
file = new File("C:/Users/Eric/Desktop", "Test.txt");
3、由于java的跨平台性,所以我们在使用路径的时候通常会把"/"换成"+File.separator+",这样在不同的平台上都能正常运行。
4、文件的重要方法:
1)createNewFile(),如果当我们这个文件不存在就在本地系统新建一个
2)exists(),判断这个文件是否存在,存在返回true,不存在返回false
3)getAbsolutePath() ,获取此对象的绝对路径。
4)isDirectory() ,判断该File对象,是不是目录(文件夹)
5)isFile(),判断该File对象,是不是文件(不是文件夹)。
5、文件目录的重要方法
1)mkdir();创建目录,但是如果父目录不存在,就无法创建
2)mkdirs();创建目录,如果没有父目录,就连父目录一起创建
1、FileInputStream 从文件系统中的某个文件中获得输入字节,所以FileInputStream是每次读取一个字节。
2、构造方法:
FileInputStream(File file) 通过一个文件实例来创建一个输入流
FileInputStream(String name) 通过一个文件路径字符串来创建一个输入流
3、重要方法:
1)read() 从此输入流中读取一个数据字节。
2)read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
3)read(byte[] b, int off, int len) 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
4)上述方法都返回了一个int,表示是否读完,当int>-1表示没有读完
5、构造方法:
FileOutputStream(File file/String name[,boolean append]) 创建一个向指定 File 对象/路径表示的文件中写入数据的文件输出流。
Boolean append:true:表示会在目标文件结尾接着添加,false/不写,会覆盖目标文件
6、重要方法:
1)write(byte[] b) 将 b.length 个字节从指定 byte 数组写入此文件输出流中。
2)write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
3)write(int b) 将指定字节写入此文件输出流。
三、Reader/Writer
1、Reader和Writer可以对数据进行一个字符的读取和写入操作
2、在使用玩Reader以后记得调用close()方法关闭,在使用玩Writer后记得先flush()再close()
四、BufferedReader/BufferedWriter
1、继承自Reader的BufferedReader拥有从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取的能力。
2、重要构造方法:
BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
3、新添加方法
readLine()读取一个文本行。
4、继承自Writer的BufferedWriter拥有将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入的能力。
5、重要构造方法:
BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流。
6、重要方法
1)close() 关闭此流,但要先刷新它。
2)void flush()刷新该流的缓冲。
3)write(String s, int off, int len) 写入字符串的某一部分。
4)newLine()写入一个行分隔符。
1、DataOutputStream和DataInputStream 数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。
2、值得注意的是:
1、使用DataInputStream的目标文件要是使用DataOutputStream写的文件,不然输入的信息会出现错误。
2、DataInputStream读取的数据类型必须按照DataOutputStream输出数据的类型顺序来书写。
1、文件和目录路径名的抽象表示形式。如果我们需要需要得到系统的一个文件或者目录,就需要用到这个类
2、重要构造方法:
File file = null;
// 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例
file = new File("C:/Users/Eric/Desktop/Test.txt");
//根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例
file = new File("C:/Users/Eric/Desktop", "Test.txt");
3、由于java的跨平台性,所以我们在使用路径的时候通常会把"/"换成"+File.separator+",这样在不同的平台上都能正常运行。
4、文件的重要方法:
1)createNewFile(),如果当我们这个文件不存在就在本地系统新建一个
2)exists(),判断这个文件是否存在,存在返回true,不存在返回false
3)getAbsolutePath() ,获取此对象的绝对路径。
4)isDirectory() ,判断该File对象,是不是目录(文件夹)
5)isFile(),判断该File对象,是不是文件(不是文件夹)。
public static void main(String[] args) {
File file = null;
// 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例
file = new File("C:/Users/Eric/Desktop/Test.txt");
// 判断此文件是否存在
if (!file.exists()) {
System.out.println("此文件是否存在:"+file.exists());
try {
// 不存在的话就在本地系统创建一个
boolean bool= file.createNewFile();
System.out.println("是否创建成功:"+bool);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("得到此文件的绝对路径:"+file.getAbsolutePath());
System.out.println("此文件是否为文件:"+file.isFile());
}
5、文件目录的重要方法
1)mkdir();创建目录,但是如果父目录不存在,就无法创建
2)mkdirs();创建目录,如果没有父目录,就连父目录一起创建
3)list(),listFile(); 获取出子文件。
public static void main(String[] args) {
File file = null;
// 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例
file = new File("C:/Users/Eric/Desktop/Test");
// 得到此路径下的文件和文件目录
String[] fileList = file.list();
for (String str : fileList) {
System.out.println(str);
}
// 得到此路径下的文件和文件目录的对象
File[] fileList2 = file.listFiles();
for (File str2 : fileList2) {
System.out.println("路径:" + str2.getAbsolutePath());
}
}
二、FileInputStream/FileOutputStream
1、FileInputStream 从文件系统中的某个文件中获得输入字节,所以FileInputStream是每次读取一个字节。
2、构造方法:
FileInputStream(File file) 通过一个文件实例来创建一个输入流
FileInputStream(String name) 通过一个文件路径字符串来创建一个输入流
3、重要方法:
1)read() 从此输入流中读取一个数据字节。
2)read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
3)read(byte[] b, int off, int len) 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
4)上述方法都返回了一个int,表示是否读完,当int>-1表示没有读完
//找到这个文件
File file=new File(fileUri);
try {
//创建一个输入流
FileInputStream fileInputStream=new FileInputStream(file);
//创建一个字节数组
byte[] byteContent=new byte[1024];
//将文件内容装入数组
fileInputStream.read(byteContent, 0, byteContent.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
4、FileOutputStream 文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。
5、构造方法:
FileOutputStream(File file/String name[,boolean append]) 创建一个向指定 File 对象/路径表示的文件中写入数据的文件输出流。
Boolean append:true:表示会在目标文件结尾接着添加,false/不写,会覆盖目标文件
6、重要方法:
1)write(byte[] b) 将 b.length 个字节从指定 byte 数组写入此文件输出流中。
2)write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
3)write(int b) 将指定字节写入此文件输出流。
// 创建一个新文件
File file = new File("D:/CopyFile/Text.txt");
try {
// 创建一个输出流
fileOutputStream = new FileOutputStream(file);
// 向文件中写入数据
fileOutputStream.write(byteContent, 0, byteContent.length);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
// 关闭输出流
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、Reader/Writer
1、Reader和Writer可以对数据进行一个字符的读取和写入操作
2、在使用玩Reader以后记得调用close()方法关闭,在使用玩Writer后记得先flush()再close()
四、BufferedReader/BufferedWriter
1、继承自Reader的BufferedReader拥有从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取的能力。
2、重要构造方法:
BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
3、新添加方法
readLine()读取一个文本行。
4、继承自Writer的BufferedWriter拥有将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入的能力。
5、重要构造方法:
BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流。
6、重要方法
1)close() 关闭此流,但要先刷新它。
2)void flush()刷新该流的缓冲。
3)write(String s, int off, int len) 写入字符串的某一部分。
4)newLine()写入一个行分隔符。
public static void main(String[] args) {
BufferedReader reader = null;
BufferedWriter writer = null;
try {
// 实例化BufferedReader
reader = new BufferedReader(new FileReader(
"C:/Users/Eric/Desktop/Test.txt"));
writer = new BufferedWriter(new FileWriter(
"C:/Users/Eric/Desktop/Tests.txt", true));
// 判断reader是否准备好
if (reader.ready()) {
String str = null;
// 读取一行数据
while ((str = reader.readLine()) != null) {
// 输出字符串
writer.write(str);
// 换行
writer.newLine();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 判断是否为空
if (reader != null) {
try {
// 关闭
reader.close();
if (writer != null) {
// 刷新
writer.flush();
// 关闭
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五、DataOutputStream/DataInputStream
1、DataOutputStream和DataInputStream 数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。
2、值得注意的是:
1、使用DataInputStream的目标文件要是使用DataOutputStream写的文件,不然输入的信息会出现错误。
2、DataInputStream读取的数据类型必须按照DataOutputStream输出数据的类型顺序来书写。
实例:
public static void main(String[] args) {
// 数据输入
DataInputStream dataInputStream = null;
// 数据输出
DataOutputStream outputStream = null;
try {
// 实例化
dataInputStream = new DataInputStream(new FileInputStream(
"C:/Users/Eric/Desktop/Read.txt"));
// 实例化
outputStream = new DataOutputStream(new FileOutputStream(
"C:/Users/Eric/Desktop/Read.txt"));
// 输出数据
outputStream.writeBoolean(false);
outputStream.writeChars("Hello");
outputStream.writeInt(100);
// 输入数据
dataInputStream.readBoolean();
dataInputStream.readChar();
dataInputStream.readInt();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}