JAVA知识点总结-13IO

一:字符流

    A.什么时候用字节流什么时候用字符流呢?

         如果是音频文件、图片、歌曲,就用字节流好点

   如果是关系到中文(文本)的用字符流好




  1. 字符流的Copy:

   
		//1.封装数据源和目的地
		FileReader fr=new FileReader("a.txt");
		FileWriter fw=new FileWriter("f.txt");
		
		//2.1一次读写一个字符
//		int ch;
//		while((ch=fr.read())!=-1){
//			fw.write(ch);
//			fw.flush();
//		}
		
		
		//2.2 一次读写一个字符数组
		char[] chs=new char[1024];
		int len;
		while((len=fr.read(chs))!=-1){
			fw.write(chs,0,len);
			fw.flush();
		}
		
		//3.关流
		fw.close();
		fr.close();
	}

  2.高效字符流每写一个换一行:
      bw.newLine(); 
  3.高效字符输出流每写一次要刷新一次:
                bw.flush();
  4. 高效字符输入流当读取内容结束后 返回的是null
  


  用基本字符流和高效字符流Copy:
  
   
public class Test {
	public static void main(String[] args) throws IOException {
      method();
      method2();
      method3();
      method4();
      
	}

	private static void method4() throws IOException {
		//D.高效字符流每次读取一个字节数组copy
		BufferedReader br=new BufferedReader(new FileReader("a.txt"));
		BufferedWriter bw=new BufferedWriter(new FileWriter("j.txt"));
		
		char[] chs=new char[1024];
		int len;
		while((len=br.read(chs))!=-1){
			bw.write(chs, 0, len);
			bw.flush();
		}
		bw.close();
		br.close();
	}

	private static void method3() throws IOException {
		//C.高效字符流每次读取一个字节copy
		BufferedReader br=new BufferedReader(new FileReader("a.txt"));
		BufferedWriter bw=new BufferedWriter(new FileWriter("j.txt"));
		
		int ch;
		while((ch=br.read())!=-1){
			bw.write(ch);
			bw.flush();
		}
		bw.close();
		br.close();
	}


	private static void method2() throws IOException {
		//B.普通字符流每次读取一个字节数组Copy
		FileReader fr=new FileReader("a.txt");
		FileWriter fw=new FileWriter("j.txt");
		
		char[] chs=new char[1024];
		int len;
		while((len=fr.read(chs))!=-1){
			fw.write(chs,0,len);
			fw.flush();
		}
		fr.close();
		fw.close();
		
	}

	
	
	private static void method() throws IOException {
       //A.普通字符流单个字节copy
		 FileReader fr=new FileReader("a.txt");
		 FileWriter fw=new FileWriter("j.txt");
		 
		 int ch;
		 while((ch=fr.read())!=-1){
			 fw.write(ch);
			 fw.flush();
		 }
		fw.close();
		fr.close();
	}

}



实现以下功能:

键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

public class StudentTest {
	public static void main(String[] args) throws IOException {
		TreeSet
     
     
      
       ts = new TreeSet
      
      
       
       (new Comparator
       
       
        
        () {

			public int compare(Student s1, Student s2) {
				int num = s1.getAllScore() - s1.getAllScore();
				int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
				return num2;

			}
		});

		for (int i = 0; i < 3; i++) {
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入你的姓名");
			String name = sc.nextLine();
			System.out.println("请输入你的语文成绩");
			int chinese = sc.nextInt();
			System.out.println("请输入你的数学成绩");
			int math = sc.nextInt();
			System.out.println("请输入你的英语成绩");
			int english = sc.nextInt();

			Student s = new Student(name, chinese, math, english);
			ts.add(s);
		}
		System.out.println("数据录入完毕!");
		
		BufferedWriter bw=new BufferedWriter(new FileWriter("score.txt"));
		
		for (Student s : ts) {
			String info=s.getName()+"  "+s.getChinese()+"  "+s.getMath()+"  "+s.getEnglish()+"  "+s.getAllScore();
			bw.write(info);
			bw.newLine();
			bw.flush();
		}
		bw.close();
		
	}
}

       
       
      
      
     
     
 
   


   

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值