有一个.CVS文件,里面包含数据
学号,姓名,语文,数学,外语,物理,化学
001,ZhangSan,86,70,84,93,78
002,LiSi,92,85,67,83,91
怎样从中读出数据,是不是首先应该先读取文件,然后再读取数据?
有哪位高手能帮忙解决问题吗,十分感激啊!!!

解决方案 »

  1.   

    readLine() 读一整行
    第一行不要
    读到第一行以后的第一行时 用split(",") 分割一下
      

  2.   

    import java.io.File;
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.Sheet;
    import jxl.Cell;public class ExcelParser {  public static void getExcelContent(String fileName) throws Exception {
        Workbook rwb = null;
        WorkbookSettings wse = new WorkbookSettings();
        wse.setEncoding("ISO-8859-1");
        rwb = Workbook.getWorkbook(new File(fileName), wse);
        Sheet[] sheets = rwb.getSheets();
        Sheet excelSheet = sheets[0];
        Cell cjm = null;
        for (int j = 1; j < excelSheet.getRows(); j++) {
            for (int m = 0; m < excelSheet.getColumns(); m++) {
              cjm = excelSheet.getCell(m, j);
              System.out.println(cjm.getContents());
            }
        }
        rwb.close();
      }
      
      public static void main(String args[]) throws Exception {
      ExcelParser.getExcelContent("C:/study.xls");
      }
    }
      

  3.   

    不能在文件中排序
    你只能先全部读取出来存入List或其它,然后按照某个字段进行排序
      

  4.   

    是啊,具体怎么做啊,我是刚学java,有些东西还不是很懂,希望各位帮帮忙了·
      

  5.   

    定义一个类 以文件中的项为属性 然后从文件中readLine()读出数据 然后构造这个类的对象
    并加入到List中 自定义按学号进行排序
    class Stu implement Comparable{//可以实现Comparable接口 依据学号排序比较大小
        private String num;
        private String name;
        private double c;
        private double s;
        private double w;
        private double wl;
        private double h;
        get...() {}
        set...() {}
        int compareTo(Stu stu) {...}
    }
    public class ReadStu {
        static List stuList=new ArrayList();
        public void inOrder(List list) {...}//对stuList中的stu排序
        public static void main(String []args) {
            BufferedReader br=new BufferedReader(FileReader("filename"));
            StringTokenizer tokens;
            String []str=new String[7];
            Stu student=new Stu();        String stu=br.readLine();       
            while(stu!=null) {
                tokens=new StringTokenizer(stu,",");
                for(int i=0;tokens.hasMoreTokens()&&i<7;i++){
                    str[i]=tokens.nextToken();
                }
                
                for(int i=0;i<7;i++) {
                     student.set...(str[i]);
                }
                stuList.add(student);
            }
        }}
    不知我理解的对不对 呵呵 只是个想法 希望对你有用
      

  6.   

    while(stu!=null) {
        ...
        ...
        stu=br.readLine();//落一句话 呵呵
    }
      

  7.   

    给你一个完整的代码:import java.util.List;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    import java.io.File;
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.Sheet;
    import jxl.Cell;public class ExcelParser {  public static HashMap getExcelContent(String fileName) throws Exception {
      HashMap dataMap = new HashMap();
        Workbook rwb = null;
        WorkbookSettings wse = new WorkbookSettings();
        wse.setEncoding("ISO-8859-1");
        rwb = Workbook.getWorkbook(new File(fileName), wse);
        Sheet[] sheets = rwb.getSheets();
        Sheet excelSheet = sheets[0];    String[] sNos = new String[excelSheet.getRows() - 1];
        for (int j = 1; j < excelSheet.getRows(); j++) {
         sNos[j-1] = excelSheet.getCell(0, j).getContents();
    Score score = new Score();
    score.setStudentNo(excelSheet.getCell(0, j).getContents());
    score.setStudentName(excelSheet.getCell(1, j).getContents());
    score.setScore1(excelSheet.getCell(1, j).getContents());
    score.setScore2(excelSheet.getCell(2, j).getContents());
    score.setScore3(excelSheet.getCell(3, j).getContents());
    score.setScore4(excelSheet.getCell(4, j).getContents());
    score.setScore5(excelSheet.getCell(5, j).getContents());
    dataMap.put(score.getStudentNo(), score);
        }
        rwb.close();
        
        Arrays.sort(sNos);
        for (int i = 0; i < sNos.length; i++) {
          Score score = (Score)dataMap.get(sNos[i]);
          System.out.println(score.getStudentNo() + " " + 
          score.getStudentName() + " " + 
          score.getScore1() + " " + 
          score.getScore2() + " " + 
          score.getScore3() + " " + 
          score.getScore4() + " " + 
          score.getScore5());
        }
        return dataMap;
      }
      
      public static void main(String args[]) throws Exception {
        ArrayList x = new ArrayList();
        ExcelParser.getExcelContent("C:/study.csv");
      }
    }class Score {
    private String studentNo;
    private String studentName;
    private String score1;
    private String score2;
    private String score3;
    private String score4;
    private String score5;

    public void setStudentNo(String studentNo) {
      this.studentNo = studentNo;
    }

    public void setStudentName(String studentName) {
      this.studentName = studentName;
    }

    public void setScore1(String score1) {
      this.score1 = score1;
    }

    public void setScore2(String score2) {
      this.score2 = score2;
    }

    public void setScore3(String score3) {
      this.score3 = score3;
    }

    public void setScore4(String score4) {
      this.score4 = score4;
    }

    public void setScore5(String score5) {
      this.score5 = score5;
    }  

    public String getStudentNo() {
      return this.studentNo;
    }

    public String getStudentName() {
      return this.studentName;
    }

    public String getScore1() {
      return this.score1;
    }

    public String getScore2() {
      return this.score2;
    }

    public String getScore3() {
      return this.score3;
    }

    public String getScore4() {
      return this.score4;
    }

    public String getScore5() {
      return this.score5;
    }
    }
      

  8.   

    首先谢谢各位了,这是我写的程序
    package com.nengxiao.ryfang.readandwrite;
    import java.io.*;/**
     * 功能:从CSV文件中读取数据进行相应的操作然后,然后写入该文件
     * @author yyfang
     *
     */
    public class ReadAndWriteFile
    {
    public void listOfFunction()
    {
    System.out.println("表示  "+"排序表示     "+"新规输入     "+"变更     "+"删除");
    //System.out.println("排序表示");
    //System.out.println("新规输入");
    //System.out.println("变更");
    //System.out.println("删除");
    }

    /**
     * 判断文件是否存在
     * @return 存在返回true,不存在返回false;
     */
    public boolean isExists(String file)
    {
    File newFile=new File(file);
    if(newFile.exists())
    {
    System.out.println("该文件存在");
    return true;
    }
    else
    {
    System.out.println("该文件不存在");
    return false;
    }
    }

    /**
     * 判断文件内容是否为空
     * @return 为空返回false,不为空返回true;
     * @throws IOException 
     */
    public Boolean isEmpty(String file) throws IOException
    {
    FileReader newFile=new FileReader(file);
    //文件字符为空
    if(newFile.read()==-1)
    {
    System.out.println("文件为空");
    return false;
    }
    else
    {
    System.out.println("文件不为空");
    return true;
    }
    }

    /**
     * 从指定的文件中读取数据
     * @return 
     * @return 读取成功返回true,否则返回false
     * @throws IOException 
     * @throws IOException 
     */
    public void dataOfRead(String path) throws IOException
    {
    //输入缓冲流
    BufferedReader input = new BufferedReader(new FileReader(path));
    String line = "";
    /**
     * 当读取的一行不为空时,进行输出
     */
    while((line=input.readLine()) != null)
    {
    String array[]=line.split(",");
    if(line.length()!= 0)
    {
    for(int i=0;i<array.length;i++)
    {
    System.out.print(array[i]+"\t");
    }
    System.out.println();
    }
    }
    }
    public Object dataOfList() 
    {
    // TODO 自动生成方法存根
    return null;
    }

    /**
     * 功能:将CSV文件中的数据进行按学号排序
     * @throws IOException 
     */
    public void sortByStudentNumber(String path) throws IOException
    {
    BufferedReader input=new BufferedReader(new FileReader(path));
    String line="";
    while((line=input.readLine())!=null)
    {
    ///创建一个二维数组用来存放字段,然后输出
    for(int i=0; ;i++)
    for(int j=0; ;j++)
    {
    char[][] array=new char[i][j];
    char[i][0]= line.split(",");
    }
    if(line.length()!= 0)
    {
    for(int i=0;i<array.length-1;i++)
    for(int j=i+1;j<array.length;j++)
    {
    if(Integer.valueOf(array[i])>Integer.valueOf(array[j]))
    {
    String[] temp=array[i][0];
    array[j][0]=array[i][0];
    array[i][0]=temp;
    }
    }
    for(int i=0;i<array.length;i++)
    {
    System.out.print(array[i]+"\t");
    }
    System.out.println();
    }
    }
    }
    我用的是每行读取,他是读取一行,那我怎么把学号这个字段分开,然后进行排序,谢谢各位了,急于解决。。
      

  9.   

    这个程序的sortByStudentNumber这个函数有错误,希望各位帮忙解决一下
      

  10.   

    仔细看看,小小的功能里可以用到很多“java技术”,读文件、反射、java排序等等。
    以下是我的一个实现,能够按 “学号,姓名,语文,数学,外语,物理,化学”中任意一个条件排序import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;public class Test
    {
        public static void main(String[] args)
        {
            Test test = new Test();
            List<Score> list = test.loadScore();
            if (list != null)
            {
                Order order = new Order("math", true);
                Collections.sort(list, order);
                for (Score s : list)
                {
                    System.out.println(s);
                }
            }
        }    private List<Score> loadScore()
        {
            try
            {
                File file = new File("D://score.txt");
                List<Score> list = new ArrayList<Score>();
                Score score = null;
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line = "";
                while ((line = br.readLine()) != null)
                {
                    score = parseToScore(line);
                    if (score != null)
                    {
                        list.add(score);
                    }
                }
            br.close();
            return list;
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return null;
            }
        }    private Score parseToScore(String str)
        {
            if (str == null || str.equals("")) return null;
            String[] someoneScores = str.split(",");
            if (someoneScores == null || someoneScores.length != 7) return null;        Score score = new Score();
            score.setId(someoneScores[0]);
            score.setName(someoneScores[1]);
            score.setChn(new Integer(someoneScores[2]));
            score.setMath(new Integer(someoneScores[3]));
            score.setEng(new Integer(someoneScores[4]));
            score.setPhy(new Integer(someoneScores[5]));
            score.setChe(new Integer(someoneScores[6]));        return score;
            }
        }    class Order implements Comparator<Score>
        {
            private String orderBy = "id";// 排序条件
            private boolean reverse = false;// 是否倒序        public Order(String str)
            {
                this.orderBy = str;
            }        public Order(String str, boolean reverse)
            {
                this.orderBy = str;
                this.reverse = reverse;
            }        public int compare(Score s1, Score s2)
            {
                try
                {
                    Method method = Score.class.getMethod(
                                getMethodName(orderBy), new Class[] {});                Object obj1 = method.invoke(s1, new Object[] {});
                    Object obj2 = method.invoke(s2, new Object[] {});                if (reverse)
                    {
                        return obj2.toString().compareTo(obj1.toString());
                    }
                    return obj1.toString().compareTo(obj2.toString());
                }
                catch (Exception e)
                {
                    return 0;
                }
            }    private String getMethodName(String name)
        {
            String methodName = "get";
            methodName += name.substring(0, 1).toUpperCase() + name.substring(1);
            return methodName;
        }
    }class Score
    {
        private String id;// 学号
        private String name;// 姓名
        private Integer chn;// 语文
        private Integer math;// 数学
        private Integer eng;// 外语
        private Integer phy;// 物理
        private Integer che;// 化学    public Integer getChe()
        {
            return che;
        }    public void setChe(Integer che)
        {
            this.che = che;
        }    public Integer getChn()
        {
            return chn;
        }    public void setChn(Integer chn)
        {
            this.chn = chn;
        }    public Integer getEng()
        {
            return eng;
        }    public void setEng(Integer eng)
        {
            this.eng = eng;
        }    public String getId()
        {
            return id;
        }    public void setId(String id)
        {
            this.id = id;
        }    public Integer getMath()
        {
            return math;
        }    public void setMath(Integer math)
        {
            this.math = math;
        }    public String getName()
        {
            return name;
        }    public void setName(String name)
        {
            this.name = name;
        }    public Integer getPhy()
        {
            return phy;
        }    public void setPhy(Integer phy)
        {
            this.phy = phy;
        }    public String toString()
        {
            return id + "," + name + "," + chn + "," + math + "," + eng + "," + phy
                + "," + che;
        }
    }在D盘下建score.txt文件
    测试数据
    003,Tom,80,70,70,70,70
    001,ZhangSan,86,70,84,93,78004,BJava,70,70,70,70,80
    015,DJsp,70,70,70,80,70
    009,AJava,70,70,80,70,70
    006,EJ2ee,70,80,70,70,70
    002,CLiSi,92,85,67,83,91
      

  11.   

    如果要按姓名排序就是:
    Order order = new Order("name");
    按外语成绩排序就是:
    Order order = new Order("eng", true);
    ...