我需要从java读取.txt文件并将排序的上下文写入不同的.txt文件

时间:2016-11-06 22:37:15

标签: java io

我有一个I / O java文件,一个包含main的SDive文件,一个包含随机单词的.txt目录文件和一个Sorted .txt,用于按升序降序返回随机单词。它会打印Sorted中的所有单词,但不会对其进行排序。

//Sort.java
// required for input
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
// required for output
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

class Sort{

   private String[] tArr = new String[100];

       public void swap(int j) {      

          String temp = tArr[j-1];
          tArr[j-1] = tArr[j];
          tArr[j] = temp;
       }

      ///Bubble sort
      public void sort() {
         int n = tArr.length;
         for(int i = 0; i < n; i++) {
            for(int j = 1; j < n-i; j++) {
               if(tArr[j-1].compareTo(tArr[j]) > 0) {
                  swap(j);
               }
            }
         }
      }

public void read() {

    System.out.println("in read()");
    String pwd = System.getProperty("user.dir");
    String fileName = pwd + "/directory.txt";
    System.out.println("Looking for: " + fileName);
    String fileLine = "";

    try {

        File f = new File(fileName);

        if(f.exists()) { 
           System.out.println("Directory profile found, loading data...");
        }
        else {
           System.out.println("Directory profile not found, loading default...");

           return; // done, return back to the caller
        }

        // Read file
        FileReader data = new FileReader(fileName);

        // Wrap FileReader with BufferedReader
        BufferedReader br = new BufferedReader(data);
        //String tmp;

        int i=0;
        while ((fileLine = br.readLine()) != null) {                    
           tArr[i++] = fileLine;
        }

        // ok, time to load an existing profile


        // close the file
        br.close();

    } catch (FileNotFoundException fnf) {
        System.out.println("File not found: " + fileName);
    } catch (IOException ioe) {
        System.out.println("Error reading file: " + fileName);
    } catch (Exception exc) {
        System.out.println("Something went wrong: " + fileName);
    }

}

    public void write() {

    System.out.println("in write()");
    String pwd = System.getProperty("user.dir");
    String fileName = pwd + "/Sorted.txt";        

    try {            

        System.out.println("Writing out to: " + fileName);

        File file = new File(fileName);
        // creates the file
        file.createNewFile();
        // create FileWriter object
        FileWriter writer = new FileWriter(file);

        // output to file
        // ADD pIdx, pArr, mood, and anything else here...

        for(int i = 0; i < tArr.length; i++) {
            writer.write(tArr[i] + "\n");
        }            

        writer.flush();
        writer.close();

    } catch (IOException ex) {
        System.out.println("Error writing to file: " + fileName);
    }

  }

}

我的主要文件是SDrive:

 class SDriver{

    public static void main(String args []){
       Sort io = new Sort();
       io.read();
       io.write();

    }
}

1 个答案:

答案 0 :(得分:0)

你应该在io.read(); 之后添加 io.sort()方法,然后在io.write()之前添加; 线。

我看到你使用冒泡排序,如果你真的想要实现自己的排序方法,请看一下快速排序和合并排序,它比大型阵列上的冒泡排序要快得多,但也难以实现。插入和选择排序不如合并或快速快,但仍然比泡沫更快,并且仍然易于自我实现。或者使用Arrays.sort(tArr);如果你想快点做。