接收 ---- 二进制 ---- 十进制 --- 老地方 编码:把看得懂的变成看不懂的: String -- byte[] 解码:把看不懂的变成看得懂的: byte[] -- String OutputStreamWriter 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) 写一个字符串的一部分 InputStreamReader InputStreamReader的构造方法 InputStreamReader(InputStream is):用默认的编码(GBK)读取数据 InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据 方法概述 public int read() 一次读取一个字符 public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1 FileWriter和FileReader FileReader和FileWriter的出现 转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的, 所以,为了简化我们的书写,转换流提供了对应的子类。 FileWriter FileReader 字符流便捷类: 因为转换流的名字太长了,并且在一般情况下我们不需要制定字符集, 于是java就给我们提供转换流对应的便捷类 转换流 -------------------------- 便捷类 OutputStreamWriter ------- FileWriter InputStreamReader ------- FileReader 字符缓冲流的特殊功能 BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符 BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数据返回null 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 字符流复制文件 基本的流一次一个字符 public class t1 { public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(new FileInputStream("Student.txt")); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("SSS.txt")); int len=0; while ((len=reader.read())!=-1){ writer.write(len); writer.flush(); } writer.close(); reader.close(); } } 基本的流一次一个字符数组 public class t2 { public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(new FileInputStream("Student.txt")); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("sss.txt")); int len=0; char[] chars = new char[1024 * 1024]; while ((len=reader.read(chars))!=-1){ writer.write(chars,0,len); writer.flush(); } reader.close(); writer.close(); } } 高效的流一次一个字符 public class t3 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("Student.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt")); int len=0; while ((len=reader.read())!=-1){ writer.write(len); writer.flush(); } reader.close(); writer.close(); } }
高效的流一次一个字符数组 public class t4 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("Student.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt")); int len=0; char[] chars = new char[1024 * 1024]; while ((len=reader.read(chars))!=-1){ writer.write(chars,0,len); writer.flush(); } reader.close(); writer.close(); } } 高效的流一次一行字符串 public class t5 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("Student.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("sss.txt")); String len=null; while ((len=reader.readLine())!=null){ writer.write(len); writer.newLine(); writer.flush(); } reader.close(); writer.close();
}
} IO流(键盘录入学生信息按照总分排序并写入文本文件) public class Student implements Comparable<Student>{ private String name; private int chinese; private int math; private int English; private int all;
publicStudent() {
}
publicStudent(String name, int chinese, int math, int english, int all) {
this.name = name;
this.chinese = chinese;
this.math = math;
English = english;
this.all = all;
}
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
publicvoidsetChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
publicvoidsetMath(int math) {
this.math = math;
}
public int getEnglish() {
returnEnglish;
}
publicvoidsetEnglish(int english) {
English = english;
}
public int getAll() {
return all;
}
publicvoidsetAll(int all) {
this.all = all;
}
@OverridepublicStringtoString() {
return"学生: " + "姓名~┏" + name + '┒' +
", 语文~" + chinese +
", 数学~" + math +
", 英语~" + English +
",总分~" + all ;
}
@Overridepublic int compareTo(Student student) {
int num=-(this.all-student.all);
int num2=num==0?this.name.compareTo(student.name):num;
return num2;
}
}
public class text3 { / 3.A:案例演示: 需求:键盘录入3个学生信息( 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),/ public static void main(String[] args) throws IOException { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Student.txt")); Scanner scanner = new Scanner(System.in); TreeSet<Student> students = new TreeSet<>(); for (int i = 0, n = 1; i < 3; i++, n++) { Student student = new Student(); System.out.println("请输入第" + n + "个学生的姓名"); String name = scanner.nextLine(); student.setName(name); System.out.println("请输入第" + n + "个学生的语文成绩"); int chinese = scanner.nextInt(); student.setChinese(chinese); System.out.println("请输入第" + n + "个学生的数学成绩"); int math = scanner.nextInt(); student.setMath(math); System.out.println("请输入第" + n + "个学生的英语成绩"); int English = scanner.nextInt(); student.setEnglish(English); scanner = new Scanner(System.in); int num = chinese + math + English; student.setAll(num); students.add(student); }
for (Student s : students) {
String s1 = String.valueOf(s);
out.write(s1.getBytes());
out.write("\n\r".getBytes());
}
out.close();
}