黑马程序员________Java中IO技术其他常用流对象学习笔记

 ------- android培训java培训、期待与您交流! ----------

 

         Propertieshashtable的子类。也就是说他具有Map的特点,是将数据以键值对的方式进行操作。一般用来进行操作配置文件。

Properties pro=new Properties();

pro.load(new FileInputStream(1.txt));//将流对象加载进集合

pro.setProperty(“key””value”)//设置键值对

pro.getProperty();获取相应的值

pro.store(new FielOutputSteam(1.txt));//将集合存储进流对象。

        

        以下是一个小练习,对一个主题程序进行运行次数的控制,五次以内正常运行,五次以上开始提示,十次以上封闭功能程序。

 

import java.io.*;

import java.util.*;

class  Demo

{

        public static void main(String[] args) throws Exception

        {

                              check();

                              System.out.println("主程序运行");//看是否输入该语句判断主程序的运行情况

        }

                  

                   public static void  check() throws Exception

                   {

                            File f=new File("c:\\cdk.txt");

                            if(f.createNewFile())//如果该文件没有则返回false,返回true代表需要建立一个新的配置文件

                            {

                                     FileOutputStream fosNew=new FileOutputStream(f);//流对象关联文件

                                     Properties proNew=new Properties();

                                     proNew.setProperty("NUM","0");//将属性设置进properties对象

                                     proNew.store(fosNew,"cdk");//将properties数据存入流对象中

                                     fosNew.close();

                            }

                           

                            FileInputStream fis=new FileInputStream(f);

                            Properties pro=new Properties();

                            pro.load(fis);//将流中数据读取到pro对象中来

                            int num=Integer.parseInt(pro.getProperty("NUM"));

                            if(num>10)

                            {

                                     System.out.println("使用超过十次,停止提供服务");

                                     System.exit(0);

                            }

                            if(num>5)

                            {

                                     System.out.println("使用超过五次请及时联系代理商,否则将不能使用");

                            }

                            pro.setProperty("NUM", String.valueOf(++num));

                            FileOutputStream fos =new FileOutputStream(f) ;

                            pro.store(fos,"cdk");

                           

                            fis.close();

                            fos.close();

                   }

       

}

 

        

         打印流,该流提供了打印方法,可以将各种数据类型的数据都原样打印。可以相关字节流也可以相关字符流。

import java.io.*;

 

class  PrintStreamDemo

{

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

{

BufferedReader bufr =

new BufferedReader(new InputStreamReader(System.in));

PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);

String line = null;

while((line=bufr.readLine())!=null)

{

if("over".equals(line))

break;

out.println(line.toUpperCase()); //打印流自带的方法println可以打印完自动刷新和换行

}

out.close();

bufr.close();

 

}

}

 

         合并流,是将几个流对象按照顺序的读取,这里要注意的是合并流用到了Vector集合。需要传给合并流对象一个Vector的枚举对象Enumeration

         以下是将三个文本文件按指定顺序合并到一个文件中。

import java.io.*;

import java.util.*;

class SequenceDemo

{

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

{

 

Vector<FileInputStream> v = new Vector<FileInputStream>();

 

v.add(new FileInputStream("c:\\1.txt"));

v.add(new FileInputStream("c:\\2.txt"));

v.add(new FileInputStream("c:\\3.txt"));

 

Enumeration<FileInputStream> e = v.elements();//获取Vector的枚举对象

 

SequenceInputStream sis = new SequenceInputStream(en);

 

FileOutputStream fos = new FileOutputStream("c:\\4.txt");//合并到4.txt

 

byte[] buf = new byte[1024];

int len =0;

while((len=sis.read(buf))!=-1)

{

fos.write(buf,0,len);

}

 

fos.close();

sis.close();

 

 

}

}

 

 

 

 

         有合并就有切割java没给我们提供相应的流对象,但是我们可以通过自己的方法来实现。

 

class SplitDemo

