个人理解:文件的输入输出流式基于java内存而言,比如说文件读入java内存就是输入流,从java内存写进磁盘就是输出流
File
package com.Zh;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
/**
* @author OZH
* @Description:创建文件的三种方式
* @date 2022/3/5 18:58
*/
public class fileDemo {
@Test
public void create01() {
String filePath = "F:\\OpenCode\\IO\\FileUpload\\create01.txt";
File file = new File(filePath);//在内存中
try {
file.createNewFile();//创建在磁盘上
System.out.println("创建文件成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式2 new File(File parent ,String child)//根据父目录+子路径构建
@Test
public void create02() {
File parentFile = new File("F:\\OpenCode\\IO\\FileUpload");
String fileName = "create02.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("创建成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test//第三种方式创建文件
public void create03() {
//这个方法和上面那个方法参数不同(重载)
File file = new File("F:\\OpenCode\\IO\\FileUpload", "create03.txt");
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("文件创建成功!");
e.printStackTrace();
}
}
}
package com.Zh;
import org.junit.Test;
import java.io.File;
/**
* @author OZH
* @Description:
* @date 2022/3/5 19:25
*/
public class FileInformation {
public static void main(String[] args) {
}
//获取文件的信息
@Test
public void info() {
//先创建文件对象
File file = new File("F:\\OpenCode\\IO\\FileUpload\\create01.txt");
//调用相应的方法,得到对应信息
long length = file.length();
System.out.println("文件绝对路径=" + file.getAbsolutePath());
System.out.println("文件名字=" + file.getName());
System.out.println("文件父级目录=" + file.getParent());
System.out.println("文件大小(字节)=" + file.length());
System.out.println("文件是否是一个文件=" + file.isFile());
System.out.println("文件是否是一个文件夹=" + file.isDirectory());
String name = file.getName();
String[] split = name.split("\\.");
System.out.println("split[0].toString() = " + split[0]);
}
}
package com.Zh;
import org.junit.Test;
import java.io.File;
/**
* @author OZH
* @Description:
* @date 2022/3/5 19:46
*/
public class Directory_ {
public static void main(String[] args) {
}
//判断 文件是否存在,如果存在则删除
@Test
public void m1() {
File file = new File("F:\\OpenCode\\IO\\FileUpload\\create01.txt");
if (file.exists()) {
System.out.println("文件存在,删除");
boolean delete = file.delete();
if (delete) {
System.out.println("文件删除成功");
} else {
System.out.println("文件删除失败");
}
} else {
System.out.println("该文件不存在............");
}
}
// /判断 目录是否存在,如果存在则删除
@Test
public void m2() {
File file = new File("F:\\OpenCode\\IO\\FileUpload\\heh");
if (file.exists()) {
System.out.println("目录存在,删除");
boolean delete = file.delete();
if (delete) {
System.out.println("目录删除成功");
} else {
System.out.println("目录删除失败");
}
} else {
System.out.println("该目录不存在............");
}
}
// /判断 目录是否存在,如果不存在则创建
@Test
public void m3() {
File file = new File("F:\\OpenCode\\IO\\FileUpload\\a");
if (file.exists()) {
System.out.println("存在"+file.getName());
} else {
//创建成功返回true,创建一级目录mkdir(),创建多级目录mkdirs()
// file.mkdir();/一级目录
file.mkdirs();//创建多级目录
}
}
}
文件输入流(字节流)
package com.Zh.InputStream;
import org.junit.Test;
import java.io.FileInputStream;
/**
* @author OZH
* @Description:FileInputStream的使用(字节输入流 文件 --- > 程序 )
* @date 2022/3/5 20:28
*/
public class FileInputStream_ {
public static void main(String[] args) {
}
/**
* 演示读取文件效果
* @throws Exception
*/
@Test
public void readFile01() throws Exception{
FileInputStream fileInputStream = new FileInputStream("F:\\OpenCode\\IO\\FileUpload\\txt.txt");
int readData = 0;
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char) readData);
}
fileInputStream.close();
}
@Test
public void readFile02() throws Exception{
FileInputStream fileInputStream = new FileInputStream("F:\\OpenCode\\IO\\FileUpload\\txt.txt");
byte[] bytes = new byte[1024];
int len = 0;
//该输入流最多读取1024字节的数据到字节数组,此方法将阻塞,直到某些输入可用
//如果返回-1,表示读取完毕
//如果读取正常,返回实际读取的字节数
while ((len = fileInputStream.read(bytes)) != -1) {
// System.out.println(len);
System.out.println(new String(bytes, 0, len));
}
fileInputStream.close();
}
}
文件输出流(字节流)
package com.Zh.OutputStream;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @author OZH
* @Description:
* @date 2022/3/5 21:02
*/
public class FileOutputStream_ {
@Test
public void FileOutputStreamDemo() throws Exception {
File file = new File("F:\\OpenCode\\IO\\FileUpload\\txt.txt");
if (!file.exists()) {
file.mkdirs();
}
// FileOutputStream fileOutputStream = new FileOutputStream(file);//覆盖的写法
FileOutputStream fileOutputStream = new FileOutputStream(file,true);//追加的写法
fileOutputStream.write("heheh".getBytes());
System.out.println("写入成功");
fileOutputStream.close();
}
}
文件复制
package com.Zh.OutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;
/**
* @author OZH
* @Description:
* @date 2022/3/5 21:20
*/
//文件拷贝
public class FileCopy {
public static void main(String[] args) throws Exception {
//思路,边使用输入流读取文件边使用输出流写进磁盘
File file = new File("F:\\OpenCode\\image\\zh.jpeg");
FileInputStream fileInputStream = new FileInputStream(file);
File file1 = new File("F:\\OpenCode\\IO\\FileUpload\\" + UUID.randomUUID().toString() + ".jpeg");
if (!file1.exists()) {
file1.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file1);
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1) {
// fileOutputStream.write(bytes);//这个方法有可能会出错
//使用这个方法,因为byte数组默认值为0,如果有14字节数据,数组长度8,那么第二次循环的时候就会有两个0在里面
fileOutputStream.write(bytes, 0, len);
}
fileInputStream.close();
fileOutputStream.close();
}
}
个人理解:读取单个字符或者比特的时候返回值是int,能转化为对应的类型的值,而读取到字符数组或者比特数组的时候,返回值是读取到的个数/字节数
FileWriter
package com.Zh.writer;
import java.io.FileWriter;
/**
* @author OZH
* @Description:
* @date 2022/3/6 12:25
*/
public class FileWriter_ {
public static void main(String[] args) throws Exception {
//创建FileWriter对象
String filePath = "F:\\OpenCode\\IO\\FileUpload\\writer.txt";
FileWriter fileWriter = new FileWriter(filePath);//默认覆盖
fileWriter.write("ZH");
//一定要close或者是flush
fileWriter.flush();
fileWriter.close();
}
}
执行close或者是flush才是真正的写入,基于FileOutputStream
节点流
直接操作数据源
包装流(处理流)(修饰器设计模式)
字节流(二进制文件(音频,视频)),字符流(字符(文本))
BufferedReader
package com.Zh.Reader;
import java.io.BufferedReader;
import java.io.FileReader;
/**
* @author OZH
* @Description:使用BufferedReader
* @date 2022/3/6 14:03
*/
public class BufferedReader_ {
public static void main(String[] args) throws Exception {
String filePath = "F:\\OpenCode\\IO\\FileUpload\\txt.txt";
//创建BufferedReader
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
//读取
String line;//按行读取,效率高
//1.line=bufferedReader.readLine(); 是按行读取文件
//2.当返回null时,表示文件读取完毕
line = bufferedReader.readLine();
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
//关闭流,这里只需要关闭bufferedReader,因为底层会自动的去关闭 节点流
//FileReader
bufferedReader.close();
}
}
BufferedWriter
package com.Zh.writer;
import java.io.BufferedWriter;
import java.io.FileWriter;
/**
* @author OZH
* @Description:
* @date 2022/3/6 14:16
*/
public class BufferedWriter_ {
public static void main(String[] args) throws Exception {
//追加或者是覆盖
FileWriter fileWriter = new FileWriter("F:\\OpenCode\\IO\\FileUpload\\bre.txt",true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (int i = 0; i < 10; i++) {
bufferedWriter.write('a');
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedWriter.close();
}
}
BufferedWriter和BufferedReader完成文本类型的拷贝
package com.Zh.writer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
/**
* @author OZH
* @Description:
* @date 2022/3/6 14:28
*/
public class BufferedCopy_ {
public static void main(String[] args) throws Exception{
//字符流不要操作二进制文件,可能会导致文件损坏
String path = "F:\\OpenCode\\IO\\FileUpload\\txt.txt";
String destFilepath = "F:\\OpenCode\\IO\\FileUpload\\copyCat.txt";//目的地
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFilepath));
String read = null;
// readLine()读取一行,没有带换行符
while ((read=bufferedReader.readLine()) != null) {
//读取到内容,写入磁盘
bufferedWriter.write(read);
bufferedWriter.newLine();//不同系统换行符号是不一样,这个会自动处理
bufferedWriter.flush();
}
bufferedWriter.close();
bufferedReader.close();
System.out.println("copy success!");
}
}
BufferedOutputStream和BufferedInputStream拷贝二进制文件夹
package com.Zh.OutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @author OZH
* @Description:演示BufferedOutputStream和BufferedInputStream使用
* @date 2022/3/6 15:02
*/
public class BufferedCopy02_ {
public static void main(String[] args) throws Exception{
//源文件
String srcFilePath = "F:\\OpenCode\\IO\\FileUpload\\315c60da-8b43-45fb-90cb-aed070c1a59e.jpeg";
String destFilePath = "F:\\OpenCode\\IO\\FileUpload\\hehe.jpeg";//
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFilePath));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFilePath));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
bufferedOutputStream.flush();
}
bufferedInputStream.close();
bufferedOutputStream.close();
System.out.println("拷贝成功");
}
}
对象流
package com.Zh.OutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* @author OZH
* @Description:ObjectOutputStream的使用,完成数据的序列化
* @date 2022/3/6 15:33
*/
public class ObjectOutStream_ {
public static void main(String[] args) throws Exception{
//序列化后,保存的文件格式,不是纯文本,而是按照它的格式来保存
String filePath = "F:\\OpenCode\\IO\\FileUpload\\data.dat";
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));
//序列化数据到F:\OpenCode\IO\FileUpload\data.dat
objectOutputStream.writeInt(100);//int -> Integer(实现了 Serializable)
objectOutputStream.writeBoolean(true);//boolean -> Boolean(实现了 Serializable)
objectOutputStream.writeChar('a');//char - >Character (实现了 Serializable)
objectOutputStream.writeDouble(9.1);
objectOutputStream.writeFloat(2.1f);
objectOutputStream.writeUTF("ozh");//字符串
//保存对象
objectOutputStream.writeObject(new Dog("狗子", 10));
objectOutputStream.close();
}
}
class Dog implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Dog() {
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
}
ObjectInputStream
package com.Zh.InputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
/**
* @author OZH
* @Description:
* @date 2022/3/6 15:51
*/
public class ObjectInputStream_ {
public static void main(String[] args) throws Exception{
//指定反序列化的文件F:\OpenCode\IO\FileUpload\data.dat
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("F:\\OpenCode\\IO\\FileUpload\\data.dat"));
//读取(反序列化)的顺序需要和你保存数据(序列化)的顺序一致
//2.否则会报异常
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());
Object dog = objectInputStream.readObject();
System.out.println("运行类型=" + dog.getClass());
System.out.println("dog信息=" + dog);
objectInputStream.close();
}
}
class Dog implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Dog() {
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
标准输入输出流
package com.Zh.standard;
import java.util.Scanner;
/**
* @author OZH
* @Description:标准输入输出流
* 输入:键盘
* 输出:显示器
* @date 2022/3/6 16:32
*/
public class InputAndOutput {
public static void main(String[] args) {
//编译类型 InputStream
//运行类型 BufferedInputStream
//表示的是标准输入 键盘
System.out.println(System.in.getClass());
//编译类型 PrintStream
//运行类型 PrintStream
//表示标准输出 显示器
System.out.println(System.out.getClass());
System.out.println("看见没?有out,这就是标准输出流");
Scanner sc = new Scanner(System.in);
System.out.println("输入点东西试试:");
String next = sc.next();
System.out.println(next);
}
}
转换流
package com.Zh.transformation;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* @author OZH
* @Description:演示使用InputStreamReader转换流解决中文乱码问题
* 将字节流转成字符流
* 将字节流 FileInputStream 转成字符流 InputStreamReader,指定编码 gbk/utf-8
* @date 2022/3/6 17:06
*/
public class InputStreamReader_ {
public static void main(String[] args) throws Exception {
String filePath = "F:\\OpenCode\\IO\\FileUpload\\txt.txt";
//1.把FileInputStream 转成 InputStreamReader
//2.指定编码GBK
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
//3.把InputStreamReader 传入 BufferedReader
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//4.读取
String read = null;
while ((read = bufferedReader.readLine()) != null) {
System.out.println(read);
}
bufferedReader.close();
}
}
package com.Zh.transformation;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
/**
* @author OZH
* @Description:OutputStreamWriter的使用
*把FileOutputStream字节流 转换成字符流
* 指定处理的编码
* @date 2022/3/6 17:42
*/
public class OutputStreamWriter_ {
public static void main(String[] args) throws Exception{
String filePath = "F:\\OpenCode\\IO\\FileUpload\\aaa.txt";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath), "GBK");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write("abc");
// outputStreamWriter.write("呵呵呵呵呵aaaaa");
// outputStreamWriter.close();
bufferedWriter.close();
}
}
打印流
PrintStream(字节流),PrintWriter(字符流)只有输出流没有输入流
package com.Zh.transformation;
import java.io.FileWriter;
import java.io.PrintWriter;
/**
* @author OZH
* @Description:PrintWriter使用方式
* @date 2022/3/6 19:43
*/
public class PrintWriter_ {
public static void main(String[] args) throws Exception{
PrintWriter printWriter = new PrintWriter(System.out);
// PrintWriter printWriter = new PrintWriter(new FileWriter("F:\\OpenCode\\IO\\FileUpload\\hehehaaa.txt"));
printWriter.print("hello");
printWriter.close();
}
}
Properties
package com.Zh.Properties;
import java.io.BufferedReader;
import java.io.FileReader;
/**
* @author OZH
* @Description:
* @date 2022/3/6 20:00
*/
public class Properties01 {
public static void main(String[] args) throws Exception{
//读取mysql.properties文件,并得到Ip,user和pwd
BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\OpenCode\\IO\\IOIO\\src\\main\\resources\\mysql.properties"));
String read = null;
while ((read = bufferedReader.readLine()) != null) {
String[] split = read.split("=");
System.out.println(split[0] + "=" + split[1]);
}
bufferedReader.close();
}
}
package com.Zh.Properties;
import java.io.FileReader;
import java.util.Properties;
/**
* @author OZH
* @Description:
* @date 2022/3/6 20:30
*/
public class Properties02 {
public static void main(String[] args) throws Exception{
//使用Properties 类来读取mysql.properties 文件
//1.创建Properties 对象
Properties properties = new Properties();
//2.加载指定1配置文件
properties.load(new FileReader("F:\\OpenCode\\IO\\IOIO\\src\\main\\resources\\mysql.properties"));
//3.把k-v显示控制台
properties.list(System.out);
//4.根据key 获取对应的值
String user = properties.getProperty("user");
System.out.println(user);
}
}
package com.Zh.Properties;
import java.io.FileWriter;
import java.util.Properties;
/**
* @author OZH
* @Description:
* @date 2022/3/6 20:36
*/
public class Properties03 {
public static void main(String[] args) throws Exception{
//使用Properties 类来创建配置文件,修改配置文件内容
Properties properties = new Properties();
//创建
//1.如果该文件没有key 就是创建
//2.如果该文件有key,就是修改
properties.setProperty("zh", "123");
//将k-v存储文件中即可
properties.store(new FileWriter("F:\\OpenCode\\IO\\IOIO\\src\\main\\resources\\mysql.properties"), null);
System.out.println("保存配置文件成功");
}
}