IO流内容整理

发布时间:2020-05-26 00:22:08 作者:龙逸尘
来源:网络 阅读:241

IO流概述
IO流用来处理设备之间的数据传输
Java对数据的操作是通过流的方式
Java用于操作流的对象都在IO包中
IO流分类
按照数据流向
输入流 读入数据
输出流 写出数据
按照数据类型
字节流 可以读写任何类型的文件 比如音频 视频 文本文件
字符流 只能读写文本文件
什么情况下使用哪种流呢?
如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。
如果你什么都不知道,就用字节流
IO流基类概述
a:字节流的抽象基类:
InputStream ,OutputStream。
b:字符流的抽象基类:
Reader , Writer。
注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
如:InputStream的子类FileInputStream。
如:Reader的子类FileReader。
FileOutputStream的构造方法,
FileOutputStream(File file)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流
FileOutputStream(File file, boolean append)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(FileDescriptor fdObj)
创建一个向指定文件描述符处写入数据的输出文件流,该文件描述符表示一个到文件 系统中的某个实际文件的现有连接。
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append)
创建一个向具有指定 name 的文件中写入数据的输出文件流。
使用具体子类FileOutputStream
Io流的分类:

字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流。

字符流: 字符流 = 字节流 + 编码表
编码: 就是把字符串转换成字节数组

高效的流一次一个字符数组
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;

public Student() {
}

public Student(String name, int chinese, int math, int english, int all) {
    this.name = name;
    this.chinese = chinese;
    this.math = math;
    English = english;
    this.all = all;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getChinese() {
    return chinese;
}

public void setChinese(int chinese) {
    this.chinese = chinese;
}

public int getMath() {
    return math;
}

public void setMath(int math) {
    this.math = math;
}

public int getEnglish() {
    return English;
}

public void setEnglish(int english) {
    English = english;
}

public int getAll() {
    return all;
}

public void setAll(int all) {
    this.all = all;
}

@Override
public String toString() {
    return "学生: " + "姓名~┏" + name + '┒' +
            ", 语文~" + chinese +
            ", 数学~" + math +
            ", 英语~" + English +
            ",总分~" + all ;
}

@Override
public 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();

}

}

推荐阅读:
  1. 04.JavaIO流问题
  2. C位运算笔记(根据网上内容整理)1

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

io流

上一篇:Oracle菜鸟学习之在RedHat 6.5上安装Oracle

下一篇:spring boot 配置druid

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》