字符流

33 篇文章 0 订阅
30 篇文章 0 订阅

字符流:为了方便中文操作

字符流 = 字节流 + 编码表

String类中的编码和解码问题:

默认的编码方式为GBK
String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组
byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组
 
编码:把看得懂的变成看不懂的:	String -- byte[]
解码:把看不懂的变成看得懂的:	byte[] -- String

普通字符流

转换流OutputStreamWriter:
构造方法:

    OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
	OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流

常用方法:

	public void write(int c) 					   写一个字符
	public void write(char[] cbuf) 				   写一个字符数组
	public void write(char[] cbuf,int off,int len) 写一个字符数组的一部分
	public void write(String str)                  写一个字符串
	public void write(String str,int off,int len)  写一个字符串的一部分
	public void flush()                    		   刷新该流的缓冲

转换流InputStreamReader:
构造方法:

    InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
	InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据

常用方法:

	public int read() 一次读取一个字符
	public int read(char[] cbuf) 一次读取一个字符数组 (如果没有读到,返回-1)

转化流的便捷类:为了简化我们的书写,转换流提供了对应的子类

OutputStreamWriter	-------		FileWriter
InputStreamReader	-------		FileReader

复制文本文件:

1、转换流
public class test {
    public static void main(String[] args) throws IOException {
        //一次读取一个字符数组,来复制文本文件
        InputStreamReader in = new InputStreamReader(new FileInputStream("D:\\a.txt"));
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("E:\\a.txt"));
        int len = 0;
        char[] chars = new char[1024];
        while ((len = in.read(chars)) != -1) {
            out.write(chars, 0, len);
            out.flush();
        }
        //释放资源
        in.close();
        out.close();
    }
}
2、便捷类
public class test1 {
    public static void main(String[] args) throws IOException {  
        FileReader fileReader = new FileReader("a.txt");
        FileWriter fileWriter = new FileWriter("E:\\a.txt");
        int len = 0;
        char[] chars = new char[1024];
        while ((len = fileReader.read(chars)) != -1) {
            fileWriter.write(chars, 0, len);
            fileWriter.flush();
        }
        fileReader.close();
        fileWriter.close();
    }
}

高效字符流

BufferedWriter写出数据 :
构造方法:

	public BufferedWriter(Writer w)

常用方法:

//字符缓冲流的特殊功能
public void newLine():根据系统来决定换行符 具有系统兼容性的换行符

BufferedReader读取数据:
构造方法:

 public BufferedReader(Reader e)

常用方法:

//字符缓冲流的特殊功能
public String readLine():一次读取一行数据 ,是以换行符为标记的,读到换行符就换行,没读到数据返回null

复制文本文件:


public class test {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("a.txt"));
        BufferedWriter out = new BufferedWriter(new FileWriter("E:\\a.txt"));
        
        String line=null;
        while ((line=in.readLine())!=null){
            out.write(line);
            out.newLine();
            out.flush();
        }
        in.close();
        out.close();
    }
}

程序小练习:

1.复制单级文件夹,复制过去后,把原文夹里面的 .jpg文件,后缀名改成 .png
public class test6 {
    public static void main(String[] args) throws IOException {
    //1.复制单级文件夹,复制过去后,把原文夹里面的 .jpg文件,后缀名改成 .png
        File old = new File("D:\\demo2");
        File news = new File("C:\\demo1");

        if (!news.exists()){
            news.mkdirs();

        }
        copyfile(old,news);
        File new1 = new File("C:\\demo1");
        xiugai(new1);
    }

    private static void xiugai(File new1) {

        File[] f1 = new1.listFiles();
        for (File file1 : f1) {
            if (file1.isFile()&&file1.getName().endsWith("jpg")){
              int n=file1.getName().length()-3;
              String s=file1.getName().substring(0,n)+"png";
                //System.out.println(file1.getPath());
                File file2 = new File("C:\\demo1\\"+s);
                file1.renameTo(file2);
            }
        }
    }

