序列流#
- SequenceInputStream 关闭时会关闭所有流
public class Demo1_Stream {
public static void main(String[] args) throws IOException {
FileInputStream fr1 = new FileInputStream("read1.md");
FileInputStream fr2 = new FileInputStream("read2.md");
SequenceInputStream sis = new SequenceInputStream(fr1,fr2);
FileOutputStream fos = new FileOutputStream("read3.md");
int b ;
while ( ( b = sis.read()) != -1){
fos.write(b);
}
sis.close();
fos.close();
}
public static void demo1() throws IOException {
FileInputStream fr1 = new FileInputStream("read1.md");
FileOutputStream fos = new FileOutputStream("read3.md");
int b;
while ((b = fr1.read()) != -1) {
fos.write(b);
}
fr1.close();
FileInputStream fr2 = new FileInputStream("read2.md");
while ((b = fr2.read()) != -1) {
fos.write(b);
}
fr2.close();
fos.close();
}
}
IO 流整合多个#
public class Demo1_Stream {
public static void main(String[] args) throws IOException {
Vector<FileInputStream> v = new Vector<>();
FileInputStream fr1 = new FileInputStream("read1.md");
FileInputStream fr2 = new FileInputStream("read2.md");
v.add(fr1);
v.add(fr2);
Enumeration<FileInputStream> en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("read3.md");
int b ;
while ( (b = sis.read()) != -1){
fos.write(b);
}
sis.close();
fos.close();
}
}
ByteArrayOutputStream 获取数据#
- ByteArrayOutputStream 的 toString 可直接转为默认或指定的字符
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Deom2_ByteArrayOutputStream {
public static void main(String[] args) throws IOException {
FileInputStream fi = new FileInputStream("read1.md");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int b ;
while ( (b = fi.read()) != -1){
baos.write(b);
}
fi.close();
System.out.println(baos.toString());
System.out.println(baos.toString("utf-8"));
}
private static void Demo1() throws IOException {
FileInputStream fi = new FileInputStream("read1.md");
byte[] arr = new byte[3];
int len;
while ( ( len = fi.read(arr)) != -1){
System.out.println(new String(arr,0,len));
}
fi.close();
}
}
内存输出#
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo3_Test {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("read1.md");
byte[] arr = new byte[5];
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ( (len = fis.read(arr)) != -1){
bos.write(arr,0,len);
}
fis.close();
System.out.println(bos.toString());
bos.close();
}
}
随机读和存 RandomAccessFile#
import java.io.IOException;
import java.io.RandomAccessFile;
public class Demo4_RandomAccessFile {
public static void main(String[] args) throws IOException {
// Demo1();
Demo2();
}
public static void Demo2() throws IOException{
RandomAccessFile raf = new RandomAccessFile("random.txt","rw");
int b;
while ( (b = raf.read()) != -1){
System.out.println(b);
}
raf.close();
}
private static void Demo1() throws IOException {
RandomAccessFile raf = new RandomAccessFile("random.txt","rw");
raf.write(97);
raf.seek(10);
raf.write(98);
raf.close();
}
}
对象操作流 ObjecOutputStream#
import com.lizicai.bean.Person;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Demo5_ObjectOutputStream {
public static void main(String[] args) throws IOException {
Person p1 = new Person("小明", 23);
Person p2 = new Person("小红", 23);
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("person.txt"));
oos.writeObject(p1);
oos.writeObject(p2);
oos.close();
}
}
import com.lizicai.bean.Person;
import java.io.*;
public class Deom6_ObjectInputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream oos = new ObjectInputStream(new FileInputStream("person.txt"));
Person p1 = (Person) oos.readObject();
System.out.println(p1.getName()+p1.getAge());
Person p2 = (Person) oos.readObject();
System.out.println(p2.getName()+p2.getAge());
}
}
对象操作优化#
import com.lizicai.bean.Person;
import java.io.*;
import java.util.ArrayList;
public class Demo7_OptimeObjectStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hashSetPerson.txt"));
ArrayList<Person> arrayList = new ArrayList<>();
arrayList.add(new Person("小明",25));
arrayList.add(new Person("小红",23));
arrayList.add(new Person("小王",26));
oos.writeObject(arrayList);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("hashSetPerson.txt"));
ArrayList<Person> personArrayList = ( ArrayList<Person> ) ois.readObject();
for(Person p : personArrayList){
System.out.println(p.getName() + p.getAge());
}
}
}
IO 流增加 id 号#
- 更改Person属性时更改serialVersionUID 可以准备知道原来Persion和现在Persion属性区别
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID= 1L;
private String name;
private int age;
public Person(){}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
import java.io.*;
public class Demo8_DataInputStream {
public static void main(String[] args) throws IOException {
Demo1();
DataOutputStream dos = new DataOutputStream(new FileOutputStream("outData.txt"));
dos.writeInt(997);
dos.writeInt(998);
dos.writeInt(999);
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("outData.txt"));
int x = dis.readInt();
int y = dis.readInt();
int z = dis.readInt();
System.out.println(x);
System.out.println(y);
System.out.println(z);
dis.close();
}
private static void Demo1() throws IOException {
FileOutputStream fos = new FileOutputStream("output.txt");
fos.write(997);
fos.close();
}
private static void Demo2() throws IOException {
FileInputStream fis = new FileInputStream("output.txt");
int b = fis.read();
fis.close();
}
}
打印流的概述和特点#
- PrintStream 和 PrintWriter 分别是打印的字节流和字符流
- 只操作数据目的
- 自动刷出作用不大
import java.io.*;
public class Demo9_PrintStream {
public static void main(String[] args) throws IOException {
// Demo2();
PrintWriter pw = new PrintWriter(new FileOutputStream("printWriter.txt"), true);
pw.println(97);
}
private static void Demo2() throws FileNotFoundException {
PrintWriter pw = new PrintWriter("printWriter.txt");
pw.println(97);
pw.write(97);
pw.flush();
pw.close();
}
private static void demo1() {
PrintStream p = System.out;
p.println("Hello World!");
p.write(97);
p.close();
}
}
标准输入输出流概述和输出语句#
- 标准输入只有1个, 未关联到文件, 不用关闭, 关闭后无法打开了
- 标准输出流未关联文件, 也不用关闭
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
public class Demo10_SystemInOut {
public static void main(String[] args) throws IOException {
// demo1();
System.setIn(new FileInputStream("systemin.txt"));
System.setOut(new PrintStream("systemout.txt"));
InputStream is = System.in;
PrintStream ps = System.out;
int b;
while ( ( b = is.read()) != -1){
ps.write(b);
}
is.close();
ps.close();
}
private static void demo1() throws IOException {
InputStream is = System.in;
int x = is.read();
System.out.println(x);
is.close();
InputStream is2 = System.in;
int y = is2.read();
System.out.println(y);
}
}
import java.io.*;
public class Demo11_SystemInOutTest {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("systemin.txt"));
System.setOut(new PrintStream("systemout.txt"));
InputStream is = System.in;
PrintStream ps = System.out;
byte [] arr = new byte[1024];
int len;
while ( ( len = is.read(arr)) != -1){
ps.write(arr,0,len);
}
is.close();
ps.close();
}
}
2种键盘录入方式#
import java.util.Scanner;
public class Demo12_SystemIn {
public static void main(String[] args) throws IOException {
// demo1();
Scanner sc = new Scanner(System.in);
String s = null;
if(sc.hasNext()){
s = sc.nextLine();
}
System.out.println(s);
}
private static void demo1() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
System.out.println(line);
}
}
Properties的概述和作为Map集合的使用#
- Object setProperty(String key, String value) 添加键值对
- Enumeration propertyNames() 获取 key值枚举
- String getProperty(String key) 根据key 值获取 value
import java.util.Enumeration;
import java.util.Properties;
public class Demo13_Properties {
public static void main(String[] args) {
// demo1();
Properties pp = new Properties();
pp.setProperty("name", "小明");
pp.setProperty("tel", "12310001000");
System.out.println(pp);
Enumeration <String> en = (Enumeration<String>) pp.propertyNames();
while ( en.hasMoreElements()){
String key = en.nextElement();
System.out.println(key+pp.getProperty(key));
}
}
private static void demo1() {
Properties p = new Properties();
p.put("intA", 12);
System.out.println(p);
}
}
Properties 读和存#
- void load(InputStream inStream) throws IOException 读取文件
- void store(OutputStream out, String comments) throws IOException 写到文件, comments 加入注释
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class Demo13_Properties {
public static void main(String[] args) throws IOException {
Properties pp = new Properties();
System.out.println(pp);
pp.load(new FileInputStream("config.properties"));
System.out.println(pp);
pp.setProperty("qq","123");
System.out.println(pp);
pp.store(new FileOutputStream("config.properties"), "utf-8");
}
}