Java字节流,字符流,转换流

一、字节流

a.可以用来操作文本,还可以操作图片,音频等
例题:使用字节的输出输入流进行文件的复制
public class Demo{
     public static void main(String [] args){
//这里获取系统时间,来进行执行效率
  long start = System.currentTimeMillis();
  //读取文件内容
  FileInputStream fis = null;
  //向文件中写入内容
  FileOutputStream fos = null;
  try{
  //这里我们进行路径的传入
  //传入程序要读取的文件路径
      fis = new FileInputStream("/Users/lanou/Desktop/Test 2/1.jpg")
//这里传入的是程序向哪个文件路径写入
     fos = new FileOutputStream("/Users/lanou/Desktop/Test 2/111.jpg");
//方法1.使用read();这个读取的方法是每次只读取一个数据字节
int len = 0;
while((len = fos.read())!= -1){
    //这里我们读取了一个字节(我们所读取的字节都是ASCII码的形式,也就是int形式),就将该字节在写入到,我们定义的输出路径文件下
  fos.write(len);
}

//方法2.使用字节数组的方法读写
   //read(byte[]b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
   byte [] b = new byte [1024];
   int len = 0;
   while((len = fis.read(b))!=-1){
       fos.write(b,0,len);
   }
  }catch (FileNotFoundException e) {
        throw new RuntimeException("文件找不到");
    } catch (IOException e) {
        throw new RuntimeException("文件复制失败");
    }
     finally {
        //这是读入和写入,都完事了,先关闭哪个都行
         try {
             if (fis != null) {
                fis.close();
            }

        } catch (IOException e) {
            throw new RuntimeException("关闭资源失败");
        }
         finally {
             //写一起,如果第一个报了异常,第二个流,会无法关闭
            try {
                if (fos != null) {
                    fos.close();
                }

            } catch (IOException e) {
                throw new RuntimeException("关闭资源失败");
            }
        }
    }
     //获取结束的时间
     long end = System.currentTimeMillis();
     System.out.println(end-start);
  }
}
例题2.
将一个文件夹复制到另一个文件夹
public class Demo02 {
    public static void main(String[] args) throws IOException {
        //  测试
        File src = new File("/Users/lanou/Desktop/tes");
        File dest = new File("/Users/lanou/Desktop/xtes");
        copyDir(src, dest);
    }

public static void copyDir(File src,File dest)throws IOException{
    File newDir = new File (dest,src.getName());
    newDir.mkdir();
}
//复制文件夹方法-a:src原文件(要被复制的文件)b:dest 目标文件夹(要把原文件夹复制进去)

public static void copyDir(File src,File dest) throws IOException{
    //在目标文件夹下创建新的文件夹,名字和原文件夹一样
    //这里我们创建一个文件对象实例,以dest为根目录路径,src.getName()为子路径
    File newDir = new File(dest,src.getName());
    newDir.mkdir();
 //复制文件过程:a.找到文件 b.边读边写 c.找到文件夹 d.继续调用本方法
 //遍历文件夹
 File[] listFiles = src.listFiles();
 for(File subFile : listFiles){
     //判断是否是文件,是就进行读写
    if(subFile.isFile()){
    FileInputStream fis = new FileInputStream(subFils);
    //新建所遍历的文件的最终文件对象实例,用于传入输出流参数中,作为写入文件内容的路径地址
File tempFile = new File (newDir,subFile.getName);
//输出写入文件中
FileOutStream fos = new FileOutputStream(temFile);
 int len = 0;
 byte [] b = new byte [1024];
 while((len = fis.read(b))!=-1){
     fos.write(new String(b,0,len));
 }
 fis.close;
 fos.close;
    }else{
        //是文件夹,就继续执行方法,遍历
        copyDir(subFile,newDir);
    }
 }
}

二、字符流

字节的输出流的write('a');直接写入字符的方法,里面传入的是ASCALL码,而字符的输出流里面有,write(String name);直接传入一个字符串,写入到文件里面
注:字符流只能用来操作文本,不能用来操作图片音频等
a.Writer:是所有字符输出流的父类--抽象类
b.FileWriter 用来向文件中写字符类
c.FileWriter(String fileName);参数是,路径的字符地址,写在哪个文件里的路径--一个一个字符的写入
d.一个中文:
1)Mac系统是3个字节,默认使用UTF-8编码表
2)Windows是2个字节,默认使用GBK编码表
e.例题1
public class Demo04 {
public static void main(String[] args) throws IOException {
       FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test 3/haha.txt");
       fw.write(100);
       //字符输出流,在写入的时候,需要调用刷新方法
       //建议,每次写完,就要刷新一次
        fw.flush();

        //关闭资源前,会刷新

       //使用字符数组写
       char [] c = {'d','p','y','g','h'};
       fw.write(c);
       fw.flush();


       fw.write(c, 1, 3);
       fw.flush();
       //使用字符串直接写进
      fw.write("床前明月光 \n 大家解答\n");
      fw.flush();

     fw.write("白日依山尽", 1, 2);
     fw.flush();
       fw.close();


}

}
三、转换流
1.OutputStreamWriter--字符流转向字节流
 作用:可以根据不同编码格式写入
 需要使用:FileOuteStream 输出流
2.InputStreamWriter--字节流转向字符流
 作用:可以读取不同编码格式的文件
 需要使用:FileInputStream 输入流
 public class Demo07 {
  public static void main(String[] args) throws IOException {
    getUTF8();
    getGBK();
    readerUTF8();
    readerGBK();
}
    //以UTF-8读取
  public static void readerUTF8() throws IOException {
      InputStream fis = new FileInputStream("/Users/lanou/Desktop/Test 2/utf8.txt");
       InputStreamReader isr = new InputStreamReader(fis);
       char [] cs = new char[1024];
       int len = 0;
       while((len = isr.read(cs))!=-1) {
           System.out.println(new String(cs, 0, len));
       }
  }
  public static void readerGBK() throws IOException {
      InputStream fis = new FileInputStream("/Users/lanou/Desktop/Test 2/gbk.txt");
       InputStreamReader isr = new InputStreamReader(fis,"gbk");

       char [] cs = new char[1024];
       int len = 0;
       while((len = isr.read(cs))!=-1) {
           System.out.println(new String(cs, 0, len));
       }
  }
  //利用转换流,写文件 OutP
  public static void getUTF8() throws IOException {
      //字符流转向字节流
      FileOutputStream fos = null;
        FileInputStream fis = null;
      fos = new FileOutputStream("/Users/lanou/Desktop/Test 2/utf8.txt");
      OutputStreamWriter osw = new OutputStreamWriter(fos);
      //写文件
      osw.write("丁鹏");
      //只关闭外层的流就行了
      osw.close();
  }
  //利用转换流使用GBK写入文件
  public static void getGBK() throws IOException {

     FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test 2/gbk.txt");
      //构建转换流,传入编码格式 (编码格式忽略大小写)
          OutputStreamWriter osw  = new OutputStreamWriter(fos, "GBK");
        osw.write("丁鹏");
        osw.close();
  }
}

Java字节流,字符流,转换流_第1张图片

你可能感兴趣的:(Java字节流,字符流,转换流)