Java基础笔记总结(15)-IO流(3)序列流 内存输入流 文件拷贝 对象操作流 打印流 随机访问流 数据输入输出流 Properties类

序列流

序列流可以把多个字节输入流整合成一个,从序列流中读取数据时,将从被整合的第一个流开始读,读完一个读第二个,以此类推

整合两个

SequenceInputStream(InputStream , InputStream)

FileInputStream fis1 = new FileInputStream("a.txt");

FileInputStream fis2 = new FileInputStream("b.txt");

SequenceInputStream sis = new SequenceInputStream(fis1,fis2);

FileOutputStream fos = new FileOutputStream("xxx.txt");

int b ;

while((b = sis.read())!=-1){

fos.write(b);

}

sis.close();

fos.close();

-----------------------------------------------------------------------------------

序列流整合多个(多个MP3)

FileInputStream fis1 = new FileInputStream("a.txt");

FileInputStream fis2 = new FileInputStream("b.txt");

FileInputStream fis3 = new FileInputStream("c.txt");

Vector v = new Vector<>();

v.add(fis1);

v.add(fis2);

v.add(fis3);

Enumeration en = v.elements();

SequenceInputStream sis = new SequenceInputStream(en);//将枚举中的输入流整合成一个

FileOutputStream fos = new FileOutputStream("xxx.txt");

----------------------------------------------------------------------------

IO流 内存输入流

输入流向内存中写数据,把内存当一个缓冲区,写出后可以一次性获取所有的数据

创建对象:

FileInputStream fis = new FileInputStream("e.txt");

byte[] arr = new byte[3];

int len;

while((len=fis.read())!=-1){

}

fis.close();

使用内存输入流可以完美解决一个一个读取的问题

byteArrayOutputStream baos

FileInputStream fis = new FileInputStream("e.txt");

ByteArrayOutputStream baos = new ByteArrayOutputStream();//无需关闭

int b ;

while((b=fis.read())!=-1){

baos.write(b);

}

byte[] arr = baos.toByteArray(); baos.toString();

new String(arr);

----------------------------------------------------------------------------

定义一个文件拷贝,其中读取小数组为5个字节,将a.txt内容打印出来、

FileInputStream fis = new FileInputStream("a.txt");

ByteArrayOutputStram baos = new ByteArrayOutputStream();

int len  ;

byte[] arr = new byte[5];

while((len = fis.read(arr))!=-1){

baos.write(arr,0,len);

}

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

fis.close();

----------------------------------------------------------------------------

对象操作流 ObjectOutputStream 序列化和反序列化操作

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("xxx.txt"));

Person p1 =(Person)ois.readObject(); 注意EOF异常 文件读取到末尾的位置

class Person implements Serializable{}

先声明一个对象

Person p = new Person("",xxx);//必须对类进行序列化操作

序列化,将对象写到文件上

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStram("b.txt"));

oos.writeObject(p);

----------------------------------------------------------------------------

为防止出现EOF异常,写入一个list进行优化即可 ArrayList

IO添加ID号码 生成serialVersionUID = 1L;  //一定要先存档 在读档

---------------------------------------------------------------------------

打印流 很方便将对象toString输出,并且加上自动换行,而且可以使用自动刷出模式

PrintStream

PrintStream ps = System.out; //获取标准输出流,底层通过Integer.toString() 将97转换成字符串打印

ps.println(97);

ps.write(97);查找码表,找到对应的啊

ps.close();

Person p = new Person();

ps.println(p);//转换成类实现

PrintWriter 可自动刷新

PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"),true);

pw.println(97);--------------》可以写入文本 自动刷出功能只针对println()方法,会把前面的都刷出

pw.close();

--------------------------------------------------------------------------

标准输入输出流

System.in 是InputStream 标准输入流

System.out PrintStream 标准输出流

InputStram is = System.in;

int x = is.read();

System.out println(x);

is.close();

改变标准输入输出流

System.setIn(new FileInputStream("a.txt"));改变标准输入流,原来默认指向键盘,改变后指向文件

System.setOut(new PrintStream("b.txt"));改变标准输出流 原来默认执行控制台,改变后默认指向文件

-----------------------------------------------------------------

两种方式的键盘录入

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//字节转换成字符

String line = br.readLine();

br.close();

Scanner sc = new Scanner(System.in);

sc.nextLine();

sc.close();

------------------------------------------------------------------

System.setIn(new FileInputStream("xxx.jpg"));

System.setOut(new PrintStream (copy.jpg));

InpoutStream 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);

}

--------------------------------------------------------------------

随机访问流

RandomAccessFile不属于流,其中即可以读,也可以写,是Object的子类

r 只读

rw 读写

rws 读写文件内容和元数据进行每个更新

rwd 读写更新文件内容

RandomAccessFile raf = new RandomAccessFile("g.txt","rw");

raf.write(97);

raf.seek(10);设置指针(向后一定10位)

int x = raf.read();

raf.close();

用于实现多线程下载

----------------------------------------------------------------------

数据输入输出流

DataInputStream DataOutputStream 按照基本数据类型读写

DataOutputStram dos = new DataOutputStream(new FileInputStream("xxx.txt"));

dos.writeInt(xxx);

DataInputStream dis = new DataInputStream(new FileInputStream("xxx.txt"));

int x = dis.readInt();

dis.close();

--------------------------------------------------------------------------

Properties类 双列集合(没有指定泛型,其目的当做配置文件)

Properites prop = new Properties();

        prop.put("abc",123); //基本都存放字符串

特殊功能实用

setProperity(String key,String value);

string str = getProperty(String key);

Enumeration enumeration = (Enumeration)stringPropertyNames();//获取到了所有的键

while(en,hasMoreElements()){

en.nextElemet();

}

----------------------------------------------------------------------------

Load() 和 Store()方法

Properties prop = new Properties();

prop.load(new FileInputStream("config.properties"));

将属性键值对存取到文件中

prop.setProperty("xxx","xxxxxxxx");

prop.stor(new FileOutputStream(config.properties)."配置文件的描述");

你可能感兴趣的:(Java基础笔记总结(15)-IO流(3)序列流 内存输入流 文件拷贝 对象操作流 打印流 随机访问流 数据输入输出流 Properties类)