所以我在阅读(加载到数组)这个文件时遇到了问题?

时间:2016-03-23 22:34:43

标签: java arrays text load

所以我在阅读(加载到数组)这个文件时遇到了问题

对于此项目,请下载文本文件weblog.txt http://www.santarosa.edu/~lmeade/weblog.txt

注意:要下载此文件,请右键单击该链接并选择SAVE AS。

我保存在桌面上

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class project4 {

    static Scanner scan=new Scanner(System.in);

    public static final String INPUT_FILENAME="weblog.txt";

    public static final String OUTPUT_FILENAME="sorted_weblog.txt";

    public static final int MAX=2990;

    public static String[] fileContent=new String[MAX];

    public static int count=0;

    public static void main(String[] args) {
        readFile();
        sortFileContent();
        writeFile();
    }

    public static void readFile() {
        try {
            File f= new File(INPUT_FILENAME);
            Scanner br = new Scanner(f);
            String sCurrentLine;
            while (br.hasNext()) {
                 sCurrentLine = br.nextLine();
                 fileContent[count++]=sCurrentLine;
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not Found");
        }
    }

    private static void sortFileContent() {
        Arrays.sort(fileContent);
    }

    private static void writeFile() {
        PrintWriter out;
        try {
            out = new PrintWriter(new File(OUTPUT_FILENAME));
            for (int i = 0; i < count; i++) {
                out.println(fileContent[i]);
            }
            out.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not Found");
        }
    }

}

0 个答案:

没有答案