加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 本地

java如何读取txt文件

时间:2024-03-03 22:07:30  来源:http://www.gzrxw.net  作者:admin

程序已经发你邮箱了,请注意查收噢。

---------------------------------------

public class Student {

String id;

String name;

int yuwen;

int shuxue;

int yingyu;

int avg;

int sum;

int sort;

public Student(String id,String name,int yuwen,int shuxue,int yingyu,int avg,int sum){

this.id=id;

this.name=name;

this.yuwen=yuwen;

this.shuxue=shuxue;

this.yingyu=yingyu;

this.avg=avg;

this.sum=sum;

}

public Student(String id,String name,int yuwen,int shuxue,int yingyu){

this.id=id;

this.name=name;

this.yuwen=yuwen;

this.shuxue=shuxue;

this.yingyu=yingyu;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getYuwen() {

return yuwen;

}

public void setYuwen(int yuwen) {

this.yuwen = yuwen;

}

public int getShuxue() {

return shuxue;

}

public void setShuxue(int shuxue) {

this.shuxue = shuxue;

}

public int getYingyu() {

return yingyu;

}

public void setYingyu(int yingyu) {

this.yingyu = yingyu;

}

public int getAvg() {

return avg;

}

public void setAvg(int avg) {

this.avg = avg;

}

public int getSum() {

return sum;

}

public void setSum(int sum) {

this.sum = sum;

}

public int getSort() {

return sort;

}

public void setSort(int sort) {

this.sort = sort;

}

}

------------------------

public class IOGrage {

public void swap(int[] array,int i,int j){

int temp=array[i];

array[i]=array[j];

array[j]=temp;

}

public static void main(String[] args) {

BufferedReader br = null;

List list = new ArrayList();

try {

br = new BufferedReader(new InputStreamReader(new FileInputStream(

new File(C:/grade.txt))));

String temp = br.readLine();

while (temp != null) {

temp = temp.replaceAll( , ,);

StringBuffer sbTemp = new StringBuffer();

String[] each = temp.split(,);

for (int i = 0; i < each.length; i++) {

if (!each[i].equals()) {

sbTemp.append(each[i] + ,);

}

}

each = sbTemp.toString().split(,);

if (!学号.equals(each[0])) {

String id = each[0];

String name = each[1];

int yuwen = Integer.parseInt(each[2]);

int shuxue = Integer.parseInt(each[3]);

int yingyu = Integer.parseInt(each[4]);

int avg = (yuwen + shuxue + yingyu) / 4;

int sum = yuwen + shuxue + yingyu;

Student s = new Student(id, name, yuwen, shuxue, yingyu,

avg, sum);

list.add(s);

}

temp = br.readLine();

}

br.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//System.out.println(list.size());

int[] array=new int[list.size()];

for(int i=0;iStudent s=(Student)list.get(i); array[i]=s.getSum(); } for(int i=0;i for(int j=0;j if(array[j] int t=array[j]; array[j]=array[j+1]; array[j+1]=t; } } } Map map=new HashMap(); for(int i=0;i map.put(array[i], i+1); } List lList=new ArrayList(); for(int i=0;i Student s=(Student)list.get(i); if(s.getName().length()==2){ s.setName(s.getName()+ ); }else if(s.getName().length()==3){ s.setName(s.getName()+ ); } if(s.getId().length()==1){ s.setId(s.getId()+ ); }else if(s.getId().length()==2){ s.setId(s.getId()+ ); } int sum=s.getSum(); int sort=((Integer)map.get(sum)).intValue(); s.setSort(sort); lList.add(s); } StringBuffer sb=new StringBuffer(学号 姓名 语文 数学 英语 平均值 总值 排序); for(int i=0;i Student s=(Student)lList.get(i); sb.append(\n+s.getId()+ +s.getName()+ +s.getYuwen()+ +s.getShuxue()+ +s.getYingyu()+ +s.getAvg()+ +s.getSum()+ +s.getSort()); } BufferedWriter bw=null; try{ bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(c:/grade.txt))); bw.write(sb.toString()); bw.close(); }catch(Exception e ){ e.printStackTrace(); } } }

来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
相关文章
    无相关信息
栏目更新
栏目热门