1. IO的概念

计算机内存中的数据 <--> 硬盘里面的数据

也就是数据的落盘 以及 数据的读取

文件的操作

package com.msb.io01;

import java.io.File;
import java.io.IOException; /**
* @Auther: jack.chen
* @Date: 2023/9/20 - 09 - 20 - 20:32
* @Description: com.msb.io01
* @version: 1.0
*/
public class Test01 {
public static void main(String[] args) throws IOException {
File f = new File("d:"+ File.pathSeparator+"test.txt");
System.out.println(f.canRead());
System.out.println(f.canWrite());
System.out.println(f.getName());
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.isHidden());
System.out.println(f.length());
System.out.println(f.exists()); /* if(f.exists()){
f.delete();
}else{
f.createNewFile();
}*/
//路径相关
System.out.println(f.getAbsolutePath());
System.out.println(f.getPath());
System.out.println(f.toString()); System.out.println("--------------");
File f5 = new File("demo.txt");
if(!f5.exists()){
f5.createNewFile();
}
File f6 = new File("a/b/c/demo.txt");
if(!f6.exists()){
f6.createNewFile();
}
System.out.println(f6.getAbsolutePath());//绝对路径
System.out.println(f6.getPath());//相对路径
}
}

目录的操作

package com.msb.io01;

import java.io.File;

/**
* @Auther: jack.chen
* @Date: 2023/9/20 - 09 - 20 - 20:42
* @Description: com.msb.io01
* @version: 1.0
*/
public class Test02 {
public static void main(String[] args) {
File f = new File("C:\\Users\\chenw\\IdeaProjects\\Testuntitled");
System.out.println("文件是否可读:"+f.canRead());
System.out.println("文件是否可写:"+f.canWrite());
System.out.println("文件的名字:"+f.getName());
System.out.println("上级目录:"+f.getParent());
System.out.println("是否是一个目录:"+f.isDirectory());
System.out.println("是否是一个文件:"+f.isFile());
System.out.println("是否隐藏:"+f.isHidden());
System.out.println("文件的大小:"+f.length());
System.out.println("是否存在:"+f.exists());
System.out.println("绝对路径:"+f.getAbsolutePath());
System.out.println("相对路径:"+f.getPath());
System.out.println("toString:"+f.toString()); String[] list = f.list();//字符串
for (String s : list) {
System.out.println(s);
} File[] fs = f.listFiles();//file对象
for (File file : fs) {
System.out.println(file);
} }
}

IO流的分类



2. 一个一个字符 完成文件的复制

package com.msb.io02;

import java.io.File;
import java.io.FileReader;
import java.io.IOException; /**
* @Auther: jack.chen
* @Date: 2023/9/20 - 09 - 20 - 20:53
* @Description: com.msb.io02
* @version: 1.0
*/
public class Test01 {
public static void main(String[] args) throws IOException {
File f = new File("d:/test.txt");
FileReader fr = new FileReader(f);
int n = fr.read();
// while (n!=-1){
// System.out.println(n);//单个字符的字节码打印
// n = fr.read();
// }
while (n!=-1){
System.out.println((char) n);//单个字符的打印
n = fr.read();
} fr.close();
}
}
package com.msb.io02;

import java.io.File;
import java.io.FileReader;
import java.io.IOException; /**
* @Auther: jack.chen
* @Date: 2023/9/20 - 09 - 20 - 20:53
* @Description: com.msb.io02
* @version: 1.0
*/
public class Test02 {
public static void main(String[] args) throws IOException {
File f = new File("d:/test.txt");
FileReader fr = new FileReader(f); // 缓冲数组
char[] ch = new char[5];
int len = fr.read(ch);//一次接收到的实际个数 不满足5的情况下 while (len!=-1){
// // 方式1 迭代输出
// for (int i = 0; i < len; i++) {//根据实际长度迭代
// System.out.println(ch[i]);
// }
//方式2 char列表转String
String str = new String(ch, 0, len);
System.out.print(str); len = fr.read(ch);
} fr.close();
}
}

输出内容到文件

package com.msb.io02;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException; /**
* @Auther: jack.chen
* @Date: 2023/9/20 - 09 - 20 - 21:06
* @Description: com.msb.io02
* @version: 1.0
*/
public class Test03 {
public static void main(String[] args) throws IOException {
File f = new File("d:demo.txt"); FileWriter fw = new FileWriter(f); String str = "hello 美女";
for (int i = 0; i < str.length(); i++) {
fw.write(str.charAt(i));
} fw.close(); }
}

