文件分类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package filesort;
import java.io.File;
import java.util.Scanner;`enter code here`
import java.io.IOException;
import java.util.Comparator;
import java.util.Collections;
import java.util.ArrayList;
import java.io.PrintWriter;


/**
 */

    /**
     * @param args the command line arguments
     */
    class recordInfo{
        int studentNumber;
        String firstName;


        public recordInfo(int studentNumber,String firstName){
            this.studentNumber = studentNumber;
            this.firstName = firstName;
        }
    }


    class firstNameCompare implements Comparator<recordInfo>{

        @Override
        public int compare(recordInfo t, recordInfo t1) {
            return t.firstName.compareTo(t1.firstName);
        }

    }

       class studentNumberCompare implements Comparator<recordInfo>{

        @Override
        public int compare(recordInfo t, recordInfo t1) {
            return t.studentNumber - t1.studentNumber;
        }

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


        File newfile = new File("student_record.txt");


//        try{
//            PrintWriter output = new PrintWriter(newfile);
//                output.println("72" + "t" +"James");
//                output.println("56" + "t" +"Mark");
//                output.println("87" + "t" +"John");
//                output.println("30" + "t" +"Phillip");
//                output.println("44" + "t" +"Andrew");
//                output.close();
//            }
//        
//        catch(IOException ex){
//            System.out.printf("error n", ex);
//                   
//        }

       try{
            Scanner input = new Scanner(newfile);
            try (PrintWriter output = new PrintWriter(newfile)) {
                ArrayList<recordInfo> allRecords = new ArrayList<>();
                String record = input.next();
                while (input.hasNext()){
                    String[] oneRecord = record.split(" ");
                    int studentNumber = Integer.valueOf(oneRecord[0]);
                    String firstName = oneRecord[1];
                    allRecords.add(new recordInfo(studentNumber, firstName));

                    //System.out.printf("%s %s n", studentNumber, firstName);
                }
                Collections.sort(allRecords, new studentNumberCompare());
                for (recordInfo RecordInfo : allRecords){
                    output.println(RecordInfo.firstName);
                    output.println("t" +RecordInfo.studentNumber);
                    output.println("n");
                    System.out.println(" " + RecordInfo.studentNumber + " " + RecordInfo.firstName);
                }
               output.close();

            }

       }

        catch(IOException ex){
            System.out.printf("error n", ex);

        }

    }

}

基本上,代码是按升序排列的学生号码对名为student_records.txt的文件中的记录进行排序。 现在,我得到这个错误:

java.util.Scanner.throwFor(Scanner.java:907)java.util.Scanner.next(Scanner.java:1416)在filesort.FileSort.main(FileSort。)中的异常。 java:79)Java结果:1

另外,student_records.txt是空的。 :/有人可以帮我吗?


尝试改变:

            String record = input.next();
            while (input.hasNext()){

至:

            while (input.hasNext()){
                String record = input.next();

移动

 try (PrintWriter output = new PrintWriter(newfile)) {

就在输出循环之前,这就是扫描之前清空文件的原因

并将扫描器更改为BufferedReader(从FileReader)并使用readline,因为Scaner接下来会给出onla令牌。

链接地址: http://www.djcxy.com/p/96083.html

上一篇: File Sorting

下一篇: java.util.scanner