java编程:编写一个文件信息存储程序,用户通过键盘输入学生的姓名,性别,班级,地址等信息,

要求编写一个java程序,输入学生信息,并能保存与显示学生信息。~

import java.util.ArrayList;
import java.util.Scanner;import com.sun.org.apache.xpath.internal.Arg;
public class Student {
//两个私有属性
private int no ;
private String name ;
//默认构造函数
public Student(){}
//带参构造函数
public Student(int no,String name){
this.no=no;
this.name=name;
}
//添加学生信息
public void addStudentInfo(ArrayList list){
String flag1="yes";
do{
Student student=new Student();
Scanner in =new Scanner(System.in);
System.out.println("请输入学生学号:");
student.setNo(in.nextInt());
System.out.println("请输入学生姓名:");
student.setName(in.next());
list.add(student);
System.out.println("是否继续添加学生信息(yes/no)?");
flag1=in.next();
} while(flag1.equals("yes"));

}
//读取学生信息
public void showStudentInfo(ArrayList list){
System.out.println(" 学生no: "+" 学生name: ");
for(Student student:list){
System.out.println(" "+student.getNo()+" "+student.getName());
}
}

public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public static void main(String arg[]) {

ArrayList studentList= new ArrayList();
Student student= new Student();
Scanner inn= new Scanner(System.in);
while(true){
System.out.println("----------------- 欢迎使用学生管理系统------------------");
System.out.println("1.输入学生信息");
System.out.println("2.显示学生信息");
System.out.println("0.退出系统");
System.out.println("请选择....");
int nn= inn.nextInt();
switch (nn) {
case 1:
student.addStudentInfo(studentList);
break;
case 2:
System.out.println("学生信息如下:");
student.showStudentInfo(studentList);
break;
case 0:
System.exit(0);
System.out.println("系统退出!");
break;
default:
break;
}
}
} }

