代码链接:https://download.csdn.net/download/qq_52354698/86404845?spm=1001.2014.3001.5501
文件在程序中是以流的形式来操作的

流:数据在数据源(文件)和程序(内存)之间经历的路径
输入流:数据从数据源(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源(文件)的路径
new File(String pathname)//根据路径创建一个File对象
new File(File parent, String child)//根据父目录文件+子路径构建
new File(String parent,String child)//根据父目录+子路径构建
getName();//得到文件名字
getAbsolutePath();//得到文件绝对路径
getParent();//得到文件父级目录
length();//得到文件大小(字节)
exists();//检查文件是否存在
isFile();//检查是否是一个文件
isDirectory();//检查是否是一个目录
mkdir();//创建一级目录
mkdirs();//创建多级目录
delete();//删除空目录或文件



InputStream抽象类是所有类字节输入流的超类
InputStream 常用的子类



package com.qdu.inputstream;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author dell
* @version 1.0
*/
public class FileInputStream_ {
public static void main(String[] args) {
}
@Test
public void readFile01() throws IOException {
String filePath = "e:\\hello.txt";
int readDate = 0;
FileInputStream fileInputStream = new FileInputStream(filePath);
while ((readDate = fileInputStream.read()) != -1) {
System.out.print((char) readDate);
}
fileInputStream.close();
System.out.println("");
}
@Test
public void readFile02() throws IOException {
String filePath = "e:\\hello.txt";
byte[] bytes = new byte[8];//最多只能读取8个字节
int readLen = 0;
FileInputStream fileInputStream = new FileInputStream(filePath);
while ((readLen = fileInputStream.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, readLen));
}
fileInputStream.close();
}
}

使用 FileOutputStream 将数据写入到文件中,如果该文件不存在,则创建该文件
package com.qdu.outputstream;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author dell
* @version 1.0
*/
public class FileOutputStream01 {
public static void main(String[] args) {
}
@Test
public void writeFile() throws IOException {
String filePath = "e:\\a.txt";
FileOutputStream fileOutputStream = new FileOutputStream(filePath);//写入内容是覆盖之前的内容
// FileOutputStream fileOutputStream = new FileOutputStream(filePath, true);//写入的内容是追加到之前的内容
fileOutputStream.write('H');//写入一个字节
String string = "hello,world!";//写入一个字符串
fileOutputStream.write(string.getBytes());//字符串->字节数组
String str = "qdu,world!";
fileOutputStream.write(str.getBytes(), 0, 3);//写入指定长度的字符串
fileOutputStream.close();
}
}
- 创建文件的输入流,将文件读入到程序
- 创建文件的输出流,将读取到文件数据,写入到指定的文件
package com.qdu.copy;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @author dell
* @version 1.0
*/
public class FileCopy {
public static void main(String[] args) {
}
@Test
public void fileCopy() throws IOException {
String startPath = "e:\\hello.txt";
String endPath = "e:\\hello1.txt";
FileInputStream fileInputStream = new FileInputStream(startPath);
FileOutputStream fileOutputStream = new FileOutputStream(endPath);
byte[] bytes = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, readLen);
}
System.out.println("拷贝成功...");
if (fileInputStream != null) fileInputStream.close();
if (fileOutputStream != null) fileOutputStream.close();
}
}
拷贝到c盘可能存在访问权限的问题…
FIleReader和FileWriter是字符流,即按照字符来操作io

new FileReader(File/String)
read:每次读取单个字符,返回该字符,如果到文件末尾返回-1
read(char[]):批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾返回-1
new String(char[]):将char[]转换成String
new String(char[], off, len):将char[]的指定部分转换成String
new FileWriter(File/String):覆盖模式,相当于流的指针在首端
new FileWriter(File/String,true):追加模式,相当于流的指针在尾端
write(int):写入单个字符
write(char[]):写入指定数组
write(char[],off,len):写入指定数组的指定部分
write(string):写入整个字符串
write(string,off,len):写入字符串的指定部分
FileWriter使用后,必须要关闭(close)或刷新(flush),否则,写入不到指定的文件!
结点流可以从一个特定的数据源读写数据,如FileReader、FileWriter

处理流(也叫包装流)是“链接”在已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,也更加灵活,如BufferedReader、BufferedWriter

