新人做io的联系题。希望有人帮我指点一二,分不多,见谅。
希望说下我代码哪些地方可以改进。哪些地方可以用更高效的方法。
我是自学的。没老师教,都是自己看书看api整。
FileOut希望能做到连续的查找,但不知道怎么修改。如题,代码如下
package Score;import java.io.*;public class FileIn { /**
 * @param args
 */
public static void main(String[] args) {

String str1 = "";
int iStart = 0; try{
FileWriter fw1 = new FileWriter(new File("./src/Score/student.txt"),true);
BufferedWriter bw1 =new BufferedWriter(fw1);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br1 = new BufferedReader(isr);

if(br1.ready()!=true){
str1=br1.readLine();
if(str1.equals("sa")==true){
iStart = 1;
}
}
else{
isr.close();
System.exit(-1);
}
//System.out.println(iStart);
while((br1.ready()!=true)&&(iStart==1)){
System.out.println("请输入学生的学号");
str1 = br1.readLine();
if((str1.equals("exit")!=true)){
bw1.write(str1);
bw1.write(',');
System.out.println("请输入学生的姓名");
bw1.write(br1.readLine());
bw1.write(',');
System.out.println("请输入学生的成绩");
bw1.write(br1.readLine());
bw1.write(';');
bw1.newLine();
bw1.flush();
}
else{
System.out.println("退出中");
isr.close();
}
}

}
catch(IOException e){
System.out.println("程序已经关闭");
} }}——————————————————————————————————————————————————————
package Score;import java.io.*;
import java.util.*;public class FileOut { /**
 * @param args
 */
public static void main(String[] args) {
int iFirst = 0;
int iLast = 0;
String find = "";
String sTemp = "";
String name = "";
String id = "";
String score = "";
boolean result = false;
int i = 0;

ArrayList al_id = new ArrayList();
ArrayList al_name = new ArrayList();
ArrayList al_score = new ArrayList();
System.out.println("请输入您要查找的学生姓名");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

// TODO Auto-generated method stub
try {
FileReader fr = new FileReader(new File("./src/Score/student.txt"));
BufferedReader br1 = new BufferedReader(fr);
while(br1.ready()){
sTemp = br1.readLine();
//System.out.println(sTemp);
iFirst = sTemp.indexOf(',');
iLast = sTemp.lastIndexOf(',');
id = sTemp.substring(0, iFirst);
//System.out.println("iFirst:"+iFirst+"\n"+"iLast:"+iLast);

//System.out.println("id:"+id); name = sTemp.substring(iFirst+1, iLast);
//System.out.println(name);
score = sTemp.substring(iLast+1, sTemp.length()-1);
//System.out.println("score:"+score);
al_id.add(id);
al_name.add(name);
al_score.add(score); System.out.flush();
} try {
//while语句配合boolean变量result做循环直到条件为true为止。
while(result!=true){
find = br.readLine();


for(i=0;i<al_id.toArray().length;i++){
//System.out.print(al_id.toArray()[i]);
//System.out.print(" "+al_name.toArray()[i]);
//System.out.print(" "+al_score.toArray()[i]);
//System.out.print("\n");
//System.out.println(find);
//System.out.println(i);
if((find.equals(al_id.toArray()[i]))){
System.out.print(" "+al_name.toArray()[i]);
System.out.print(" "+al_score.toArray()[i]);
//System.out.print(al_id.toArray()[i].getClass());
System.out.print("\n");
result = true;
}
}
if(result!=true){
System.out.println("您输入的id无效");
}
}


} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }}

解决方案 »

  1.   

    代码有点多,没仔细看不过有个想法可给你建议下其实最好做个排序:比如查找成绩的时候,可以把所有信息查出来放到一个list里面,然后做一个排序,这样查找的时候就可以用 hash、或者 二分法来进行查找,速度应该比较快要不你每次查找都要进行i/o的重复很多次操作,i/O 很影响效率的。
      

  2.   

    我知道了
    while外面再加个
    while(br.ready()!=true){
       while{
         //
       }
       result = false;
    }
      

  3.   

    hash,二分法具体要看些什么内容?
    具体要在哪个类中去学习?
    谢谢你们了。
      

  4.   

    自学的吗。
    自学的话,不错了呢。
    不过还是有很多问题。
    例如输入时,没有判断输入的内容是什么
    如果手一抖按了ENTER,你那边就拿不到内容了
    又或者在输入score时,不小心输入了一个字母,那到时后面保存时就会存入错误的数据另外,从文件当中解析时,不需要用什么iFirst,iLast,改成这样吧 sTemp = br1.readLine();
    //System.out.println(sTemp);
                    String[] arr=sTemp.split(",");
                    id = arr[0];
                    name = arr[1];
                    score = arr[2];//当然,取值时最好判断一下arr的长度和当中的内容
    另外,应该定义一个学生的类public class Student{
     private long id;
     private String name;
     private int score;
     
    get set get set~//3个变量的get,set方法
    }
    这样一来, ArrayList al_id = new ArrayList();
            ArrayList al_name = new ArrayList();
            ArrayList al_score = new ArrayList();
    不需要3个list
    一个Array<Student> al_student = new ArrayList<Student>();
    就可以了
    保存时,相应的
    al_id.add(id);
                    al_name.add(name);
                    al_score.add(score);
    改为   Student student = new Student();
       student.setId(Long.parseLong(id));//注意判断id是否为空,是否为数字
       student.setName(name);//注意判断name是否为空
       student.setScore(Integer.parseInt(score));注意判断score是否为空,是否为数字
       al_student.add(student);
    连续查找指的是什么?可以考虑用正则 Pattern p = Pattern.compile("(\\d+),(\\w+),(\\d+)",Pattern.DOT_ALL);
      

  5.   

    笔误。
    一个ArrayList<Student> al_student = new ArrayList<Student>();Pattern p = Pattern.compile("(\\d+),(\\w+),(\\d+)",Pattern.DOT_ALL);
      

  6.   

    楼上的多些了。这个帖子我慢慢结。
    另外我想直接把。输出的txt改为xml格式文档                        bw1.write(str1);
                            bw1.write(',');
                            System.out.println("请输入学生的姓名");                    
                            bw1.write(br1.readLine());
                            bw1.write(',');
                            System.out.println("请输入学生的成绩");
                            bw1.write(br1.readLine());
                            bw1.write(';');
                            bw1.newLine();
                            bw1.flush(); 改为                        bs1.write("<student>")
                            bs1.write("<id>")
                            bw1.write(str1);
                            bw1.write("</id>"+"<name>");
                            System.out.println("请输入学生的姓名");                    
                            bw1.write(br1.readLine());
                            bw1.write("</name>"+"<score>");
                            System.out.println("请输入学生的成绩");
                            bw1.write(br1.readLine());
                            bw1.write("</score>");
                            bs1.write("</student>")
                            bw1.newLine();
                            bw1.flush();
    再做一个类把这样的xml复制到另一个xml中。
    前后加上
    <?xml version="1.0"?>
    <myPro>
    /*上一个xml内容*/
    </myPro>
    再用xhtml
    中的 xmlDoc()配合javascript(我现在就会一点javascript)
    将xml转换为假动态的html查询页面
      

  7.   

    正则表达式看了一天,回来联系下,分离html标签内容
      

  8.   

    <student><id>1</id><name>koj</name><score>88</score></student>
    这个用正则怎么分出来nodeValue?
    java正则,
    分出来koj 1 88三个string对象
      

  9.   


    String s = "<student><id>1</id><name>koj</name><score>88</score></student>";
    Pattern p = Pattern.compile("<([^\\>]*)>(.*)<\\/\\1>");
    Matcher m = p.matcher(s);
    int index = 0;
    while(m.find(index)){
    System.out.println(m.group(1)+"内容为:"+m.group(2));
    index = m.start()+1;
    }
    希望对你有帮助
      

  10.   


    这是一个标准的xml字符串直接用dom4j解析 比较方便,