public class Util { public static void main(String[] args) throws java.io.IOException{ //编写一个JAVA程序,将自己的个人信息(学号、姓名、性别等)保存到c:\myinfo.txt文件中。 Person p1 = new Person("0001","小明",22); java.io.File file = new java.io.File("C:\\myinfo.txt"); java.io.BufferedWriter bw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(file))); bw.write("学号:" + p1.id); bw.write("
姓名:" + p1.name); bw.write("
性别:" + p1.age); bw.flush(); bw.close(); }}class Person{ public String id; public String name; public int age; public Person(String id, String name, int age){ this.id = id; this.name = name; this.age = age; }}
程序有异常,还有判断文件是否存在我也没处理,没运行,请自行尝试。。。

//test.java

import java.io.*;
import java.util.*;
public class test {
public static void main (String[] args) {
try{
String strName="";
String strSex="";
int i=1;
//构造写入文件的File对象
File file2=new File("a.txt"); //创建一个a.txt文件
//构造低层输出流
FileWriter fw=new FileWriter(file2);
//构造高层输出流
BufferedWriter bw=new BufferedWriter(fw);
while(true){
System.out.println ("请输入第"+i+"学生姓名:");
Scanner sc = new Scanner(System.in);
strName = "姓名:"+sc.next();
System.out.println ("请输入性别:");
Scanner sc2 = new Scanner(System.in);
strSex = "性别"+sc2.next();
//写入文件
bw.write(strName+" "+strSex);
bw.newLine(); //换行
System.out.println ("是否结束程序:y/n");
Scanner sc3 = new Scanner(System.in);
String str = sc3.next();
if(str.equals("y"))break; //如果输入的是y则退出程序
i++;

}
//关闭流
bw.close();
fw.close();
}catch(IOException ex1){
System.out.println ("输入输出异常");
}
System.out.println ("程序结束");
}
}

本人刚入手java 可能写得不是很好.. 希望能给你带来帮助..
不足之处,敬请原谅!

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class StuFile1 {

public static void main(String[] args) throws IOException {

Scanner s = new Scanner(System.in);
FileOutputStream fOS = new FileOutputStream(
"F:/java/window/BaiduQuestion/src/stufile/a.txt");

while (true) {
System.out.println("姓名:");
fOS.write(("姓名:" + s.next()).getBytes());
System.out.println("性别:");
fOS.write((" 性别:" + s.next()).getBytes());
System.out.println("exit 退出");
if (s.next().equalsIgnoreCase("exit")) {
s.close();
fOS.close();
System.exit(0);
}
}

}
}

只是这个写文件你就得导好多类,还有界面,又要好多类,估计要写几千行。

用ObjectOutputStream就可以实现了。
定义一个学生的类,信息通过控制台输入,然后用ObjectOutputStream写到文件里去。
自己先试着做一下吧。
碰到技术问题再来问。

java编程:编写一个文件信息存储程序,用户通过键盘输入学生的姓名,性 ...
答:import java.util.*;public class test { public static void main (String[] args) { try{ String strName="";String strSex="";int i=1;//构造写入文件的File对象 File file2=new File("a.txt"); //创建一个a.txt文件 //构造低层输出流 FileWriter fw=new FileWriter(file2);//构...

编写java程序,往一个txt文件里写入学生的基本信息,然后再读出这些信息...
答:import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;/ 编写java程序,往一个txt文件里写入学生的基本信息,然后再读出这些信息并打印出来,最后把该文件拷贝到指定位置并在文件名前加入日期信息...

编写一个JAVA程序,将自己的个人信息(学号、姓名、性别等)保存到c:\...
答:public class Util { public static void main(String[] args) throws java.io.IOException{ //编写一个JAVA程序,将自己的个人信息(学号、姓名、性别等)保存到c:\myinfo.txt文件中。 Person p1 = new Person("0001","小明",22); java.io.File file = new java.io.File("C:...

编写一个JAVA程序,将自己的个人信息(学号、姓名、性别等)保存到c:\...
答:import java.io.*;import java.util.*;/** * @author hardneedl */public class SaveFileDemo { public static void main(String... args) throws FileNotFoundException { Scanner scanner = new Scanner(System.in); String[] questions = new String[]{ "姓名", "学号", ...

求大神帮忙编写一个Java程序显示指定目录中所有的文件和目录,包括文 ...
答:package Test2;import java.io.File;public class TestFile {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFile file = new File("D:\\Java\\");printFile(file,0);}/** * 输出文件树 * @param file * @param l */public static...

编写一个Java应用程序,实现将一个由英文字母、数字及其他符号构成的文件...
答:import java.io.IOException;public class jiami { public static void main(String[] args) { String source=null,target=null;try { FileInputStream fileread = new FileInputStream(new File("D:/a.txt"));//路径自己改 int length = fileread.available();byte[] buffer = new byte[...

使用JAVA编写一个简单的银行存取款程序
答:另一个构造方法带4个参数分别初始化4个属性(带数据有效性验证)。/ public Account(String id, int password, String name, double balence) { this.id = id;this.password = password;this.name = name;this.balence = balence;} / 查询余额 / public static double selectMoney(Account account...

用java编写程序 1.建立一个文本文件,输入英语短文.编写一个程序,统计...
答:Read { public static void main(String[] args) throws Exception { long size = readFileByChars("D://test.txt"); write("D://test1.txt",size); } public static long readFileByChars(String fileName) { File file = new File(fileName); Reader reader = ...

用JAVA 编写一个程序 如果test.TXT不存在,以该名创建一个文件,如果该文...
答:输出流默认就是文件不存在就创建一个为了清楚我直接抛异常了 随机数没有范围和正负之分,不知道你们有没有要求 哪里有问题再问我 public static void main(String[] args) throws Exception{ File f=new File("D://test.txt");FileWriter fw=new FileWriter(f);Random r=new Random();int sum=...

java 创建文件的实例 并写入信息
答:OutputStreamWriter pw = null;//定义一个流 pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例 pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write pw.close();//关闭流...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网