欢迎光临散文网 会员登陆 & 注册

Java对IO中每个流的理解心得,字符流字节流,转换流复制,对象流,分割流【诗书画唱】

2020-07-21 23:01 作者:诗书画唱  | 我要投稿

写一下你对IO流的心得,对于每个流的理解



如果我们把水当做文件,把水工厂当做file类,将运输用的大小水管当做inputstream,outputstream,reader,writer,再把瓢子,盘,水桶等容器当做数组,char等。file类是可以放很多很多的水的大仓库,通过inputstream,outputstream,reader,writer这四个节点流(=水管),将水运输到社区,最后,用不同容器(=数组,char)将水装出来用。


字节流:一次读入或读出是8位二进制

字符流:一次读入或读出是16位二进制

JDK 中后缀是 Stream 是字节流;后缀是 Reader,Writer 是字符流

节点流:直接与数据源相连,读入或写出

处理流:与节点流一块使用,在节点流的基础上,再套接一层

 

常用的流

  1. 对文件进行操作:FileInputStream(字节输入流)、FileOutputStream(字节输出流)、FileReader(字符输入流)、FileWriter(字符输出流)

  2. 对管道进行操作:PipedInputStream(字节输入流)、PipedOutStream(字节输出流)、PipedReader(字符输入流)、PipedWriter(字符输出流)

  3. 字节/字符数组:ByteArrayInputStream、ByteArrayOutputStream、CharArrayReader、CharArrayWriter

  4. Buffered 缓冲流:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter

  5. 字节转化成字符流:InputStreamReader、OutputStreamWriter

  6. 数据流:DataInputStream、DataOutputStream

  7. 打印流:PrintStream、PrintWriter

  8. 对象流:ObjectInputStream、ObjectOutputStream

  9. 序列化流:SequenceInputStream



使用字符流(就是用上BufferedReader等)复制一个文本文件


//字节流:

// FileInputStream:字节输入流

// FileOutputStream:字节输出流

// 字节流是可以读取任何文件,每次读取的时候是1字节

//缓冲流(包装流):

// BufferedReader:字符输入缓冲流

// BufferedWriter:字符输出缓冲流

// BufferedInputStream:字节输入缓冲流

// BufferedOutputStream:字节输出缓冲流

//为了提高读写流的效率,引入了缓冲机制,

//进行批量的读写,提高了读写的效率。

//Buffered包装类用于加快了读写内容的速度转换流:

// 两个功能:1.将输入的字节转换为字符

// 2.进行编码转换

// InputStreamReader:字节输入转换流

// OutputStreamWriter:字节输出转换流

package all;


import java.io.*;


public class copy {

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

File old = new File("xiangDui.txt");

File copy = new File("xiangDuiCopy.txt");


CopyFangFa(old,copy);

}


public static void CopyFangFa(File old,File copy) 

throws IOException {

//用FileInputStream等来创建字节输入输出流对象:

FileInputStream byteInput = new FileInputStream(old);

FileOutputStream byteOut = new FileOutputStream(copy);


//复制过程用字节数组和write写入来实现:

// 用每次读取1024的字节的话,执行所花的时间就会变得更少。

int len;

byte[] byteArray = new byte[1024];

while ((len = byteInput.read(byteArray)) != -1) {

byteOut.write(byteArray, 0, len);

}


//用close,关闭,之后可释放资源:

byteOut.close();

byteInput.close();

}

}









使用字节流(FileInputStream等)复制一个图片




package IO;


import java.io.*;


public class copyImg {

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

File oldFile=new File("7.jpg");


BufferedInputStream CharInput=

new BufferedInputStream(

new FileInputStream(oldFile));

BufferedOutputStream CharOut=

new BufferedOutputStream(new FileOutputStream("7Copy.jpg"));


byte[] byteArray=new byte[1024];

int length=0;

while((length=CharInput.read(byteArray))!=-1){

CharOut.write(byteArray,0,length);

}

CharOut.flush();

CharOut.close();


CharInput.close();


System.out.println("复制成功!");

}}





使用缓冲流复制一个文件夹和里面的所有文件



package all;


import java.io.*;



public class copyWenJianjia {


  public static void copyAllFanFa(File oldFile, File copyFile) 

 throws Exception{

  

          //用isDirectory判断原来的老的文件是否是文件夹

          if (oldFile.isDirectory()) {

              // 被复制的文件没复制成功,不存在,就创建文件夹

              if (!copyFile.exists()) {

                  copyFile.mkdir();

              }

              // 将文件夹下的文件存入文件数组StringArray(字符串数组)

              String[] StringArray = oldFile.list();

              for (String File : StringArray) {

                  //用new等创建文件夹下的子目录,src:路径或目录

                  File oldFileSrc = new File(oldFile, File);

                  File copyFileSrc = new File(copyFile, File);

                  // 个人的理解:在自身方法中调用自身的方法,

//                  这样的话调用一次方法,就是调用了

//                  很多次正在循环执行的方法

//                  直到文件等遍历完了,就是将文件进行下一层循环。

                  copyAllFanFa(oldFileSrc, copyFileSrc);

              }

          } else {



              // 创建FileInputStream(文件输入字节流)

//        :  用于读取文件内容,源文件

              FileInputStream byteInput = new FileInputStream(oldFile);


    // 创建FileOutputStream(文件输出的字节流)

//              ,用于将读取到的文件内容

//              写到另一个磁盘文件中,复制目标文件等

              FileOutputStream byteOut =

              new FileOutputStream(copyFile);


            


              int len = -1;

              byte[] byteArray = new byte[1024];

              while (true) {

 // 从FileInputStream(文件输入流)

//     中读取数据。每执行一次,数据读到字节数组b中

                  len = byteInput.read(byteArray, 0, 256);

                  if (len == -1) {

                      break;

                  }

//                  System.out.println(byteArray.toString());

                  byteOut.write(byteArray);

              }

//              byteOut.write("\r\n".getBytes()); // 换行

              byteInput.close();

              byteOut.close();

          }


      


  }

