IO流

File类:

File f = new File(path);

文件的创建:f.creageNewFile();

文件夹的创建:f.mkdir();多级:f.mkdir();

删除判断f.exists()文件是否存在,删除:f.delete();

判断目录:f.isDirectory();

字节流:

输入:InputStream in = new FileInputStream(path路径);

     byte[] b = new byte[(int)
f.length()];//(int) f.length()用来确定接收
     两种方式读:
     1. Int len =in.read(b); 
     System.out.print (new String(b));
     2.for(int i= 0 ;i<b.length;i++){
             b[i] =(byte)in.read();
	}

输出:OutputStream out = new FileOutputStream(f,true);//加上ture后表示追加

byte[] b =n.getBytes();//(int) f.length()用来确定接收数组的长度
     两种方式写:
     1.out.write(b);
     2.for(int i=0;i<b.length;i++){
                     out.write(b[i]);
              } 

字符流:

输入:FileReader in = new FileReader(f);//new本类
输出:Writer out = new FileWriter(路径,true);//多态,父类的引用变量指向子类对象

总结:字节流和字符流的区别
字符流使用了缓冲区,一个是以二进制进行读写,一个是字符的形式进行读写。

转换流
好比字符这根管子里有字节流管子,使用字符流来操作,实现字节传输。

将输出的字符流转化为字节流
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(f));
将输入的字节流转化为字符流
 InputStreamReader isw = new InputStreamReader(new FileInputStream(f));

管道流:
多线程机制应用,相当于在两个线程上插入流管道,一个写,一个读。注意:流,要随着类的加载而加载,应为当线程运行就无法插入流,插入过后,利用get方法获取两个流:p.getPos().connect(s.getPis());
进行连接。

   发送端:class Person implements Runnable{
     private PipedOutputStream pos=null;
     public Person() {
               pos=new PipedOutputStream();
     }
     public void run() {
                        pos.write(“写出内容”.getBytes());
                        pos.close();
     }
     public PipedOutputStream getPos() {
               return pos;
     }
     public void setPos(PipedOutputStream pos) {
               this.pos = pos;
     }
}

     接收端:class Spook implements Runnable{
     private PipedInputStream pis=null;
     public Spook() {
               pis=new PipedInputStream();
     }
     public void run() {
               byte[] b = new byte[1024];
               int length=0;
               length= pis.read(b);
               System.out.println(new String(b,0,length));
     }
     public PipedInputStreamgetPis() {
               return pis;
     }
     public void setPis(PipedInputStream pis) {
               this.pis = pis;
     }
}	
 测试类:
 public class pipedDemo {  
	public static void main(String[] args){
              Person p = new Person();
               Spook s = new Spook();
               p.getPos().connect(s.getPis());
               new Thread(p).start();
               new Thread(s).start();
     }
}

输入输出的重定向:

1.static void setErr(PrintStream err) 重新分配“标准”错误输出流。

2.static void setIn(InputStream in) 重新分配“标准”输入流。

3.static void setOut(PrintStream out) 重新分配“标准”输出流。

输出重定向:

 System.setOut(new PrintStream(new FileOutputStream(path)));
 System.out.print("输出重定向");

输入重定向:

 System.setIn(new FileInputStream(path));
 InputStream is = System.in;
 byte[] b = new byte[1000];
 int l = is.read(b);
 System.out.println(new String(b,0,l));

压缩流:(ZipOutputStream)
注意:压缩文件夹(一定要关闭流不然就压缩不成功,文件名最好是用英文)

               Filef = new File(path1);
               InputStreamips = new FileInputStream(f);
               ZipOutputStream zops = new ZipOutputStream(new FileOutputStream(path2));
               zops.putNextEntry(new ZipEntry(f.getName()));//为每一个被压缩的文件设置名字
               zops.setComment("注释");//设置注释
               int temp;
               while((temp=ips.read())!=-1){
                        zops.write(temp);
               }
               ips.close();
               zops.close();

对象流:

序列化:被序列化的类必须实现Serializable接口),序列化主要是对类整体输出起作用。
transient关键字(这个属性就不会在被序列化,序列化主要是对属性的序列化)。
多个对象的存和读:

		Person[] p = {new Person(”属性1”, ” 属性2”),newPerson(” 属性1”, ” 属性2”)};

               String path = "D:"+ File.separator + "soft" + File.separator + "object.txt";
               
               //写:
               OutputStream ops = new FileOutputStream(path);
               InputStream ips = new FileInputStream(path);
               ObjectOutputStream oops = new ObjectOutputStream(ops);
               oops.writeObject(p);
               oops.close();
               
               //读:
               ObjectInputStream oips = new ObjectInputStream(ips);
               Object[] b = (Object[]) oips.readObject();
               for(int i=0;i<b.length;i++){
                        System.out.println(b[i]);
               }
               oips.close();

合并流:
把两个读取流合并到SequenceInputStream流里,在通过输出流输出

1.两个流合并输出

 SequenceInputStream(InputStreams1, InputStream s2)
 FileInputStreamis1 = new FileInputStream(f1); 
 FileInputStreamis2 = new FileInputStream(f2); 
 FileOutputStreamis3 = new FileOutputStream(f3);
 SequenceInputStream  s = new SequenceInputStream(is1, is2);
  int temp=0;
  while((temp=s.read())!=-1){
        is3.write(temp);
   }

2.合并多个文件输出
SequenceInputStream(Enumeration<?extends InputStream> e) //可以利用集合工具类产生Enumeration对象
FileInputStream is1 = new FileInputStream(f1); 
FileInputStream is2 = new FileInputStream(f2); 
FileInputStream is4 = new FileInputStream(f4); 
FileOutputStream is3 = new FileOutputStream(f3);
ArrayList<FileInputStream> ar = new ArrayList<FileInputStream>();
ar.add(is1);
ar.add(is2);
ar.add(is4);
Enumeration<FileInputStream> et = Collections.enumeration(ar);
SequenceInputStream  s = new SequenceInputStream(et);

缓冲流:

BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。

缓冲流特有的方法:
voidnewLine():写一个换行符,这个换行符由系统决定,不同的操作系统newLine()方法使用的换行符不同
String readLine():一次读取一行数据,但是不读取换行符
缓冲流读写两种方式:

1.第一种方式:使用缓冲流不使用字符数组

 int ch;
       while((ch=br.read())!=-1){
                bw.write(ch);
       }

2.第二种方式:使用缓冲流使用字符数组

char[] chs = new char[1024];
               int len;
               while((len=br.read(chs))!=-1){
                        bw.write(chs,0,len);
               }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值