文件复制

package com.msb.io02;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; /**
* @Auther: jack.chen
* @Date: 2023/9/20 - 09 - 20 - 21:09
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo04 {
public static void main(String[] args) throws IOException {
File f1 = new File("d:test.txt");
File f2 = new File("d:demo.txt"); FileReader fr = new FileReader(f1);
FileWriter fw = new FileWriter(f2); // 方法一
// int n = fr.read();
// while (n!=-1){
// fw.write(n);
// n = fr.read();
// } // 方法二
// char[] ch = new char[5];
// int len = fr.read(ch);
// while (len!=-1){
// fw.write(ch, 0, len);//实际有效长度
// len = fr.read(ch);//注意这里也要传入ch
// }
// 方法三
char[] ch = new char[5];
int len = fr.read(ch);
while (len!=-1){
String str = new String(ch, 0, len);
fw.write(str);
len = fr.read(ch);
} fw.close();
fr.close(); }
}

用字节流去处理字符流

package com.msb.io02;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; /**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 19:45
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo05 {
public static void main(String[] args) throws IOException {
//利用字节流 读取文本
File f = new File("d:\\test.txt");
FileInputStream fis = new FileInputStream(f); /*
注意1:
文件是utf-8存储的 一个英文字符占用1字节 一个中文字符占用3个字节 注意2
文本文件建议用字符流去处理 */
int n = fis.read();
while (n!=-1){
System.out.println(n);
n = fis.read();
} fis.close(); }
}



警告:不要用字符流去处理字节流

字符流:.txt .java .c .cpp 这些都是存文本的文件

字节流 非文本的文件:.jpg .mp3 .mp4 ...

3. 字节流

beach.jpg

package com.msb.io02;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; /**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 19:56
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo06 {
public static void main(String[] args) throws IOException {
File f = new File("d:\\beach.jpg");
FileInputStream fis = new FileInputStream(f); int count = 0; int n = fis.read();
while (n!=-1){
count++;
System.out.println(n);
n = fis.read();
}
System.out.println("count="+count);
fis.close();
}
}



另一种方式读取

package com.msb.io02;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; /**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 19:56
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo07 {
public static void main(String[] args) throws IOException {
File f = new File("d:\\beach.jpg");
FileInputStream fis = new FileInputStream(f); // 利用缓存数组
byte[] b = new byte[1024*2]; // read
int len = fis.read(b); // 读的时候需要传入b
while (len!=-1){ for (int i = 0; i < len; i++) {
System.out.println(b[i]);
} len = fis.read(b);
}
fis.close();
}
}

图片的复制

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 20:04
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo08 { public static void main(String[] args) throws IOException {
File f1 = new File("d:\\beach.jpg");
File f2 = new File("d:\\beach_copy.jpg"); FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2); // copy
int n = fis.read(); while (n!=-1){
fos.write(n);
n = fis.read();
} fos.close();
fis.close(); }
}

再套一层 缓冲字节流

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 20:04
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo09 { public static void main(String[] args) throws IOException {
File f1 = new File("d:\\beach.jpg");
File f2 = new File("d:\\beach_copy.jpg"); FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2); BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//复制
byte [] b = new byte[1024*2];
int len = bis.read(b);
while (len!=-1){
bos.write(b, 0, len);//读到多少写多少 长度是len
len = bis.read(b);
} bos.close();
bis.close();
fos.close();
fis.close(); }
}

为什么要有缓冲字节流 减少与磁盘的访问次数

同样的也有对应的处理 字符流的 缓冲 方式

BufferedReader BufferedWriter

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 20:42
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo10 {
public static void main(String[] args) throws IOException {
File f1 = new File("d:\\test.txt");
File f2 = new File("d:\\test_copy.txt"); FileReader fr = new FileReader(f1);
FileWriter fw = new FileWriter(f2); BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw); /*
int n = br.read();
while (n!=-1){
bw.write(n);
n=br.read();
}
*/ /*
char[] ch = new char[30];
int len = br.read(ch);
while (len!=-1){
bw.write(ch, 0, len);
len = br.read();
} */ String str = br.readLine();
while (str!=null){
bw.write(str);
bw.newLine();
str = br.readLine();
} bw.close();
br.close(); fw.close();
fr.close(); }
}

4. 转换字节流

InputStreamReader

OutputStreamWriter

文本的底层存储是二进制 可以用 字节流 根据文件的编码格式 转换成 字符流

