package IO;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.StringTokenizer;
public class FileRw {
public void inputPerson(int Number) throws IOException{
int fieldcount=0;
System.out.println("----------需要输入"+Number+"名学生的信息---------");
Person[] people=new Person[Number];
String[] stringfield=new String[4];
for(int i=0;i<Number;i++){
//String[] stringfield=new String[4];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请分别输入姓名、年龄、工资(月)、婚姻(是/否)以“:”为间隔:");
StringTokenizer str=new StringTokenizer(in.readLine(),":");
while(str.hasMoreTokens()){
stringfield[fieldcount]=str.nextToken();
fieldcount++;
}
fieldcount=0;
if(stringfield[3]!=" ")
people[i]=new Person(stringfield[0],Integer.parseInt(stringfield[1]),Double.parseDouble(stringfield[2]),stringfield[3]);
else{
people[i]=new Person(stringfield[0],Integer.parseInt(stringfield[1]),Double.parseDouble(stringfield[2]));
}
}
PrintWriter out=new PrintWriter("f://imooc//people.txt");
for(int i=0;i<people.length;i++){
out.write("姓名:"+people[i].name+" "+"年龄"+people[i].age+" "+people[i].salary+" "+people[i].marriage);
out.println();
out.flush();
}
out.close();
BufferedReader in1=new BufferedReader(new InputStreamReader(
new FileInputStream("f://imooc//people.txt")));
String s;
while((s=in1.readLine())!=null){
System.out.println(s);
}
in1.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileRw f=new FileRw();
f.inputPerson(3);
}
class Person{
private String name;
private int age;
private double salary;
private String marriage;
public Person(){
}
public Person(String n,int a,double s,String m){
name=n;
age=a;
salary=s;
marriage=m;
}
public Person(String n,int a,double s){
name=n;
age=a;
salary=s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getMarriage() {
return marriage;
}
public void setMarriage(String marriage) {
this.marriage = marriage;
}
}
}
JAVA IO输入输出流部分代码的入门应用
最新推荐文章于 2024-08-23 15:54:50 发布