  public static void main(String[] args) 

throws Exception{

      copyAllFanFa(new File("NewFile"),new File("NewCopyFile"));

  }

}








使用转换流复制一个文件夹下的所有txt文件,将文件的编码格式转换为utf-8




package all;


import java.io.*;



public class copyAlltxt {


  public static void copyAllFanFa(File oldFile, File copyFile) 

 throws Exception{

  

          if (oldFile.isDirectory()) {

            

              if (!copyFile.exists()) {

                  copyFile.mkdir();

              }

            

              String[] StringArray = oldFile.list();

              for (String File : StringArray) {

                

                  File oldFileSrc = new File(oldFile, File);

                  File copyFileSrc = new File(copyFile, File);


                  copyAllFanFa(oldFileSrc, copyFileSrc);

              }

          } else {



              FileInputStream byteInput = 

              new FileInputStream(oldFile);


      InputStreamReader byteChangeInput=

      new InputStreamReader(byteInput,"UTF-8");

//      InputStreamReader:字节输入转换流(个人的理解:

//      把一些内容转格式后保存。)

              FileOutputStream byteOut =

              new FileOutputStream(copyFile);


              char[] charArray=new char[100];

      int len;

      byte[] byteArray= new byte[1024];

      while((len=byteChangeInput.read(charArray))!=-1){


      byteOut.write(byteArray);

      }

      byteChangeInput.close();           


              byteInput.close();

              byteOut.close();

          }


      


  }

  public static void main(String[] args) 

throws Exception{

      copyAllFanFa(new File("NewFile"),new File("NewCopyFile"));

  }

}






使用分割流复制一个文本文件



//字节流:


// FileInputStream:字节输入流


// FileOutputStream:字节输出流


// 字节流是可以读取任何文件,每次读取的时候是1字节


//缓冲流(包装流):


// BufferedReader:字符输入缓冲流


// BufferedWriter:字符输出缓冲流


// BufferedInputStream:字节输入缓冲流


// BufferedOutputStream:字节输出缓冲流


//为了提高读写流的效率,引入了缓冲机制,


//进行批量的读写,提高了读写的效率。


//Buffered包装类用于加快了读写内容的速度转换流:


// 两个功能:1.将输入的字节转换为字符


// 2.进行编码转换


// InputStreamReader:字节输入转换流


// OutputStreamWriter:字节输出转换流


package fenGeLiu;




import java.io.*;




public class copy {


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


File old = new File("xiangDui.txt");


File copy = new File("xiangDuiCopy.txt");




CopyFangFa(old,copy);


}




public static void CopyFangFa(File old,File copy) 


throws IOException {


//用FileInputStream等来创建字节输入输出流对象:

RandomAccessFile byteInput=new RandomAccessFile(old,"r");

//r:是固定不可变的“key”一般的,代表read的部分

RandomAccessFile byteOut=new RandomAccessFile(copy,"rw");

//rw:是固定不可变的“key”一般的,代表read后的write的部分

//——————————————————

//FileInputStream byteInput = new FileInputStream(old);

//

//FileOutputStream byteOut = new FileOutputStream(copy);


//——————————————————————


//复制过程用字节数组和write写入来实现:


// 用每次读取1024的字节的话,执行所花的时间就会变得更少。


int len;


byte[] byteArray = new byte[1024];


while ((len = byteInput.read(byteArray)) != -1) {


byteOut.write(byteArray, 0, len);


}




//用close,关闭,之后可释放资源:


byteOut.close();


byteInput.close();


}


}






使用对象流保存一个对象的信息




package fenGeLiu;

import java.io.*;

import java.util.Date;

public class baoLiuOneObject {

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


student duiXiang1=new student(666,"诗书画唱三连关注",100.0f);


File File=new File("new.txt");


FileOutputStream FileOutput=new FileOutputStream(File);


ObjectOutputStream ObjectOutput

=new ObjectOutputStream(FileOutput);

ObjectOutput.writeObject(duiXiang1);


ObjectOutput.writeObject(null);

ObjectOutput.close();

System.out.println("存储成功");



// 下面是打印所有的存在new.txt的文件的对象的内容:

// File File=new File("new.txt");


//传入字节输入流:

FileInputStream byteInputAll=new FileInputStream(File);


// duiXiangInput【“拼音+英文”的自己的命名】(对象输入流)。

ObjectInputStream duiXiangInput

=new ObjectInputStream(byteInputAll);

Object duiXiang="";

while((duiXiang=duiXiangInput.readObject())!=null){

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

}

duiXiangInput.close();

}

}


class student implements Serializable{


int bianHao;

String name;

// transient float chengJi;

float chengJi;

// 这里用transient来让成绩(chengJi)不参与序列化,

// 就是后面用toString方法返回的时候,

// 不会打印出传入的真实成绩放的内容。

public student(int bianHao, String name, float chengJi) {

this.bianHao = bianHao;

this.name = name;

this.chengJi = chengJi;

}

@Override

public String toString() {

return "编号:" + 

bianHao + ", 姓名:" + name + ", 成绩("

+ "若该变量数据类型前"

+ "\n设置为transient,就不能打印传入的内容"

+ "\n出来的成绩默认为0.0):" +chengJi+ "\n";

}


}



Java对IO中每个流的理解心得,字符流字节流,转换流复制,对象流,分割流【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律