一、学习内容
1)java.io.File类的使用
2)IO原理及流的分类
3)文件流
FileInputStream / FileOutputStream / FileReader / FileWriter
4)缓冲流
BufferedInputStream / BufferedOutputStream /
BufferedReader / BufferedWriter
5)转换流
InputStreamReader / OutputStreamWriter
6)标准输入/输出流
7)打印流(了解)
PrintStream / PrintWriter
8)数据流(了解)
DataInputStream / DataOutputStream
9)对象流 ----涉及序列化、反序列化
ObjectInputStream / ObjectOutputStream
10)随机存取文件流
RandomAccessFile
二、File类的使用
package com.xijian.java;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.junit.jupiter.api.Test;
/*
* java.io.File
* 1.凡是与输入、输出相关的类、接口都定义在java.io包下
* 2.File是一个类,可以有构造器创建其对象,此对象对应着一个文件(.txt .avi .doc .ppt……)或文件目录
* 3.File类对象是与平台无关的。
* 4.File中的方法,仅涉及到如何创建、删除、重命名等等。只要设计文件内容的,File是无能为力的。必须由io流来完成
* 5.File类的对象常作为io流具体类的构造器的形参
*/
public class TestFile {
/*
* createNewFile() //创建一个新的文件
* delete() //删除文件
* mkDir()//创建一个目录
* mkDirs() //创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
* list() //返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
* listFiles()//返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
*
*/
@Test
public void test3() throws IOException {
File file1 = new File("d:/io/helloworld.txt");
System.out.println(file1.delete());//删除文件
if(!file1.exists()){
boolean b = file1.createNewFile();//创建一个新的文件
System.out.println(b);
}
File file2 = new File("d:\\io1\\io2");
if(!file2.exists()){
boolean b = file2.mkdirs();//创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
System.out.println(b);
}
File file3 = new File("d:\\teach");
String[] strs = file3.list();//返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
for(int i = 0;i < strs.length;i++){
System.out.println(strs[i]);
}
System.out.println();
File[] files = file3.listFiles();//返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
for(int i = 0;i < files.length;i++){
System.out.println(files[i].getName());
}
}
/*
* exists()判断是否存在
canWrite()判断是否可写
canRead()判断是否可读
isFile()判断是否是个文件
isDirectory()判断是否是个目录
lastModified()返回最后一次修改时间
length()返回长度
*/
@Test
public void test2() {
File file1 = new File("d:\\io\\helloworld.txt");
File file3 = new File("d:\\io\\io1");
System.out.println(file1.exists());
System.out.println(file1.canWrite());
System.out.println(file1.canRead());
System.out.println(file1.isFile());
System.out.println(file1.isDirectory());
System.out.println(new Date(file1.lastModified()));
System.out.println(file1.length());
}
/*
* 路径:
* 绝对路径:包括盘符在内的完整的文件路径
* 相对路径:在当前文件目录下的文件的路径
*
* getName()获取file1对象所指向文件或者文件夹的名字
* getPath()获取file1对象所指向文件或者文件夹的路径
* getAbsoluteFile()返回此抽象路径名的绝对路径名形式。
* getAbsolutePath()返回此抽象路径名的绝对路径名字符串
* getParent()返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
* renameTo(File newName)
*/
@Test
public void test1() {
File file1 = new File("d:\\io\\helloworld.txt");//构造器中的是绝对路径
File file2 = new File("hello.txt");//构造其中的是相对路径
File file3 = new File("d:\\io\\io1");//构造器中的是个文件夹
File file4 = new File("d:\\io");
System.out.println(file1.getName());
System.out.println(file1.getPath());
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getParent());
System.out.println(file1.getAbsolutePath());
System.out.println();
System.out.println(file3.getName());
System.out.println(file3.getPath());
System.out.println(file3.getAbsoluteFile());
System.out.println(file3.getParent());
System.out.println(file3.getAbsolutePath());
//renameTo(File newName):重命名
//file1.renameTo(file2):file1重命名为file2,要求:file1文件一定存在,file2一定不存在
System.out.println(file1.renameTo(file4));
}
}
三、IO流概述
1)按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
2)按数据流的流向不同分为:输入流,输出流
3)按流的角色的不同分为:节点流,处理流
Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。
由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。
四、FileInputStream的使用
package com.xijian.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class TestFileInputStream {
@Test//一般用FileInputStream流的read(byte[] b);方法进行读取操作
public void testFileInputStream3() { // abcdefgcde
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file);
byte[] b = new byte[5];// 读取到的数据要写入的数组。
int len;// 每次读入到byte中的字节的长度
while ((len = fis.read(b)) != -1) {//len是此次读取出的字节数
// for (int i = 0; i < len; i++) {
// System.out.print((char) b[i]);
// }
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();//关闭节点流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 使用try-catch的方式处理如下的异常更合理:保证流的关闭操作一定可以执行
@Test
public void testFileInputStream2() {
// 2.创建一个FileInputStream类的对象
FileInputStream fis = null;
try {
// 1.创建一个File类的对象。
File file = new File("hello.txt");
fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取。
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭相应的流
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 从硬盘存在的一个文件中,读取其内容到程序中。使用FileInputStream
// 要读取的文件一定要存在。否则抛FileNotFoundException
@Test
public void testFileInputStream1() throws Exception {
// 1.创建一个File类的对象。
File file = new File("hello.txt");
// 2.创建一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取。
/*
* read():读取文件的一个字节。当执行到文件结尾时,返回-1
*/
// int b = fis.read();
// while(b != -1){
// System.out.print((char)b);
// b = fis.read();
// }
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
// 4.关闭相应的流
fis.close();
}
}
五、FileOutputStream的使用(字节流)
package com.xijian.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class TestFileInputStream {
// 实现文件复制的方法
public void copyFile(String src, String dest) {
// 1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 从硬盘读取一个文件,并写入到另一个位置。(相当于文件的复制)
@Test
public void testFileInputOutputStream() {
// 1.提供读入、写出的文件
File file1 = new File("C:\\Users\\shkstart\\Desktop\\1.jpg");
File file2 = new File("C:\\Users\\shkstart\\Desktop\\2.jpg");
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制
byte[] b = new byte[20];
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// FileOutputStream
@Test
public void testFileOutputStream() {
// 1.创建一个File对象,表明要写入的文件位置。
// 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
File file = new File("hello2.txt");
// 2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
// 3.写入的操作
fos.write(new String("I love China!").getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4.关闭输出流
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
六、FileReader FileWriter(字符流)
package com.atguigu.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
/*
* 使用FileReader、FileWriter 可以实现文本文件的复制。
* 对于非文本文件(视频文件、音频文件、图片),只能使用字节流!
*/
public class TestFileReaderWriter {
@Test
public void testFileReaderWriter(){
//1.输入流对应的文件src一定要存在,否则抛异常。输出流对应的文件dest可以不存在,执行过程中会自动创建
FileReader fr = null;
FileWriter fw = null;
try{
//不能实现非文本文件的复制
// File src = new File("C:\\Users\\shkstart\\Desktop\\1.jpg");
// File dest = new File("C:\\Users\\shkstart\\Desktop\\3.jpg");
File src = new File("dbcp.txt");
File dest = new File("dbcp1.txt");
//2.
fr = new FileReader(src);
fw = new FileWriter(dest);
//3.
char[] c = new char[24];
int len;
while((len = fr.read(c)) != -1){
fw.write(c, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fr != null){
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void testFileReader(){
FileReader fr = null;
try {
File file = new File("dbcp.txt");
fr = new FileReader(file);
char[] c = new char[24];
int len;
while((len = fr.read(c)) != -1){
String str = new String(c, 0, len);
System.out.print(str);
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fr != null){
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}