    public static void copyfile(File old,File news) throws IOException {

        File[] files = old.listFiles();
        for (File file : files) {
            copysingfile(file,news);
        }

    }

    private static void copysingfile(File file, File news) throws IOException {

        FileInputStream in = new FileInputStream(file);

        FileOutputStream out = new FileOutputStream(news+"/"+file.getName());

        byte[] b = new byte[1024];
        int len=0;
        while ((len=in.read())!=-1){
            out.write(b);
        }
        in.close();
        out.close();
    }
}
2.复制多级文件夹
public class test {
    public static void main(String[] args) throws IOException {
        //2.复制多级文件夹
        File old = new File("D:\\abc");
        File news = new File("C:\\QQQ");

        copyfile(old,news);
    }
    public static void copyfile(File old,File news) throws IOException {

        File[] files = old.listFiles();
        for (File f1 : files) {

           if (f1.isDirectory()){
               copywenjiejia(f1,news);
           }else{
               copysingfile(f1,news);
           }

        }

    }
    private static void copywenjiejia(File f1,File news) throws IOException {
        File file = new File(news+"\\"+f1.getName());
        file.mkdirs();
        copyfile(f1,file);

    }

    private static void copysingfile(File file, File news) throws IOException {

        FileInputStream in = new FileInputStream(file);

        FileOutputStream out = new FileOutputStream(news+"/"+file.getName());

        byte[] b = new byte[1024];
        int len=0;
        while ((len=in.read())!=-1){
            out.write(b);
        }
        in.close();
        out.close();
    }
}
3、需求:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩)
  按照总分从高到低存入文本文件
public class test11 {
    public static void main(String[] args) throws IOException {
     
        TreeSet<student> set = new TreeSet<>(new Comparator<student>() {

            @Override
            public int compare(student o1, student o2) {

                 return o2.getSumScore()-o1.getSumScore();
            }
        });

        ArrayList<student> list = new ArrayList<>();
        System.out.println("请输入学生信息");

            for (int i = 0; i <5 ; i++) {
                Scanner sc=new Scanner(System.in);
                System.out.println("请输入第"+(i+1)+"个学生的姓名:");
                String name=sc.nextLine();

                System.out.println("请输入第"+(i+1)+"个学生的语文成绩:");
                int chineseScore=sc.nextInt();

                System.out.println("请输入第"+(i+1)+"个学生的数学成绩:");
                int mathScore=sc.nextInt();

                System.out.println("请输入第"+(i+1)+"个学生的英语成绩:");
                int englishScore=sc.nextInt();
                int sumScore=chineseScore+mathScore+englishScore;
                student student = new student(name,chineseScore,mathScore,englishScore,sumScore);
                set.add(student);
            }

        BufferedWriter out = new BufferedWriter(new FileWriter("c.txt"));
            
        for (student s : set) {
            out.write("姓名:"+s.getName()+"\t");
            out.write("语文:"+s.getChineseScore());
            out.write("数学:"+s.getMathScore());
            out.write("英语:"+s.getEnglishScore());
            out.write("总分:"+s.getSumScore());
            out.write("\r\n");
            out.flush();
        }

        out.close();
    }
}

class student implements Serializable{
    private static final long serialVersionUID = 5903060056394132996L;
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;
    private int sumScore;

    public student () {
    }

    public student(String name, int chineseScore, int mathScore, int englishScore, int sumScore) {
        this.name = name;
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
        this.sumScore = sumScore;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChineseScore() {
        return chineseScore;
    }

    public void setChineseScore(int chineseScore) {
        this.chineseScore = chineseScore;
    }

    public int getMathScore() {
        return mathScore;
    }

    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }

    public int getEnglishScore() {
        return englishScore;
    }

    public void setEnglishScore(int englishScore) {
        this.englishScore = englishScore;
    }

    public int getSumScore() {
        return sumScore;
    }

    public void setSumScore(int sumScore) {
        this.sumScore = sumScore;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值