编码格式 就很重了 否则会转换失败 得到字符出现乱码

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 20:54
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo11 { public static void main(String[] args) throws IOException {
File f = new File("d:\\test.txt");
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr= new InputStreamReader(fis, "utf-8");//默认就是utf-8 可以不写 char[] ch = new char[20];
int len = isr.read(ch);
while (len!=-1){
System.out.println(new String(ch, 0, len));
len = isr.read(ch);
} isr.close(); }
}

完成文本的复制

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 21:07
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo12 {
public static void main(String[] args) throws IOException {
File f1 = new File("d:\\test.txt");
File f2 = new File("d:\\test_copy.txt"); FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2); InputStreamReader isr= new InputStreamReader(fis, "utf-8");//默认就是utf-8 可以不写
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8"); char[] ch = new char[20];
int len = isr.read(ch);
while (len!=-1){
osw.write(ch, 0, len);
len = isr.read(ch);
} osw.close();
isr.close();
}
}

5. System.in

package com.msb.io02;

import java.io.IOException;
import java.io.InputStream; /**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 21:14
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo3 { public static void main(String[] args) throws IOException {
InputStream in = System.in;//系统的标准输入 从键盘输入 int i = in.read();//阻塞方法 等待从键盘输入 得到输入的 ascii码值
System.out.println(i);
}
}

直接读取文本的内容并打印

package com.msb.io02;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner; /**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 21:14
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo3 { public static void main(String[] args) throws IOException {
InputStream in = System.in;//系统的标准输入 从键盘输入 /*
int i = in.read();//阻塞方法 等待从键盘输入 得到输入的 ascii码值
System.out.println(i);
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
System.out.println(i); */ Scanner sc = new Scanner(new FileInputStream(new File("d:\\demo.txt")));
while (sc.hasNext()){
System.out.println(sc.next());
} }
}

练习:将键盘的输入内容 写到文本中

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 21:28
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo13 {
public static void main(String[] args) throws IOException {
InputStream is = System.in;//字节流
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); File f = new File("d:\\demo1.txt");
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw); String s = br.readLine();
while(!s.equals("exit")){
bw.write(s);
bw.newLine();
s = br.readLine();
} bw.close();
br.close();
}
}

7.基本数据类型的数据

DataInputStream:将文件中存储的基本数据类型和字符串 写入 内存的变量中

DataOutputStream: 将内存中的基本数据类型和字符串的变量 写出 文件中

将基本数据类型写到文本中

DataOutputStream

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 21:58
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo14 {
public static void main(String[] args) throws IOException {
File f = new File("d:\\demo02.txt");
FileOutputStream fos = new FileOutputStream(f);
DataOutputStream dos = new DataOutputStream(fos); dos.writeUTF("你好");
dos.writeBoolean(false);
dos.writeInt(10086); dos.close(); }
}

同样的方式读入

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 22:03
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo15 {
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(new File("d:\\demo02.txt"))); System.out.println(dis.readUTF());
System.out.println(dis.readBoolean());
System.out.println(dis.readInt()); }
}

8. object的处理

ObjectInputStream,ObjectInputStream

object 如何冷冻起来 又如何解冻

序列化 -->冷冻

以及反序列化 -->解冻

字符串 序列化--冷冻

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 22:08
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo16 {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:\\demo03.txt")));
oos.writeObject("你好 china");
oos.close();
}
}

字符串反序列化--解冻

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 22:11
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo7 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:\\demo03.txt")));
String s = (String)(ois.readObject());
System.out.println(s); ois.close(); }
}

对象的序列化

package com.msb.io02;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 22:13
* @Description: com.msb.io02
* @version: 1.0
*/
public class Person {
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 Person() {
} public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 22:14
* @Description: com.msb.io02
* @version: 1.0
*/
public class Test05 {
public static void main(String[] args) throws IOException {
Person p = new Person("lili", 18);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:\\Demo4.txt")));
oos.writeObject(p);
oos.close(); }
}

报错了什么原因?

对象需要序列化的话 必须实现一个接口 Serializable (可以序列化的) 例如 手机是 可以拍照的



如何反序列化Person object

package com.msb.io02;

import java.io.*;

/**
* @Auther: jack.chen
* @Date: 2023/9/21 - 09 - 21 - 22:20
* @Description: com.msb.io02
* @version: 1.0
*/
public class Demo17 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:\\Demo4.txt"))); Person p = (Person)(ois.readObject());
System.out.println(p.toString()); ois.close(); }
}

