Java IO操作
程序员文章站
2022-06-27 16:19:12
缓冲流(重点)缓冲流就是先把数据缓冲到内存中,在内存中做IO操作,基于内存的IO操作大概能比基于硬盘的IO操作快75000多倍。缓冲字节流public class Test { public static void main(String[] args) { Test.BufferedInputStream("D:\\demo\\test11.txt"); Test.BufferedOutputStream("一个小demo!!!", "D:\\demo\\a...
缓冲流(重点)
缓冲流就是先把数据缓冲到内存中,在内存中做IO操作,基于内存的IO操作大概能比基于硬盘的IO操作快75000多倍。
缓冲字节流
public class Test {
public static void main(String[] args) {
Test.BufferedInputStream("D:\\demo\\test11.txt");
Test.BufferedOutputStream("一个小demo!!!", "D:\\demo\\aa\\bb\\text11.txt");
Test.CopyFiles("D:\\demo\\aa\\bb\\text11.txt", "D:\\demo\\aa\\bb\\cc\\dd\\text11.txt");
}
/**
* 缓冲字节输入流
* BufferedInputStream
*/
public static void BufferedInputStream(String inpath) {
try {
BufferedInputStream br = new BufferedInputStream(new FileInputStream(inpath));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = br.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, len));
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 缓冲字节输出流
* BufferedOutputStream
* @param text
* @param outpath
*/
public static void BufferedOutputStream(String text, String outpath) {
try {
BufferedOutputStream bf = new BufferedOutputStream(new FileOutputStream(outpath));
byte[] bytes = text.getBytes();
bf.write(bytes);
bf.flush();
bf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 缓冲流实现文件的复制
* @param inpath
* @param outpath
*/
private static void CopyFiles(String inpath, String outpath) {
try {
BufferedInputStream bfin = new BufferedInputStream(new FileInputStream(inpath));
BufferedOutputStream bfout = new BufferedOutputStream(new FileOutputStream(outpath));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bfin.read(bytes)) != -1) {
bfout.write(bytes, 0, len);
}
bfout.flush();
bfout.close();
bfin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
缓冲字符流
public class Test1 {
public static void main(String[] args) {
Test1.BufferedReader("D:\\demo\\test11.txt");
Test1.BufferedWriter("**********Hello World!!!***********", "D:\\demo\\aa\\bb\\text11.txt");
Test1.CopyFiles("D:\\demo\\aa\\bb\\text11.txt", "D:\\demo\\test11.txt");
}
/**
* 缓冲字符输入流
* BufferedReader
* @param inpath
*/
public static void BufferedReader(String inpath) {
try {
BufferedReader bfr = new BufferedReader(new FileReader(inpath));
char[] chars = new char[1024];
int len = 0;
while ((len = bfr.read(chars)) != -1) {
System.out.println(new String(chars, 0, len));
}
bfr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 缓冲字符输出流
* BufferedWriter
* @param text
* @param outpath
*/
public static void BufferedWriter(String text, String outpath) {
try {
BufferedWriter bfw = new BufferedWriter(new FileWriter(outpath));
bfw.write(text);
bfw.flush();
bfw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 用缓冲字符流拷贝文件
* @param inpath
* @param outpath
*/
public static void CopyFiles(String inpath, String outpath) {
try {
BufferedReader bfr = new BufferedReader(new FileReader(inpath));
BufferedWriter bfw = new BufferedWriter(new FileWriter(outpath));
char[] chars = new char[1024];
int len = 0;
while ((len = bfr.read(chars)) != -1) {
bfw.write(chars, 0, len);
}
bfw.flush();
bfw.close();
bfr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:缓冲流是把数据缓冲到内存中
转换流
所有的文件都有编码格式,对于我们来说,txt和java文件一般来讲有三种编码
- ISO8856-1, 西欧编码,是纯粹的英文编码,不适应汉字
- GBK和UTF-8, 这两种编码适用于中文和英文,一般使用UTF-8这种编码
/**
* 转换流
* 可以把字节流转换为字符流
* 当字节流中的数据都是字符的时候,使用转换流处理效率更高
*/
public class Test2 {
public static void main(String[] args) {
Test2.InputStreamReader("D:\\demo\\demo.txt");
Test2.OutputStreamWriter("你好世界!!", "D:\\demo\\test33.txt");
}
/**
* 转换输入流
* InputStreamReader
* @param inpath
*/
public static void InputStreamReader(String inpath) {
try {
//把字节流转换为字符流
InputStreamReader isr = new InputStreamReader(new FileInputStream(inpath), "UTF-8"); //参数1是字节流,参数2是编码
char[] chars = new char[1024];
int len = 0;
while ((len = isr.read(chars)) != -1) {
System.out.println(new String(chars, 0, len));
}
isr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 转换输出流
* OutputStreamWriter
* @param text
* @param outpath
*/
public static void OutputStreamWriter(String text, String outpath) {
try {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(outpath), "UTF-8");
osw.write(text);
osw.flush();
osw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:在转换字符流的时候,设置的字符集编码要与读取文件的编码一致。
标准输入输出流
public class Test3 {
public static void main(String[] args) {
try {
Test3.SystemIn();
} catch (Exception e) {
e.printStackTrace();
}
try {
Test3.SystemToFiles();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 标准输入流
* @throws Exception
*/
public static void SystemIn() throws Exception {
//创建一个接收键盘输入数据的输入流
InputStreamReader isr = new InputStreamReader(System.in);
//把输入流放到缓冲流里
BufferedReader br = new BufferedReader(isr);
//定义一个临时接收数据的字符串
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
isr.close();
}
/**
* 标准输出流
* @throws Exception
*/
public static void SystemToFiles() throws Exception {
//把输入流放到缓冲流里
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\demo\\demo.txt"));
String str = "";
while ((str = br.readLine()) != null) {
if (str.equals("over")) break;
bw.write(str);
}
bw.flush();
bw.close();
br.close();
}
}
对象流(难点)
假如我有一个Person对象,有如下两个需求:
- 1.把这个对象存到电脑的硬盘上,硬盘存储的基础是什么?是二进制。那就需要把对象转化为一个二进制的字节流,把这个流保存到电脑上,要使用这个对象,得把流转化为对象再使用。
- 2.把这个对象通过网络传到另一台电脑上,网络得通信基础是什么?也是二进制。也就是需要把一个对象转化为二进制得数据流,把这个流通过网络进行传输,再接收者如果要使用对象得先把流转化为对象。
正是因为保存对象到硬盘(对象的持久化)和对象的网络传输,需要做这两件事,就产生了对象的的输入与输出流。
对象写入IO流的过程被称为对象的序列化(Serialize)
从IO流恢复该java对象的过程称为对象的反序列化(Deserialize)
序列化与反序列化都针对的是对象的各种属性,不包括类的属性
/**
* 可以序列化与反序列化的对象
*/
public class Person implements Serializable {
/**
* 一个表示序列化版本标识符的静态变量
* 用来表明类的不同版本间的兼容性
*/
private static final long serialVersionUID = 1L;
public String name;
public int age;
}
/**
* 序列化与反序列化
* 注意:对象的序列化与反序列化使用的类要严格一致,包名,类名,类结构等等所有都要一致。
*/
public class Test4 {
public static void main(String[] args) {
try {
Test4.serialize("D:\\demo\\demo.txt");
} catch (Exception e) {
e.printStackTrace();
}
try {
Test4.deserialize("D:\\demo\\demo.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 对象的序列化
* @param outpath
* @throws Exception
*/
public static void serialize(String outpath) throws Exception{
//定义对象的输出流,把对象的序列化之后的流放到指定的文件中
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(outpath));
Person p = new Person();
p.name = "张三";
p.age = 23;
out.writeObject(p);
out.flush(); //刷写数据到硬盘
out.close();
}
/**
* 对象的反序列化
* @param input
* @throws Exception
*/
public static void deserialize(String input) throws Exception{
//创建对象的输入流,从指定的文件中把对象的序列化后的流读取出来
ObjectInputStream in = new ObjectInputStream(new FileInputStream(input));
Object o = in.readObject();
Person p = (Person) o;
System.out.println(p.name + "---------" + p.age);
in.close();
}
}
随机存取流
/**
* 文件的随机读写
*/
public class Test5 {
public static void main(String[] args) {
// try {
// Test5.RandomAccessFileRead();
// } catch (Exception e) {
// e.printStackTrace();
// }
try {
Test5.RandomAccessFileWrite();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 随机读文件
*/
public static void RandomAccessFileRead() throws Exception{
RandomAccessFile ra = new RandomAccessFile("D:\\demo\\demo1.txt", "r");
ra.seek(8); //通过设置读取文件的起始点,来达到从文件的任意位置读取
byte[] bytes = new byte[1024];
int len = 0;
while ((len = ra.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, len));
}
ra.close();
}
/**
* 随机写文件
*/
public static void RandomAccessFileWrite() throws Exception{
RandomAccessFile ra = new RandomAccessFile("D:\\demo\\demo1.txt", "rw");
// ra.seek(0); //设置写的起始点,0代表从开头写
ra.seek(ra.length()); //ra.length()代表从文件的结尾写,也就是文件的追加。
ra.write("你好".getBytes());
ra.close();
}
}
本文地址:https://blog.csdn.net/qq_26498311/article/details/109894402
上一篇: 低价主板都是垃圾吗 听买家讲述使用心得