{

public static void splitFile()throws IOException

{

FileInputStream fis =  new FileInputStream("c:\\1.bmp");//源文件

 

FileOutputStream fos = null;

 

 

byte[] buf = new byte[1024*1024];//按照一照来且可

 

int len = 0;

int count = 1;

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

{

fos = new FileOutputStream("c:\\splitfiles\\"+(count++)+".part");每次读取一兆数据后就new一个文件

fos.write(buf,0,len);//并存储

fos.close();

}

 

fis.close();

 

}

}

         切割完毕后把切割完的几个part碎片文件通过SequenceInputStream对象合并回去,文件依然可以用。说明切割成功。

 

        

         对象的序列化,被序列化的对象一定要实现Serializable接口,但是该接口内没有定义任何抽象方法,所以实现该接口其实就是为对象添加一个UID固定标示。注意对象中被transientstatic修饰的成员不会被序列化。

import java.io.*;

 

class ObjectStreamDemo

{

public static void main(String[] args) throws Exception

{

writeObj();

readObj();

}

public static void readObj()throws Exception

{

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

Person p = (Person)ois.readObject();//读取person对象,必须要强转

 

System.out.println(p);

ois.close();

}

 

public static void writeObj()throws IOException

{

ObjectOutputStream oos =

new ObjectOutputStream(new FileOutputStream("obj.txt"));

 

oos.writeObject(new Person("lisi0",399,"kr"));//person对象写入文件

 

oos.close();

}

}

 

 

         管道流是一个可以讲流内数据在不同线程中相关联的流对象,应用于多线程对数据的传输。

package com.itheima.gaoxin;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PipedInputStream;

import java.io.PipedOutputStream;

import java.util.ArrayList;

import java.util.Collection;

 

public class Demo

{

 

public static void main(String[] args)throws Exception

{

 

                  

PipedInputStream pis=new PipedInputStream(pos);

PipedOutputStream pos=new PipedOutputStream(pis);

//pis.connect(pos);如果构造方法没有将两个管道流建立关系,就可以用这个方法关联

new Thread(new Read(pos)).start();

 

new Thread(new Write(pis)).start();

}

        

}

class Read implements Runnable

{

PipedOutputStream pos;

 

public Read(PipedOutputStream pos)

{

 

this.pos = pos;

}

 

 

public void run()

{

 

                  

try

{

FileInputStream fis=new FileInputStream("c:\\1.txt");

int len=0;

byte[] buf=new byte[1024];

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

{

pos.write(buf);

                           

}

fis.close();

pos.close();

                           

}

catch (Exception e1)

{

 

e1.printStackTrace();

}

                  

                  

}

        

}

 

class Write implements Runnable

{

PipedInputStream pis;

 

public Write(PipedInputStream pis)

{

this.pis = pis;

}

 

 

public void run() {

 

try

{

FileOutputStream fos=new FileOutputStream ("d:\\1.txt");

int len=0;

byte[] buf=new byte[1024];

while((len=pis.read(buf))!=-1)

fos.write(buf);

fos.close();

pis.close();

}

catch (Exception e)

{

 

e.printStackTrace();

}

                  

}

        

}

 

 

         RandomAccessFile这个流对象的特点在于它内部封装了一个数组,而且通过指针对数组的元素进行操作。他可以通过getFilePointer获取指针位置,通过seek改变指针的位置,通过skipBytes方法跳过指定的字节数,就为了我们提供了一些操作模式。比如文件中存了12字节的姓名和4个字节的年龄,我只想读取姓名,那么就可以

byte[] buf=new byte[12];

int len=0;

while((len=ram.read(buf))!=-1)

{

         System.out.println(new String(buf,0,len));

         ram. skipBytes(4);

}

         该流对象内部同时封装了读取和写入的方法,操作对象的时候可以指定模式:

         1、如果模式为只读 r。不会创建文件。会去读取一个已存在文件,如果该文件不存在,则会出现异常。

         2、如果模式为rw。操作的文件不存在,会自动创建。如果存则在不会覆盖。

 

 

         DataStream这个流对象是用来操作基本数据类型的。其中提供了很多针对基本数据类型的方法如writeInt()writeChar()readInt()readChar()等更加便捷的方法。用法前面的流对象相似。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值