Java的IO流是咋玩呢?

Java的IO流是咋玩呢?,第1张

Java的IO流是咋玩呢?

什么是文件?在网络中是如何呈现的呢???

文件,对于我们来说并不陌生,在生活中是息息相关的,比如我们发的朋友圈的图片 &
再比如我们经常使用的word文档,txt文件等等;它们都统称为文件,它既可以保存视频,声音。。。

1. 探讨一下Java是如何处理文件的呢?

java对于文件处理衍生出了,自身的一套api,‘IO’流,对于文件处理使用的是“文件流”

1.1 IO流

流是一种抽象概念,它代表了数据的无结构化传递。按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列。从流中取得数据的 *** 作称为提取 *** 作,而向流中添加数据的 *** 作称为插入 *** 作。用来进行输入输出 *** 作的流就称为IO流。换句话说,IO流就是以流的方式进行输入输出

2.Java是如何处理文件的呢?


3. Java *** 作流

4.Java是实 ***

@Test
public void create01() {
String filePath = "e:\news1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}

巨有实用

//调用相应的方法,得到对应信息
System.out.println("文件名字=" + file.getName());
//getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
System.out.println("文件绝对路径=" + file.getAbsolutePath());
System.out.println("文件父级目录=" + file.getParent());
System.out.println("文件大小(字节)=" + file.length());
System.out.println("文件是否存在=" + file.exists());//T
System.out.println("是不是一个文件=" + file.isFile());//T
System.out.println("是不是一个目录=" + file.isDirectory());//F

按 *** 作数据单位不同分为:字节流(8bit)二进制文件,字符流(按字符)文本文件
按数据流的流向不同分为:输入流 & 输出流
按流的角色的不同分为 :字节流,处理流 ,包装流

5.使用字节流 (复制 copy)

public class FileCopy {
public static void main(String[] args) {
//完成 文件拷贝,将 e:\Koala.jpg 拷贝 c:\
//思路分析
//1. 创建文件的输入流 , 将文件读入到程序
//2. 创建文件的输出流, 将读取到的文件数据,写入到指定的文件. String srcFilePath = "e:\Koala.jpg";
String destFilePath = "e:\Koala3.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath);
//定义一个字节数组,提高读取效果
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1) {
//读取到后,就写入到文件 通过 fileOutputStream
//即,是一边读,一边写
fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法
}
System.out.println("拷贝 ok~");
} catch (IOException e) {
e.printStackTrace();
} finally {
韩顺平循序渐进学 Java 零基础
第 832页
try {
//关闭输入流和输出流,释放资源
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

5.1FileReader

public void readFile02() {
System.out.println("~~~readFile02 ~~~");
String filePath = "e:\story.txt";
FileReader fileReader = null;
int readLen = 0;
char[] buf = new char[8];
//1. 创建 FileReader 对象
try {
fileReader = new FileReader(filePath);
//循环读取 使用 read(buf), 返回的是实际读取到的字符数
//如果返回-1, 说明到文件结束
while ((readLen = fileReader.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

5.2 FileWriter

public class FileWriter_ {
public static void main(String[] args) {
String filePath = "e:\note.txt";
//创建 FileWriter 对象
FileWriter fileWriter = null;
char[] chars = {'a', 'b', 'c'};
try {
fileWriter = new FileWriter(filePath);//默认是覆盖写入
// 3) write(int):写入单个字符
fileWriter.write('H');
// 4) write(char[]):写入指定数组
fileWriter.write(chars);
// 5) write(char[],off,len):写入指定数组的指定部分
fileWriter.write("韩顺平教育".toCharArray(), 0, 3);
// 6) write(string):写入整个字符串
fileWriter.write(" 你好北京~");
fileWriter.write("风雨之后,定见彩虹");
// 7) write(string,off,len):写入字符串的指定部分
fileWriter.write("上海天津", 0, 2);
//在数据量大的情况下,可以使用循环 *** 作. } catch (IOException e) {
e.printStackTrace();
}

6.节点流和处理流

1.节点流可以从一个特定的数据源读写数据,FileReader & FileWriter
2.处理流(包装流)是“连接已存在”的(节点流 等)之上,可以进行包装,方便 *** 作( objectOutputStream = new ObjectInputStream(new FileInputStream(filePath));)

6.1 处理流的优势

1、性能的提高 :主要以增加缓冲的方式来提高输入输出的效率
2、 *** 作的便捷 :处理流可能提供了一系列便捷的方法来一次输入输出大批量的数据,使得更加灵活

6.2处理流案例 与上面节点流 *** 作对比

【1. 老案例 字符处理流 copy】

public static void main(String[] args) {
//老韩说明
//1. BufferedReader 和 BufferedWriter 是安装字符 *** 作
//2. 不要去 *** 作 二进制文件[声音,视频,doc, pdf ], 可能造成文件损坏
//BufferedInputStream
//BufferedOutputStream
String srcFilePath = "e:\a.java";
String destFilePath = "e:\a2.java";
// String srcFilePath = "e:45_韩顺平零基础学 Java_引出 this.avi";
// String destFilePath = "e:\a2 韩顺平.avi";
BufferedReader br = null;
BufferedWriter bw = null;
String line;
try {
br = new BufferedReader(new FileReader(srcFilePath));
bw = new BufferedWriter(new FileWriter(destFilePath));
//说明: readLine 读取一行内容,但是没有换行
while ((line = br.readLine()) != null) {
//每读取一行,就写入
bw.write(line);
//插入一个换行
bw.newline();
}
System.out.println("拷贝完毕...");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
try {
if(br != null) {
br.close();
}
if(bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

【2. 老案例 字节处理流 copy】

public static void main(String[] args) {
String srcFilePath = "e:\a.java";
String destFilePath = "e:\a3.java";
//创建 BufferedOutputStream 对象 BufferedInputStream 对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//因为 FileInputStream 是 InputStream 子类
bis = new BufferedInputStream(new FileInputStream(srcFilePath));
bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
//循环的读取文件,并写入到 destFilePath
byte[] buff = new byte[1024];
int readLen = 0;
//当返回 -1 时,就表示文件读取完毕
while ((readLen = bis.read(buff)) != -1) {
bos.write(buff, 0, readLen);
}
System.out.println("文件拷贝完毕~~~");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流 , 关闭外层的处理流即可,底层会去关闭节点流
try {
if(bis != null) {
bis.close();
}
if(bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

7.对象流

功能:提供了对基本类型或对象类型的序列化和反序列化的方法
ObjectOutputStream 提供 序列化功能
ObjectInputStream 提供 反序列化功能


ObjectOutputStream

public static void main(String[] args) throws Exception {
//序列化后,保存的文件格式,不是存文本,而是按照他的格式来保存
String filePath = "e:\data.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
//序列化数据到 e:data.dat
oos.writeInt(100);// int -> Integer (实现了 Serializable)
oos.writeBoolean(true);// boolean -> Boolean (实现了 Serializable)
oos.writeChar('a');// char -> Character (实现了 Serializable)
oos.writeDouble(9.5);// double -> Double (实现了 Serializable)
//保存一个 dog 对象
oos.writeObject(new Dog("旺财", 10, "日本", "白色"));
oos.close();
System.out.println("数据保存完毕(序列化形式)");
}

ObjectInputStream

// 1.创建流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\data.dat"));
// 2.读取, 注意顺序
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());
System.out.println(ois.readObject());
System.out.println(ois.readObject());
System.out.println(ois.readObject());
// 3.关闭
ois.close();
System.out.println("以反序列化的方式读取(恢复)ok~")

8. Properties 类

案例

public class Properties02 {
public static void main(String[] args) throws IOException {
//使用 Properties 类来读取 mysql.properties 文件
//1. 创建 Properties 对象
Properties properties = new Properties();
//2. 加载指定配置文件
properties.load(new FileReader("src\mysql.properties"));
//3. 把 k-v 显示控制台
properties.list(System.out);
//4. 根据 key 获取对应的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名=" + user);
System.out.println("密码是=" + pwd);
}
}

@lcc 学习 & play 结束,明天上班。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5697010.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存