创建文件对象相关构造器和方法
package com.peanut.file;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) {
}
//方式1:new File(String pathname)
@Test
public void create01() {
String filePath = "E:\\news1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("success1");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式2 new File(File parent,String child) 根据父目录文件+子路径构建
@Test
public void create02() {
File parentFile = new File("E:\\");
String fileName = "news2.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("success2");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式3 new File(String parent,String child) 根据父目录文件+子路径构建
@Test
public void create03() {
String parnetPath = "E:\\";
String fileName = "news3.txt";
File file = new File(parnetPath, fileName);
try {
file.createNewFile();
System.out.println("success3");
} catch (IOException e) {
e.printStackTrace();
}
}
}
获取文件相关信息
//获取文件信息
@Test
public void Info() {
File file = new File("E:\\news1.txt");
//文件名字
System.out.println("文件名字 " + file.getName());
//文件绝对路径
System.out.println("文件绝对路径 " + file.getAbsolutePath());
//文件父级目录
System.out.println("文件父级目录 " + file.getParent());
//文件大小
System.out.println("文件大小 " + file.length());
//文件是否存在
System.out.println("文件是否存在 " + file.exists());
//是否是一个文件
System.out.println("是否是一个文件 " + file.isFile());
//是否是一个文件夹
System.out.println("是否是一个文件夹 " + file.isDirectory());
}
}
目录操作
//判断 E;\\news1.txt是否存在,存在就删除 目录也被当作文件
@Test
public void t1() {
String filePath = "E:\\news1.txt";
File file = new File(filePath);
if (file.exists()) {
if (file.delete()) {
System.out.println(filePath + " delete success");
} else {
System.out.println(filePath + " failed");
}
} else {
System.out.println("do not find");
}
}
//判断 E:\\a\\b\\c是否存在,存在就提示已存在,不存在则创建
@Test
public void t2() {
String directoryPath = "E:\\a\\b\\c";
File file = new File(directoryPath);
if (file.exists()) {
System.out.println(directoryPath + " has existed");
} else {
if (file.mkdirs()) { //mkdir创建一级目录
System.out.println(directoryPath + " has created");
} else {
System.out.println("failed");
}
}
}
}
IO流原理及流的分类
派生类
FileInputStream
文件输入流
//单个字节读取read() int
@Test
public void readFile01()throws IOException {
String filePath = "E:\\hello.txt";
int readData = 0;
//字节数组
FileInputStream fileInputStream=null;
fileInputStream = new FileInputStream(filePath);
//返回-1表示读取完毕
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char) readData);
}
fileInputStream.close();
}
}
------------------------------------------------------------------
//使用read(byte[] b)
@Test
public void readFile02() throws IOException {
String filePath = "E:\\hello.txt";
//字节数组
byte[] buf = new byte[8];
int readLen = 0;
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(filePath);
//从该输入流最多读取b.length字节的数据到数组
//返回-1表示读取完毕
//如果读取正常返回实际读取字节数
while ((readLen = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf,0,readLen));
}
fileInputStream.close();
}
FileOutputStream
文件输出流
@Test
//将数据写入到文件中,如果文件不存在则创建文件
public void writeFile() throws IOException {
//创建对象
String filePath = "E:\\a.txt";
FileOutputStream fileOutputStream = null;
//得到对象
//1.new FileOutputStream(filePath)创建方式,写入会覆盖原来内容
//2.new FileOutputStream(filePath,true)创建方式,写入会追加内容,不会覆盖
fileOutputStream = new FileOutputStream(filePath, true);
//写入一个字节 fileOutputStream.write('H');
/* String str = "hello,world!";//写入字符串
//.getBytes()可以把字符串-->字节数组
fileOutputStream.write(str.getBytes());*/
String str = "hello,world!";
//write(byte[]b int off,int len)将len字节从位于偏移量off的指定字节数组写入此文件输出流
fileOutputStream.write(str.getBytes(), 0, str.length() - 1);
fileOutputStream.close();
}
应用实例–Filecopy
public class FileCopy {
public static void main(String[] args) throws IOException {
//1.创建输入流,将图片读入到程序
//2.创建输出流,将数据写入指定文件中
String srcFilePath = "E:\\1.png";
String copFilePath = "D:\\1.png";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(copFilePath);
//定义一个字节数组
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1) {
//读取到后,写入文件
fileOutputStream.write(buf,0,readLen);
}
System.out.println("success");
if (fileInputStream!=null){
fileInputStream.close();
}
if (fileOutputStream!=null){
fileOutputStream.close();
}
}
}
节点流
FileReader
案例
@Test
//单个字符读取
public void readFile01() throws IOException {
FileReader fileReader = null;
String filePath = "E:\\2.txt";
int data = 0;
fileReader = new FileReader(filePath);
//循环读取
while ((data = fileReader.read()) != -1) {
System.out.print((char) data);
}
if (fileReader != null) {
fileReader.close();
}
}
@Test
//字符数组读取
public void readFile02() throws IOException {
FileReader fileReader = null;
String filePath = "E:\\2.txt";
int readLen = 0;
char[] buf = new char[8];
fileReader = new FileReader(filePath);
//循环读取,使用read(buf),返回的是实际读取到的字符数
while ((readLen = fileReader.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));
}
if (fileReader != null) {
fileReader.close();
}
}
FileWriter
使用之后必须关闭流
案例
public static void main(String[] args) throws IOException {
String filePath = "E:\\2.txt";
char[] chars = {'1', '2', '3'}; //2.3.4
FileWriter fileWriter = null;
fileWriter = new FileWriter(filePath);
//writer(int)写入单个字符 fileWriter.write('H');
//2.writer(char[])写入指定数组 fileWriter.write(chars);
//3.writer(char[],off,len)写入指定数组的指定部分 fileWriter.write("NB666".toCharArray(),0,3);
//4.writer(String)写入指定字符串 fileWriter.write("you are perfect");
//writer(String,off,len)写入指定字符串的指定部分 fileWriter.write("北上广深",0,2);
fileWriter.close();
System.out.println("E N D");
}
处理流
BufferedReader
public static void main(String[] args) throws IOException {
String filePath = "E:\\2.txt";
//创建bufferedReader
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
//读取
String line;//按行读取
//bufferedReader.readLine()是按行读取文件,返回值为null时,表示文件读取完毕
while (( line=bufferedReader.readLine())!=null){
System.out.println(line);
}
//关闭外部流
bufferedReader.close();
}
BufferedWriter
public static void main(String[] args) throws IOException {
String filePath = "E:\\2.txt";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath, true));//不覆盖
bufferedWriter.write("Hello,peanut is here@@");
bufferedWriter.newLine();//插入一个换行
bufferedWriter.write("Hello,peanut was here@@");
bufferedWriter.newLine();//插入一个换行
System.out.println("SUCCESS");
bufferedWriter.close();
}
用BufferedReader和BufferedWirter完成文件拷贝
操作二进制文件可能导致文件损坏
public static void main(String[] args) throws IOException {
String srcPath = "E:\\1.txt";
String desPath = "E:\\2.txt";
BufferedReader br = null;
BufferedWriter bw = null;
String line;
br = new BufferedReader(new FileReader(srcPath));
bw = new BufferedWriter(new FileWriter(desPath));
//边读边写
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
System.out.println("SUCCESS");
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
}
用BufferedInputStream和BufferedOutputStream拷贝图片音乐等文件二进制
public static void main(String[] args) throws IOException {
String srcPath = "E:\\1.jpg";
String desPath = "E:\\2.jpg";
//创建对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
bis = new BufferedInputStream(new FileInputStream(srcPath));
bos = new BufferedOutputStream(new FileOutputStream(desPath));
//循环的读取文件,写入到desPath中
byte[] buff = new byte[1024];
int readLen = 0;
while ((readLen = bis.read(buff)) != -1) {
bos.write(buff, 0, readLen);
}
System.out.println("success!");
if (bis!=null){
bis.close();
}
if (bos!=null){
bos.close();
}
}
对象流
案例
序列化数据
public static void main(String[] args) throws IOException {
//序列化后,文件的保存格式不是文本
String filePath = "E:\\a.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
//序列化数据
oos.writeInt(100);//int-->Integer(实现了Serialization接口)
oos.writeBoolean(true);
oos.writeChar('a');
oos.writeDouble(9.2);
oos.writeUTF("nb666");
oos.writeObject(new Dog("NB",10) );
oos.close();
System.out.println("success(序列化)");
}
}
//如果需要序列化某个类的对象,必须实现Serializable
class Dog implements Serializable {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
案例
反序列化恢复数据
public static void main(String[] args) throws IOException, ClassNotFoundException {
//指定反序列化文件
String filePath = "E:\\a.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
//按输入顺序-->输出
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readChar());
System.out.println(ois.readDouble());
System.out.println(ois.readUTF());
Object dog = ois.readObject();
System.out.println("运行类型=" + dog.getClass());
System.out.println("信息:" + dog);//底层 Object-->Dog
//如果调用dog方法,需要向下转型
ois.close();
}
标准输入输出流
转换流
InputStreamReader
public static void main(String[] args) throws IOException {
String filePath = "E:\\2.txt";
//指定编码 new FileInputStream转成 InputStreamReader
InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");
//把InputStreamReader传入BufferedReader
BufferedReader br = new BufferedReader(isr);
String s =br.readLine();
System.out.println("读取到内容:"+s);
br.close();
}
OutputStreamWriter
public static void main(String[] args) throws IOException {
String filePath = "E:\\1.txt";
String charSet = "gbk";
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), charSet);
osw.write("hi,peanut牛啊");
osw.close();
System.out.println("按照"+charSet+"保存文件");
}
打印流
打印流只有输出流,没有输入流
Properties类
读取文件
修改配置文件