IO流(2)

## 系统流

| System.out | 标准输出流 |
| ———- | ———- |
| System.in | 标准输入流 |
| System.err | 标准错误流 |

“`java
public static void main(String[] args) {
System.out.println();
int i = System.in.read();
System.out.println(i);

System.err.println(i);
}
“`

### 案例:用已知的流从控制台获取一行数据

“`java
package cn.javasm.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

public class TestDemo {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
String next = scanner.next();
System.out.println(next);
scanner.close();
Scanner scanner1 = new Scanner(System.in);
String next1 = scanner1.next();
System.out.println(next1);
scanner1.close();
}

public static void method() {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String s = bufferedReader.readLine();
System.out.println(s);
bufferedReader.close();
}
}
“`

## 打印流

打印流只有输出流,没有输入流

– PrintWriter 字符输出流
– PrintStream 字节输出流

“`java
import java.io.PrintStream;
import java.io.PrintWriter;

public static void main(String[] args) {
// method1();
// method2();
}

public static void method1() {
//打印流的字节输出流
PrintWriter printWriter = new PrintWriter(“test1.txt”);
printWriter.write(“这是写入测试”);
printWriter.println(“打印流”);
//关流
printWriter.close();
}

public static void method2() {
PrintStream printStream = new PrintStream(“test2.txt”);
printStream.println(“IO流”);
printStream.write(“abc”.getBytes());
//关流
printStream.close();
}
“`

## 合并流

把多个流合并成一个流

合并的流要求格式一致,编码也需要一致

### 合并三个流并输出

“`java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Vector;

public class TestDemo {
public static void main(String[] args) {
Vector vector = new Vector<>();
vector.add(new FileInputStream(“D:\\aaa.txt”));
vector.add(new FileInputStream(“D:\\bbb.txt”));
vector.add(new FileInputStream(“D:\\ccc.txt”));

//创建合并流
SequenceInputStream sequenceInputStream = new SequenceInputStream(vector.elements());

//创建字节输出流 fileOutputStream顺序访问流
FileOutputStream fileOutputStream = new FileOutputStream(“D:\\ddd.txt”);

//读取到的实际个数
int len;
while ((sequenceInputStream.read(bys)) != -1) {
fileOutputStream.write(bys, 0, len);
}

//关闭流
fileOutputStream.close();
sequenceInputStream.close();
}
}
“`

## 随机获取流

随机获取流RandomAccessFile把文件看作一个字节数组,其中有一个索引在指定读取到的位置

随机获取流是一个双向流,可以读也可以写

mode:模式

– r 读
– rw 读写
– rws 读写并写入到硬盘
– rwd 读写并立即写入到硬盘

“`java
import java.io.IOException;
import java.io.RandomAccessFile;

public static void main(String[] args) throws IOException {

//创建随机获取流
//a.txt中的数据是abcdefg
RandomAccessFile randomAccessFile = new RandomAccessFile(“a.txt”, “rw”);

//写入123
//结果是123defg
randomAccessFile.write(“123”.getBytes());

//读取数据 结果是d
System.out.println((char) randomAccessFile.read());

//设置游标的位置
randomAccessFile.seek(0);
System.out.println((char) randomAccessFile.read());

//跳过字节数
randomAccessFile.skipBytes(4);

//读取
System.out.println((char) randomAccessFile.read());
}
“`

## 序列化流 反序列化流

序列化:将对象以及其中的信息转化为字节进行完整保存

反序列化:将存储的字节取出来还原回对应的对象

“`java
import java.io.*;

public static void main(String[] args) {

//创建反序列化对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(“person.data”));
Person person = (Person) ois.readObject();
System.out.println(person);
ois.close();
}

public static void demo() throws IOException {
//Serializable 序列化
//对于要被序列化的对象的类,需要实现Serializable接口
//创建对象
Person person = new Person();
person.setName(“123”);
person.setAge(18);

//创建序列化流对象
ObjectOutput oos = new ObjectOutputStream(new FileOutputStream(“person.data”));

//序列化
oos.writeObject(person);

//关流
oos.close();
}
“`

static修饰的属性是属于类的,不是属于对象的,所以不会被序列化

transient 强制属性不会被序列化

“`
serialVersionUID :序列化版本号.每一个类在实现Serializable接口之后这个类中就会自动产生一个版本号。
如果这个版本号没有被指定,那么java在编译的时候会根据当前类中的属性和方法进行计算。
也就意味着当类中的属性或者方法产生变动的时候,版本号就会重新自动计算。
序列化的时候版本号会随着对象一起序列化,当对象反序列化的时候,会拿着原来的版本号会最新的版本号进行比较,如果版本号一致,则反序列化成功,否则抛出异常InvalidClassException

解决方案:
手动指定一个版本号:
private static final long serialVersionUID = 54654656L;
“`

“`
@Data
public class Person implements Serializable {
private static final long serialVersionUID = 54654656L;
private String name;

private int age;

static String kongfu;

private transient double weight;

private String address;
}
“`

## Properties

Properties是Hashtable的子类,是同步的,key和value都是字符串类型

Properties是一个映射,可以将映射中的信息持久化到文件中,也能从文件中读取信息

“`java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class AddUserTest {
public static void main(String[] args) throws IOException {
demo2();
demo1();
}

private static void demo1() throws IOException {
Properties properties = new Properties();

properties.load(new FileInputStream(“data.properties”));
System.out.println(properties);
System.out.println(properties.getProperty(“username”));

//根据Key找不到的数据会返回null
//null 空常量

System.out.println(properties.getProperty(“root”));

//”null” defaultValue是默认值
System.out.println(properties.getProperty(“age”, “19”));
properties.list(System.out);
}

private static void demo2() throws IOException {
Properties properties = new Properties();
properties.setProperty(“username”, “123”);
properties.setProperty(“age”, “20”);
properties.setProperty(“gender”, “male”);
// 把映射持久化到硬盘中
// 要求必须保存在.properties文件中
properties.store(new FileOutputStream(“data.properties”), “这是一条注释”);
}
}

“`

## Properties实际应用

“`java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class AppUtil {
private AppUtil() {}
private static Properties properties = new Properties();
//静态代码块
static {
//读取src下的app.properties文件
try {
properties.load(new FileInputStream(“Phase 1 Day 25/src/app.properties”));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void test(){
System.out.println(properties.getProperty(Constant.ROOT));
System.out.println(properties.getProperty(Constant.KEY));
}
}

“`

“`java
public interface Constant {
String ROOT = “root”;
String KEY = “key”;
}

“`

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