让你的数据和对象有源有出路,一文打尽,Java常用IO流处理流(处理字节流文件流)缓冲流、转换流

public class Test {

public static void main(String[] args) {

InputStreamReader isr = null;

OutputStreamWriter osw = null;

try {

//1、指明输入输出文件

File inFile = new File(“IO\hi.txt”);

File outFile = new File(“IO\hello.txt”);

//2、提供字节节点流

FileInputStream fis = new FileInputStream(inFile);

FileOutputStream fos = new FileOutputStream(outFile);

//3、提供转换流

isr = new InputStreamReader(fis,“gbk”);

osw = new OutputStreamWriter(fos,“utf-8”);

//4、读写操作

char[] chars = new char[10];

int len = isr.read(chars);

while (len != -1){

osw.write(chars,0,len);

len = isr.read(chars);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

//5、关闭外层流

try {

if (osw != null) osw.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

if (isr != null) isr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}


标准输入输出流


System.inSystem.out分别代表了系统标准的输入和输出设备

默认输入设备是:键盘,输出设备是:显示器(控制台)

System.in的类型是InputStream

System.out的类型是PrintStream,其是OutputStream的子类

重定向:通过System类的setIn()setOut()方法对默认设备进行改变:

public static void setIn(InputStream in)

public static void setOut(PrintStream out)

在这里插入图片描述

import java.io.*;

/**

  • @Author: Yeman

  • @Date: 2021-09-26-20:13

  • @Description:

*/

public class Test {

public static void main(String[] args) {

BufferedReader bis = null;

try {

//1、提供转换流(System.in是字节流,将其转换为字符流)

System.out.println(“请输入信息(退出输入e或exit):”);

InputStreamReader isr = new InputStreamReader(System.in);

//2、提供缓冲流将输入的一行读取

bis = new BufferedReader(isr);

//3、读操作

String s = null;

while ((s = bis.readLine()) != null){

if (“e”.equalsIgnoreCase(s) || “exit”.equalsIgnoreCase(s)){

System.out.println(“程序结束,退出程序!”);

break;

}

System.out.println(“==” + s.toUpperCase());

System.out.println(“继续输入(退出输入e或exit):”);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

//4、关闭外层流

try {

if (bis != null) bis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}


打印流


实现将基本数据类型的数据格式转化为字符串输出。

打印流:PrintStreamPrintWriter

提供了一系列重载的print()println()方法,用于多种数据类型的输出:

PrintStream和PrintWriter的输出不会抛出IOException异常,

PrintStream和PrintWriter有自动flush功能,

PrintStream打印的所有字符都使用平台的默认字符编码转换为字节

在需要写入字符而不是写入字节的情况下,应该使用PrintWriter类。

System.out返回的是PrintStream的实例。

常与System.out搭配使用,可以不在控制台输出,而是输出到指定位置:

import java.io.*;

/**

  • @Author: Yeman

  • @Date: 2021-09-26-20:13

  • @Description:

*/

public class Test {

public static void main(String[] args) {

PrintStream ps = null;

try {

FileOutputStream fos = new FileOutputStream(new File(“IO\text.txt”));

// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 ‘\n’ 时都会刷新输出缓冲区)

ps = new PrintStream(fos, true);

if (ps != null) {// 把标准输出流(控制台输出)改成文件

System.setOut(ps);

}

for (int i = 0; i <= 255; i++) { // 输出ASCII字符

System.out.print((char) i);

if (i % 50 == 0) { // 每50个数据一行

System.out.println(); // 换行

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

if (ps != null) {

ps.close();

}

}

}

}


数据流


为了方便地操作Java语言的基本数据类型和String的数据,可以使用数据流。

数据流有两个类:(用于读取和写出基本数据类型、String类的数据)

DataInputStreamDataOutputStream

分别“套接”在 InputStream 和 OutputStream 子类的流上。

在这里插入图片描述

import java.io.*;

/**

  • @Author: Yeman

  • @Date: 2021-09-26-20:13

  • @Description:

*/

public class Test {

public static void main(String[] args) {

//写

DataOutputStream dos = null;

try {

dos = new DataOutputStream(new FileOutputStream(“IO\test.txt”));

dos.writeUTF(“叶绿体”);

dos.writeInt(22);

dos.writeBoolean(true);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (dos != null) dos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

//读

DataInputStream dis = null;

try {

dis = new DataInputStream(new FileInputStream(“IO\test.txt”));

//注意读的顺序要和写的顺序一样

String name = dis.readUTF();

int age = dis.readInt();

boolean isMan = dis.readBoolean();

System.out.println(name + age + isMan);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (dis != null) dis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}


对象流


ObjectInputStreamOjbectOutputSteam

用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。

序列化:用ObjectOutputStream类保存基本类型数据或对象的机制

反序列化:用ObjectInputStream类读取基本类型数据或对象的机制

ObjectOutputStreamObjectInputStream不能序列化statictransient修饰的成员变量。

实现Serializable或者Externalizable两个接口之一的类的对象才可序列化,关于对象序列化详见:序列化

可序列化对象:

import java.io.Serializable;

/**

  • @Author: Yeman

  • @Date: 2021-09-27-8:27

  • @Description:

*/

class pet implements Serializable {

public static final long serialVersionUID = 999794470754667999L;

private String name;

public pet(String name) {

this.name = name;

}

@Override

public String toString() {

return “pet{” +

“name='” + name + ‘’’ +

‘}’;

}

}

public class Person implements Serializable {

public static final long serialVersionUID = 6849794470754667999L;

private String name;

private int age;

private pet pet;

public Person(String name, int age, pet pet) {

this.name = name;

this.age = age;

this.pet = pet;

}

@Override

public String toString() {

return “Person{” +

“name='” + name + ‘’’ +

“, age=” + age +

“, pet=” + pet +

‘}’;

}

}

序列化(ObjectOutputStream):

import java.io.*;

/**

  • @Author: Yeman

  • @Date: 2021-09-26-20:13

  • @Description:

*/

public class Test {

public static void main(String[] args) {

ObjectOutputStream oos = null;

try {

oos = new ObjectOutputStream(new FileOutputStream(“IO\test.txt”));

oos.writeUTF(new String(“你好世界!”));

oos.flush();

oos.writeObject(new Person(“Lily”,20,new pet(“Xinxin”)));

oos.flush();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (oos != null) oos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

反序列化(ObjectInputStream):

import java.io.*;

/**

  • @Author: Yeman

  • @Date: 2021-09-26-20:13

  • @Description:

*/

public class Test {

public static void main(String[] args) {

ObjectInputStream ois = null;

try {

ois = new ObjectInputStream(new FileInputStream(“IO\test.txt”));

String s = ois.readUTF();

Person o = (Person) ois.readObject();

System.out.println(o.toString());

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} finally {

if (ois != null) {

try {

ois.close();

那么如何才能正确的掌握Redis呢?

为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题

  • 并发编程面试题汇总

  • JVM面试题汇总

  • Netty常被问到的那些面试题汇总

  • Tomcat面试题整理汇总

  • Mysql面试题汇总

  • Spring源码深度解析

  • Mybatis常见面试题汇总

  • Nginx那些面试题汇总

  • Zookeeper面试题汇总

  • RabbitMQ常见面试题汇总

JVM常频面试:

Redis高频面试笔记:基础+缓存雪崩+哨兵+集群+Reids场景设计

Mysql面试题汇总(一)

Redis高频面试笔记:基础+缓存雪崩+哨兵+集群+Reids场景设计

Mysql面试题汇总(二)

Redis高频面试笔记:基础+缓存雪崩+哨兵+集群+Reids场景设计

Redis常见面试题汇总(300+题)

Redis高频面试笔记:基础+缓存雪崩+哨兵+集群+Reids场景设计

ing());

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} finally {

if (ois != null) {

try {

ois.close();

那么如何才能正确的掌握Redis呢?

为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题

  • 并发编程面试题汇总

  • JVM面试题汇总

  • Netty常被问到的那些面试题汇总

  • Tomcat面试题整理汇总

  • Mysql面试题汇总

  • Spring源码深度解析

  • Mybatis常见面试题汇总

  • Nginx那些面试题汇总

  • Zookeeper面试题汇总

  • RabbitMQ常见面试题汇总

JVM常频面试:

[外链图片转存中…(img-uQp3j27a-1714798508454)]

Mysql面试题汇总(一)

[外链图片转存中…(img-hpDdoRCP-1714798508455)]

Mysql面试题汇总(二)

[外链图片转存中…(img-GZAlS9ju-1714798508455)]

Redis常见面试题汇总(300+题)

[外链图片转存中…(img-ErYZZl5p-1714798508456)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 20
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值