else {
people[i] = new Person(field[0],
Integer.parseInt(field[1]), Double
.parseDouble(field[2]));
}
}
// 将输入的数据保存至“people.dat”文本文件中 。
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("people.dat")));
writeData(people, out);
// 关闭流 。
out.close();
// 从文件“people.dat”读取数据 。
BufferedReader in = new BufferedReader(new FileReader("people.dat"));
Person[] inPeople = readData(in);
// 关闭流 。
in.close();
// 输出从文件中读入的数据 。
for (int i = 0; iinPeople.length; i++) {
System.out.println(inPeople[i]);
}
} catch (IOException exception) {
System.err.println("IOException");
}
}
// 将所有数据写入输出流 。
static void writeData(Person[] p, PrintWriter out) throws IOException {
// 写入记录条数 , 即人数 。
out.println(p.length);
for (int i = 0; ip.length; i++) {
p[i].writeData(out);
}
}
// 将所有数据从输入流中读出 。
static Person[] readData(BufferedReader in) throws IOException {
// 获取记录条数,即人数 。
int n = Integer.parseInt(in.readLine());
Person[] p = new Person[n];
for (int i = 0; in; i++) {
p[i] = new Person();
p[i].readData(in);
}
return p;
}
}
class Person {
private String name;
private int age;
private double salary;
private String married;
public Person() {
}
public Person(String n, int a, double s) {
name = n;
age = a;
salary = s;
married = "F";
}
public Person(String n, int a, double s, String m) {
name = n;
age = a;
salary = s;
married = m;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getMarried() {
return married;
}
// 设置输出格式 。
public String toString() {
return getClass().getName() + "[name=" + name + ",age=" + age
+ ",salary=" + salary + ",married=" + married + "]";
}
// 写入一条记录,即一个人的数据到输出流 。
public void writeData(PrintWriter out) throws IOException {
// 格式化输出 。
out.println(name + ":" + age + ":" + salary + ":" + married);
}
// 从输入流读入一条记录,即一个人的数据 。
public void readData(BufferedReader in) throws IOException {
String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, ":");
name = t.nextToken();
age = Integer.parseInt(t.nextToken());
salary = Double.parseDouble(t.nextToken());
married = t.nextToken();
}
}
package IO;
import java.io.IOException;
public class FileStdRead {
public static void main(String[] args) throws IOException {
int b = 0;
char c = ' ';
System.out.println("请输入:");
while (c != 'q') {
int a = System.in.read();
c = (char) a;
【国外java代码例子经典 国外java英文视频教程】b++;
System.out.println((char) a);
}
System.err.print("counted\t" + b + "\ttotalbytes.");
}
}
//读取输入的数据,直到数据中有Q这个字母然
package IO;
import java.io.*;
public class IOStreamExample {
public static void main(String[] args) throws IOException {
// 1. 读入一行数据:
BufferedReader in = new BufferedReader(new FileReader(
"FileStdRead.java"));
String s, s2 = new String();
while ((s = in.readLine()) != null) {
s2 += s + "\n";
}
in.close();
BufferedReader stdin = new BufferedReader(new InputStreamReader(
推荐阅读
- b站锁直播间,b站直播间被锁定怎么回事
- qt中只能写c语言吗,qt可以编译c++吗
- 韩国kt服务器购买指南,韩国kt注册
- mysql表存到redis,把表中的所有数据放到redis
- php获取表格单元格数据 php获取数据库内容
- 老显卡导致没有声音怎么办的简单介绍
- 小程序退订蛋糕怎么退钱,小程序的订单怎么退
- 搞直播设备,直播设备有什么用
- linux中命令man linux中命令是如何执行的