BufferedReader和BufferedWriter 属于字符流,是按照字符来读取数据的
关闭处理流,只需要关闭外层流即可
使用BufferedReader读取文件
package com.qdu.reader_;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* @author dell
* @version 1.0
*/
public class Reader_ {
public static void main(String[] args) {
}
@Test
public void BufferedReader_() throws IOException {
String filePath = "e:\\hello.txt";
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
}
使用BufferedWriter写入文件
package com.qdu.writer_;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author dell
* @version 1.0
*/
public class BufferedWriter_ {
public static void main(String[] args) throws IOException {
String filePath = "e:\\hello1.txt";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write("hello1");
bufferedWriter.newLine();//换行
bufferedWriter.write("hello2");
bufferedWriter.newLine();
bufferedWriter.write("hello3");
bufferedWriter.close();
}
}
使用BufferedReader和BufferedWriter完成文本文件拷贝
package com.qdu.copy;
import java.io.*;
/**
* @author dell
* @version 1.0
*/
public class BufferCopy {
public static void main(String[] args) throws IOException {
String srcFilePath = "e:\\a.txt";
String destFilePath = "e:\\a1.txt";
BufferedReader bufferedReader = new BufferedReader(new FileReader(srcFilePath));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFilePath));
String line;
//读取一行时,不会读入换行符
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
if (bufferedWriter != null) bufferedWriter.close();
if (bufferedReader != null) bufferedReader.close();
}
}
BufferedInputStream和BufferedOutputStream属于字节流,实现缓冲的输出输入流
完成图片、音乐的拷贝
图片、音乐属于二进制文件因此只用字节流来处理
能够对基本数据类型或对象进行序列化和反序列化
序列化和反序列化

提供了对基本类型或对象类型的序列化和反序列化的方法
使用ObjectOutputStream序列化基本数据类型和一个Dog对象(name,age),并保存到data.txt
package com.qdu.outputstream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* @author dell
* @version 1.0
*/
public class ObjectOutStream {
public static void main(String[] args) throws IOException {
String filePath = "e:\\data.txt";
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));
objectOutputStream.writeInt(100);
objectOutputStream.writeBoolean(true);
objectOutputStream.writeChar('a');
objectOutputStream.writeDouble(9.5);
objectOutputStream.writeUTF("hello,world!!!");
objectOutputStream.writeObject(new Dog("老黄", 10));
objectOutputStream.close();
}
}
class Dog implements Serializable {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
}
使用ObjectInputStream反序列化恢复数据
package com.qdu.inputstream;
import java.io.*;
/**
* @author dell
* @version 1.0
*/
public class ObjectPutStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filePath = "e:\\data.txt";
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
//反序列化的顺序要和保存数据(序列化)的顺序一致
System.out.println(objectInputStream.readInt());
System.out.println(objectInputStream.readBoolean());
System.out.println(objectInputStream.readChar());
System.out.println(objectInputStream.readDouble());
System.out.println(objectInputStream.readUTF());
System.out.println(objectInputStream.readObject());
objectInputStream.close();
}
}
class Dog implements Serializable {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}


介绍
将字节流FileInoutStream转换成字符流InputStreamReader,对文件进行读取,进而包装成BufferReader类
package com.qdu.transformation;
import java.io.*;
/**
* @author dell
* @version 1.0
*/
public class InputStreamReader_ {
public static void main(String[] args) throws IOException {
String filePath = "e:\\a.txt";
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath), "gbk");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = bufferedReader.readLine();
System.out.println(str);
bufferedReader.close();
}
}
将字节流FileOutputStream转换成字符流OutputStreamWriter,对文件进行写入
package com.qdu.transformation;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* @author dell
* @version 1.0
*/
public class OutputStreamWriter_ {
public static void main(String[] args) throws IOException {
String filePath = "e:\\qdu.txt";
String charSet = "gbk";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath), charSet);
outputStreamWriter.write("hi,青大牛皮!!!");
outputStreamWriter.close();
}
}
打印流只有输出流,没有输入流
PrintStream
package com.qdu.printstream;
import java.io.IOException;
import java.io.PrintStream;
/**
* @author dell
* @version 1.0
*/
public class PrintStream_ {
public static void main(String[] args) throws IOException {
PrintStream printStream = System.out;
//默认情况下,PrintStream输出数据的位置是标准输出,即显示器
printStream.println("hello!");
printStream.write("hi".getBytes());
printStream.close();
System.setOut(new PrintStream("e:\\f1.txt"));
System.out.println("hello,qdu!!!");
}
}

PrintWriter
package com.qdu.transformation;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author dell
* @version 1.0
*/
public class PrintWriter_ {
public static void main(String[] args) throws IOException {
PrintWriter printWriter = new PrintWriter(System.out);
printWriter.println("h1,你好!!!");
printWriter.close();
PrintWriter printWriter1 = new PrintWriter(new FileWriter("e:\\f2.txt"));
printWriter1.print("hello,你好呀!!!");
printWriter1.close();
}
}

专门用于读写配置文件的集合类
配置文件的格式:
键=值
注意:键值对不需要有空格,值不需要用引号括起来。默认类型是String
package com.qdu.properties_;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* @author dell
* @version 1.0
*/
public class Properties01 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.load(new FileReader("src\\mysql.properties"));
properties.list(System.out);
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println(user);
System.out.println(pwd);
}
}

package com.qdu.properties_;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author dell
* @version 1.0
*/
public class Properties02 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("charset", "utf-8");
properties.setProperty("user", "jack");
properties.setProperty("pwd", "abc123");
properties.store(new FileOutputStream("src\\mysql2.properties"), null);
}
}

如果文件有key,则setProperty就是修改
如果文件没有key,则setProperty就是创建