您的位置:首页 > 编程语言 > Java开发

JAVA知识点总结-13IO

2017-05-10 22:52 363 查看
一:字符流

    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();

}
}




   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: