Java 字符流

时间:2022-07-22
本文章向大家介绍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();
    }

}