package cn.itcast.otherio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
public class Demo01_SequenceInputStream {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileInputStream fis1 = new FileInputStream("a.txt");
FileInputStream fis2 = new FileInputStream("b.txt");
FileInputStream fis3 = new FileInputStream("c.txt");
FileInputStream fis4 = new FileInputStream("d.txt");
Vector<FileInputStream> v = new Vector<>();
v.add(fis1);
v.add(fis2);
v.add(fis3);
v.add(fis4);
Enumeration<FileInputStream> en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("e.txt");
int b;
while((b = sis.read()) != -1) {
fos.write(b);
}
sis.close();
fos.close();
}
public static void demo2() throws FileNotFoundException, IOException {
FileInputStream fis1 = new FileInputStream("a.txt");
FileInputStream fis2 = new FileInputStream("b.txt");
SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
FileOutputStream fos = new FileOutputStream("c.txt");
int b;
while((b = sis.read()) != -1) {
fos.write(b);
}
sis.close();
fos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis1 = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("c.txt");
int b1;
while((b1 = fis1.read()) != -1) {
fos.write(b1);
}
fis1.close();
FileInputStream fis2 = new FileInputStream("b.txt");
int b2;
while((b2 = fis2.read()) != -1) {
fos.write(b2);
}
fis2.close();
fos.close();
}
}
ByteArrayOutputStream
package cn.itcast.otherio;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo02_ByteArrayOutputStream {
/**
* @param args
* 字节流在读中文会出现半个中文,乱码
* 解决
* 1,字符流
* 2,ByteArrayOutputStream
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("e.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int b;
while((b = fis.read()) != -1) {
baos.write(b);
}
System.out.println(baos.toString());
fis.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("e.txt");
byte[] arr = new byte[4];
int len;
while((len = fis.read(arr)) != -1) {
System.out.println(new String(arr,0,len));
}
fis.close();
}
}
ObjectOutputStream
package cn.itcast.otherio;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import cn.itcast.bean.Person;
public class Demo03_ObjectOutputStream {
/**
* @param args
* @throws IOException
* 序列化,将对象写到文件上(存档)
*/
public static void main(String[] args) throws IOException {
Person p1 = new Person("张三", 23);
Person p2 = new Person("李四", 24);
Person p3 = new Person("马哥", 18);
Person p4 = new Person("辉哥", 20);
ArrayList<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt"));
oos.writeObject(list);
oos.close();
}
public static void demo2() throws IOException, FileNotFoundException {
Person p1 = new Person("张三", 23);
Person p2 = new Person("李四", 24);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));
oos.writeObject(p1);
oos.writeObject(p2);
oos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
Person p1 = new Person("张三", 23);
FileOutputStream fos = new FileOutputStream("e.txt");
FileWriter fw = new FileWriter("e.txt");
}
}
package cn.itcast.otherio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import cn.itcast.bean.Person;
public class Demo04_ObjectInputStream {
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
* @throws FileNotFoundException
* 反序列化,将文件上的对象读出来(读档)
* 如果写出两个对象,读三个对象会抛出EOFException
* EOF end of File 文件到末尾异常
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt"));
ArrayList<Person> list = (ArrayList<Person>)ois.readObject();
for (Person person : list) {
System.out.println(person);
}
ois.close();
}
public static void demo1() throws IOException, FileNotFoundException,
ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
Person p1 = (Person)ois.readObject();
Person p2 = (Person)ois.readObject();
System.out.println(p1);
System.out.println(p2);
ois.close();
}
}
PrintStream
public static void main(String[] args) throws IOException {
PrintStream ps = new PrintStream("e.txt");
ps.write(97);
ps.println(97);
ps.close();
}
public static void demo1() {
Person p = new Person("张三", 23);
PrintStream ps = System.out;
ps.println(p);
ps.println(97);
ps.write(97);
ps.close();
}
PrintWriter
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"), true);
pw.write(97);
pw.print(97);
pw.close();
}
改变标准输入流
package cn.itcast.otherio;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Demo07_SetInOut {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("a.txt"));
System.setOut(new PrintStream("h.txt"));
InputStream is = System.in;
PrintStream ps = System.out;
int b;
while((b = is.read()) != -1) {
ps.write(b);
}
is.close();
ps.close();
}
public static void demo2() {
System.out.println(97);
System.out.close();
System.out.println(97);
}
public static void demo1() throws IOException {
InputStream is = System.in;
int x = is.read();
int y = is.read();
System.out.println(x);
System.out.println(y);
}
}
RandomAccessFile
package cn.itcast.otherio;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Demo09_RandomAccessFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("h.txt", "rw");
raf.seek(10);
raf.write(102);
raf.close();
}
}
Prop写入文件
package cn.itcast.otherio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Properties;
public class Demo12_Properties {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));
prop.setProperty("username", "lisi");
prop.list(new PrintStream("config.properties"));
System.out.println(prop);
}
public static void demo2() {
Properties prop = new Properties();
prop.setProperty("username", "zhangsan");
prop.setProperty("password", "123456");
prop.setProperty("qq", "654321");
prop.setProperty("tel", "18987654321");
Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
while(en.hasMoreElements()) {
String key = en.nextElement();
String value = prop.getProperty(key);
System.out.println(key + "=" + value);
}
}
public static void demo1() {
Properties prop = new Properties();
prop.setProperty("username", "zhangsan");
prop.setProperty("password", "123456");
prop.setProperty("qq", "654321");
prop.setProperty("tel", "18987654321");
String value = prop.getProperty("username");
System.out.println(value);
System.out.println(prop);
}
}