什么是serialVersionUID?

Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体类的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。(InvalidCastException)

static,transient修饰的属性 不可以被序列化

java基础-IO流-day13的更多相关文章

  1. Java基础IO流(二)字节流小案例

    JAVA基础IO流(一)https://www.cnblogs.com/deepSleeping/p/9693601.html ①读取指定文件内容,按照16进制输出到控制台 其中,Integer.to ...

  2. Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)

    Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...

  3. Java基础-IO流对象之随机访问文件(RandomAccessFile)

    Java基础-IO流对象之随机访问文件(RandomAccessFile) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.RandomAccessFile简介 此类的实例支持对 ...

  4. Java基础-IO流对象之内存操作流(ByteArrayOutputStream与ByteArrayInputStream)

    Java基础-IO流对象之内存操作流(ByteArrayOutputStream与ByteArrayInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.内存 ...

  5. Java基础-IO流对象之数据流(DataOutputStream与DataInputStream)

    Java基础-IO流对象之数据流(DataOutputStream与DataInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据流特点 操作基本数据类型 ...

  6. Java基础-IO流对象之打印流(PrintStream与PrintWriter)

    Java基础-IO流对象之打印流(PrintStream与PrintWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.打印流的特性 打印对象有两个,即字节打印流(P ...

  7. Java基础-IO流对象之序列化(ObjectOutputStream)与反序列化(ObjectInputStream)

    Java基础-IO流对象之序列化(ObjectOutputStream)与反序列化(ObjectInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.对象的序 ...

  8. java基础-IO流对象之Properties集合

    java基础-IO流对象之Properties集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Properties集合的特点 Properties类表示了一个持久的属性集. ...

  9. Java基础-IO流对象之字符缓冲流(BufferedWriter与BufferedReader)

    Java基础-IO流对象之字符缓冲流(BufferedWriter与BufferedReader) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符缓冲流 字符缓冲流根据流的 ...

  10. Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)

    Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我们学习字 ...

随机推荐

  1. org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException报错问题

    这个原因是 高版本SpringBoot整合swagger 造成的 我的项目是2.7.8 swagger版本是3.0.0 就会出现上面的报错 解决方式: 1.配置WebMvcConfigurer.jav ...

  2. C++学习笔记二:变量与数据类型(整型)

    1.int(整型数据): 1.1 进制的表示:十进制,八进制,16进制,二进制 int number1 = 15; // Decimal int number2 = 017; // Octal int ...

  3. bash shell笔记整理——ls命令

    语法: ls [选项] [文件 或 目录] 选项 使用说明 –a 显示指定目录下的所有文件,包括隐藏文件. -A 显示除了.和..的外的所有文件. -l 显示详细的文件信息. -d 如果是目录,只显示 ...

  4. Scrapy-settings.py常规配置

    # Scrapy settings for scrapy_demo project # # For simplicity, this file contains only settings consi ...

  5. Python——第二章:字典(dictionary)以及 添、删、改、查

    首先, 字典是以键值对的形式进行存储数据的,必须有键[key],有值[value] 字典的表示方式: {key:value, key2:value, key3:value} 举例: dic = {&q ...

  6. 【scikit-learn基础】--『监督学习』之 贝叶斯分类

    贝叶斯分类是一种统计学分类方法,基于贝叶斯定理,对给定的数据集进行分类.它的历史可以追溯到18世纪,当时英国统计学家托马斯·贝叶斯发展了贝叶斯定理,这个定理为统计决策提供了理论基础. 不过,贝叶斯分类 ...

  7. JVM优化:如何进行JVM调优,JVM调优参数有哪些

    Java虚拟机(JVM)是Java应用运行的核心环境.JVM的性能优化对于提高应用性能.减少资源消耗和提升系统稳定性至关重要.本文将深入探讨JVM的调优方法和相关参数,以帮助开发者和系统管理员有效地优 ...

  8. 斯坦福 UE4 C++ ActionRoguelike游戏实例教程 04.角色感知组件PawnSensingComponent和更平滑的转身

    斯坦福课程 UE4 C++ ActionRoguelike游戏实例教程 0.绪论 概述 本文章对应课程第十一章 43.44节.本文讲述PawnSensingComponent中的视觉感知的使用,以及对 ...

  9. nacos-config配置中心

    依赖 <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-clou ...

  10. C++篇:第十章_命名空间_知识点大全

    C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 十.命名空间 命名空间可以在全局作用域或其他命名空间内部定义,但不能在函数.结构 ...