如何读取含有字符串和数字的txt文件
1个回答
2016-08-17 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class Test {
private static BufferedReader bw;
public static void main(String[] args) {
String ss = null;
List<TestPo> list = new ArrayList<TestPo>();
try{
File file=new File("E:\\test.txt");
FileOutputStream out=new FileOutputStream(new File("E:\\test1.txt"),true);
bw=new BufferedReader(new FileReader(file));
boolean flag = true;
int[] temps = null;
while((ss=bw.readLine())!=null){
String[] mark = ss.split(":");
int sum =0;
TestPo po = new TestPo();
int[] temp = new int[mark.length-1];
temps = new int[mark.length-1];
for (int i = 1;i< mark.length;i++) {
temp[i-1] = Integer.parseInt(mark[i].trim());
sum += temp[i-1];
if (flag) {
temps[i-1] = 0;
}
}
flag = false;
po.setMark(temp);
po.setName(mark[0]);
po.setAvg(sum/(mark.length-1));
list.add(po);
out.write(("\t\n"+ss).getBytes());
}
bw.close();
out.close();
int row = list.size();
int[][] marks = new int[row+1][];
for (int i = 0; i < row; i++) {
marks[i] = list.get(i).getMark();
}
marks[row] = temps;
int sumTemp =0 ;
int count = list.get(0).getMark().length;
for (int i = 0; i < count; i++) {
for (int j = 0; j < row; j++) {
sumTemp += marks[j][i];
}
marks[row][i] = sumTemp/row;
sumTemp = 0;
}
for (int[] is : marks) {
for (int i : is) {
System.out.print(i+" ");
}
System.out.println();
}
}catch(IOException e){
e.printStackTrace();
}
for (TestPo po : list) {
System.out.println("Name:"+po.getName()+" AVG:"+po.getAvg());
}
}
}
class TestPo{
private String name;
private Integer avg;
private int[] mark;
public int[] getMark() {
return mark;
}
public void setMark(int[] mark) {
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAvg() {
return avg;
}
public void setAvg(Integer avg) {
this.avg = avg;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class Test {
private static BufferedReader bw;
public static void main(String[] args) {
String ss = null;
List<TestPo> list = new ArrayList<TestPo>();
try{
File file=new File("E:\\test.txt");
FileOutputStream out=new FileOutputStream(new File("E:\\test1.txt"),true);
bw=new BufferedReader(new FileReader(file));
boolean flag = true;
int[] temps = null;
while((ss=bw.readLine())!=null){
String[] mark = ss.split(":");
int sum =0;
TestPo po = new TestPo();
int[] temp = new int[mark.length-1];
temps = new int[mark.length-1];
for (int i = 1;i< mark.length;i++) {
temp[i-1] = Integer.parseInt(mark[i].trim());
sum += temp[i-1];
if (flag) {
temps[i-1] = 0;
}
}
flag = false;
po.setMark(temp);
po.setName(mark[0]);
po.setAvg(sum/(mark.length-1));
list.add(po);
out.write(("\t\n"+ss).getBytes());
}
bw.close();
out.close();
int row = list.size();
int[][] marks = new int[row+1][];
for (int i = 0; i < row; i++) {
marks[i] = list.get(i).getMark();
}
marks[row] = temps;
int sumTemp =0 ;
int count = list.get(0).getMark().length;
for (int i = 0; i < count; i++) {
for (int j = 0; j < row; j++) {
sumTemp += marks[j][i];
}
marks[row][i] = sumTemp/row;
sumTemp = 0;
}
for (int[] is : marks) {
for (int i : is) {
System.out.print(i+" ");
}
System.out.println();
}
}catch(IOException e){
e.printStackTrace();
}
for (TestPo po : list) {
System.out.println("Name:"+po.getName()+" AVG:"+po.getAvg());
}
}
}
class TestPo{
private String name;
private Integer avg;
private int[] mark;
public int[] getMark() {
return mark;
}
public void setMark(int[] mark) {
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAvg() {
return avg;
}
public void setAvg(Integer avg) {
this.avg = avg;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询