为了账号安全,请及时绑定邮箱和手机立即绑定

Java 字符流

标签:
Java

Reader

用于读取字符流的抽象类。

InputStreamReader

是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符

FileReader

用来读取字符文件的便捷类

BufferedReader

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

Writer

写入字符流的抽象类

OutputStreamWriter

是字符流通向字节流的桥梁:使用指定的 charset 将要向其写入的字符编码为字节

FileWriter

用来写入字符文件的便捷类

BufferedWriter

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

1.字符流复制数据字节
public static void main(String[] args) throws IOException {        //数据源
        FileReader fReader = new FileReader("a.txt");        //目的地
        FileWriter fWriter = new FileWriter("b.txt");        
        int by = 0;        while((by=fReader.read())!=-1){
            fWriter.write(by);
        }
        fReader.close();
        fWriter.close();
    }
2.字符数组
public class copy4 {    public static void main(String[] args) throws IOException {        //数据源
        FileReader fReader = new FileReader("a.txt");        //目的地
        FileWriter fWriter = new FileWriter("b.txt");        
        char[] chs = new char[1024];        int len = 0;        while((len = fReader.read(chs))!=-1){
            fWriter.write(chs,0,len);
            fWriter.flush();
        }
        
        fReader.close();
        fWriter.close();
    }
}
3.字符缓冲数组
public class copy5 {    public static void main(String[] args) throws IOException {        //数据源
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));        //目的地
        BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));        
        char[] chs = new char[1024];        int len = 0;        while((len = br.read(chs))!=-1){
            bw.write(chs,0,len);
            bw.flush();
        }        //释放资源
        br.close();
        bw.close();
    }
}
4.字符缓冲流

newLine();换行方法
readLine()方法到末尾返回null

String line = null;while((line=br.readLine())!=null){
         System.out.println(line);
}
5.字符流
public class copy6 {        
    public static void main(String[] args) throws Exception {        //数据源
        String srcStr1= "a.txt";        //目的地
        String srcStr2= "b.txt";
        mathod(srcStr1,srcStr2);
    }    private static void mathod(String srcStr1, String srcStr2) throws IOException {
        
        BufferedReader br= new BufferedReader(new FileReader(srcStr1));
        BufferedWriter bw= new BufferedWriter(new FileWriter(srcStr2));
        
        String line = null;        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();
    }
6.字符流与集合数组
public class ArrayListToFile {
    
     public static void main(String[] args) throws IOException {        
         //数据源
         ArrayList<String> array = new ArrayList<String>();         //添加数据
         array.add("hello");         array.add("word");         array.add("java");         //目的地
         BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));         
         for (String string : array) {
            bw.write(string);
            bw.newLine();
            bw.flush();
        }         //释放资源
         bw.close();
    }
7.学生类
  • 1.创建学生类

  • 2.创建排序集合TreeSet<Student>

  • 3.创建键盘输入

  • 4.添加进学生类

  • 5.添加进集合

  • 6.创建输出流

  • 7.写入文本

public class SortInfo {     
     public static void main(String[] args) throws IOException {        //创建排序集合
         TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {            
            @Override
            public int compare(Student s1, Student s2) {            
                int sum = s2.sum()-s1.sum();                int sum1 = sum == 0 ? s2.getChinese()-s1.getChinese():sum;                int sum2 = sum1 == 0 ?s2.getMath()-s1.getMath():sum1;                int sum3 = sum2 == 0 ?s2.getName().compareTo(s1.getName()):sum2;                return sum3;
            }
             
        });    
         //创建键盘输入
         for(int i =0 ;i< 5 ;i++){
            Scanner input = new Scanner(System.in);
             System.out.println("请输入第"+(i+1)+"次得学生信息");
             System.out.println("学生姓名:");
             String name = input.nextLine();
             System.out.println("语文成绩:");             int chinese = input.nextInt();
             System.out.println("数学成绩:");             int math  =  input.nextInt();
             System.out.println("英语成绩:");             int english = input.nextInt();             //创建学生对象
             Student student = new Student();
             student.setMath(math);
             student.setChinese(chinese);
             student.setEnglish(english);
             student.setName(name);             //添加进入集合
             ts.add(student);
         }         //创建输出流
         BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
         bw.write("学生信息如下:");
            bw.newLine();
            bw.flush();
            bw.write("姓名,语文,数学,英语");
            bw.newLine();
            bw.flush();         for (Student stu : ts) {
            StringBuilder sb = new StringBuilder();
            sb.append(stu.getName()).append(",").append(stu.getChinese()).append(",")
            .append(stu.getMath()).append(",").append(stu.getEnglish());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }         
         //释放资源
         bw.close();
    }

}
8.将文本中的字符排序后加入到另一个文本中

/**

  • 1.已知s.txt文件中有一个字符串“ndjdnsnakdapiiisnjvmsdsiajdsailn”;

  • 2.读取文件的内容,存储到字符串中

  • 3.把字符串转化为字符数组

  • 4.对字符数组进行排序

  • 5.把字符数组转化为字符串

  • 6.通过字符输出流把字符串输出到ss.txt


*/

public class StringArray {    
       public static void main(String[] args) throws IOException {         //封装路径
           File srcFolder = new File("H:\\s.txt");
           File destFolder = new File("H:\\ss.txt");         //字符读取流
           BufferedReader br = new BufferedReader(new FileReader(srcFolder));         //读取字符串
           StringBuilder sb = new StringBuilder();
           String line = null;          while((line = br.readLine())!=null){
              sb.append(line);
          }         //字符串转化为字符数组
          char[] arrays = sb.toString().toCharArray();         //将字符数组进行排序
          Arrays.sort(arrays);         //将字符数组转化为字符串
          String str = String.valueOf(arrays);
          System.out.println(str);         //建立输出流输出
          BufferedWriter bw = new BufferedWriter(new FileWriter(destFolder));
          bw.write(str);
          bw.flush();
          bw.close();
    }
}
9.使用PrintWriter进行输出操作
public class PrintWriteDemo {      
       public static void main(String[] args) throws IOException {         
           //数据源
           BufferedReader br = new BufferedReader(new FileReader("ByteArrayStreamDemo.java"));           //目的地
           PrintWriter   pw = new PrintWriter(new FileWriter("a.java"),true);
           
           String line = null;           while((line = br.readLine())!=null){
               pw.println(line);
           }           //释放资源
           pw.close();
           br.close();
    }

}



作者:张晓天a
链接:https://www.jianshu.com/p/9be91891bcd2


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
205
获赞与收藏
1007

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消