java大文件正则匹配_8-1 jmu-java-流、文件与正则表达式

0. 字节流与二进制文件

主函数

package student;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class MainStream {

public static void main(String[] args)

{

String fileName="student.data";

//写入

try(DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName))){

Student stu1=new Student(50,"李",18,12);

dos.writeInt(stu1.getId());

dos.writeUTF(stu1.getName());

dos.writeInt(stu1.getAge());

dos.writeDouble(stu1.getGrade());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

//读出

try(DataInputStream dis=new DataInputStream(new FileInputStream(fileName))){

int id=dis.readInt();

String name=dis.readUTF();

int age=dis.readInt();

double grade=dis.readDouble();

Student stu=new Student(id,name,age,grade);

System.out.println(stu);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

运行结果如下

65eb9f97674297ca765d25b1731d016c.png

1. 字符流与文本文件:使用 PrintWriter(写),BufferedReader(读)

1.使用BufferedReader从编码为UTF-8的文本文件中读出学生信息,并组装成对象然后输出。

package student;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Arrays;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

String FILENAME="Students.txt";

try(BufferedReader br=new BufferedReader(new FileReader(new File(FILENAME))) ;)

{

String line=null;

while((line=br.readLine())!=null)

{

String []x=line.split("\\s+");

int id=Integer.parseInt(x[0]);

String name=x[1];

int age=Integer.parseInt(x[2]);

double grade=Double.parseDouble(x[3]);

Student stu=new Student(id,name,age,grade);

System.out.println(stu);

/*for(String e:x)

{

System.out.println(e);

}*/

}</

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值