在Java中如何使用控制台输入将输入的数据存储到文本文件中------Java初学者
2个回答
展开全部
public class Student {
public int id;
public String name;
public String gender;
public int age;
public String classteam;
public Student(int id,String name,String gender,int age,String classteam){
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.classteam = classteam;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String isGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassteam() {
return classteam;
}
public void setClassteam(String classteam) {
this.classteam = classteam;
}
public String toString()
{
return "id="+id+",name="+name+",sex="+gender+",age="+age+",class="+classteam;
}
}
*************************************
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class StudentManager {
private final static int MAX_SIZE = 10;
private int length;
public StudentManager(){
length = 0;
}
public void display() throws IOException{
/**
* 采用控制台动态输入数据
*/
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\学生情况.txt"));
Student[] stu=new Student[10];
String s1,s2,s3,s4,s5;
while(length < MAX_SIZE)
{
System.out.println("学生学号:");
s1=br.readLine();
if(s1.length()==0)
break;
System.out.println("学生姓名:");
s2=br.readLine();
if(s2.length()==0)
break;
System.out.println("学生性别:");
s3=br.readLine();
if(s3.length()==0)
break;
System.out.println("学生年龄:");
s4=br.readLine();
if(s4.length()==0)
break;
System.out.println("学生所在班级:");
s5=br.readLine();
if(s5.length()==0)
break;
bw.write(s1);
bw.write(" ");
bw.write(s2);
bw.write(" ");
bw.write(s3);
bw.write(" ");
bw.write(s4);
bw.write(" ");
bw.write(s5);
int mid=Integer.parseInt(s1);
int mage=Integer.parseInt(s4);
stu[length]=new Student(mid,s2,s3,mage,s5);
bw.newLine();//产生新行
length++;
}
bw.close();
for(int i=0;i<10;i++)
System.out.println(stu[i]);
}
}
**********************************
class test
{
public static void main(String [] args) throws Exception
{
StudentManager SM=new StudentManager();
SM.display();
}
}
public int id;
public String name;
public String gender;
public int age;
public String classteam;
public Student(int id,String name,String gender,int age,String classteam){
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.classteam = classteam;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String isGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassteam() {
return classteam;
}
public void setClassteam(String classteam) {
this.classteam = classteam;
}
public String toString()
{
return "id="+id+",name="+name+",sex="+gender+",age="+age+",class="+classteam;
}
}
*************************************
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class StudentManager {
private final static int MAX_SIZE = 10;
private int length;
public StudentManager(){
length = 0;
}
public void display() throws IOException{
/**
* 采用控制台动态输入数据
*/
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\学生情况.txt"));
Student[] stu=new Student[10];
String s1,s2,s3,s4,s5;
while(length < MAX_SIZE)
{
System.out.println("学生学号:");
s1=br.readLine();
if(s1.length()==0)
break;
System.out.println("学生姓名:");
s2=br.readLine();
if(s2.length()==0)
break;
System.out.println("学生性别:");
s3=br.readLine();
if(s3.length()==0)
break;
System.out.println("学生年龄:");
s4=br.readLine();
if(s4.length()==0)
break;
System.out.println("学生所在班级:");
s5=br.readLine();
if(s5.length()==0)
break;
bw.write(s1);
bw.write(" ");
bw.write(s2);
bw.write(" ");
bw.write(s3);
bw.write(" ");
bw.write(s4);
bw.write(" ");
bw.write(s5);
int mid=Integer.parseInt(s1);
int mage=Integer.parseInt(s4);
stu[length]=new Student(mid,s2,s3,mage,s5);
bw.newLine();//产生新行
length++;
}
bw.close();
for(int i=0;i<10;i++)
System.out.println(stu[i]);
}
}
**********************************
class test
{
public static void main(String [] args) throws Exception
{
StudentManager SM=new StudentManager();
SM.display();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询