处理流

一、处理流

package cn.Buffered;
import java.io.BufferedReader;
import java.io.BufferedWriter;
/*
 * 字符缓冲流+新增方法(不能发生多态)
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class Buffereds {
    public static void main(String[] args) throws IOException {
        File src=new File("C:/Users/Administrator/Desktop/sun/a.txt");
        File src2=new File("C:/Users/Administrator/Desktop/sun/b.txt");
        BufferedReader reader=null;
        BufferedWriter writer=null;
        try {
            reader=new BufferedReader(new FileReader(src));
            writer=new BufferedWriter(new FileWriter(src2,true));
            //3.读取操作
//            char[] s=new char[1024];
//            int len=0;
//            try {
//                while(-1!=(len=reader.read(s))) {
//                    String info=new String(s,0,len);
//                    System.out.println(info);
//                    writer.write(s, 0,len);
//                }
            String line=null;
            while(null!=(line=reader.readLine())) {
                writer.write(line);
//                writer.append("\r\n"); 
                writer.newLine(); //换行符
            }
            
            writer.flush();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件读取失败");
            }
        } 
    

}

二、转换流 : 字节流转为字符流  处理乱码(编码集、解码集) 字符编码为字节,字节解码为字符

1.编码与解码概念

  编码:字符  ---编码字符集     二进制

  解码:二进制 ---解码字符集     字符

2.乱码问题

1)编码与解码的字符集不统一

2)字节缺少,长度丢失

示例代码

package cn.Buffered;

import java.io.UnsupportedEncodingException;

public class Test01 {
    public static void main(String[] args) throws UnsupportedEncodingException   {
        String str2="中国";
        byte[] data=str2.getBytes();
        //字节数不完整
        System.out.println(new String(data,0,3));
                
//        test01();
    }
    
    //编码与解码字符集必须相同,否则乱码
    public static void test01() throws UnsupportedEncodingException {
        //解码    byte----char
                String str="中国"; //gbk
                //编码  char----byte
                byte[] data=str.getBytes();
                //编码与解码字符集统一;
                System.out.println(new String(data));
                
                byte[] datas=str.getBytes("utf-8");  //设定编码字符集
                //不统一出现乱码
                System.out.println(new String(datas));
                
                //编码
                byte[] data2="中国".getBytes("utf-8");    //编码成字节
                //解码
                str=new String(data2,"utf-8");          //解码成字符串
                System.out.println(str);
                
                
    }
}

 

转换流:字节转为字符
* 1、输出流 OutputStreamWriter 编码
* 2、输入流 InputStreamReader 解码

package cn.Buffered;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/*
 * 转换流:字节转为字符
 * 1、输出流 OutputStreamWriter 编码
 * 2、输入流 InputStreamReader 解码
 * 
 */
public class CoverDemo {
    public static void main(String[] args) throws IOException {
        //指定解码字符集
        BufferedReader br=new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(
                                new File("C:/Users/Administrator/Desktop/sun/a.txt")),"UTF-8")
                    );
            String info =null;
            while(null!=(info=br.readLine())) {
                System.out.println(info);
            }
            br.close();
            
        
    }
}

 

posted on 2019-07-23 10:47  Mentality  阅读(182)  评论(0编辑  收藏  举报