【问题标题】:How to takes a filename from the command line & outputs that file heap sorted?如何从命令行获取文件名并输出该文件堆排序?
【发布时间】:2016-06-28 22:46:53
【问题描述】:

我是 java 新手,如果有人能给我一些帮助,我将不胜感激。提前致谢。

我正在尝试从命令行获取文件名并输出排序后的文件(从我的 Heap.java 中),然后每行打印一个单词。

这是我所拥有的:

public class HeapSort {

public static void main(String[] args) throws IOException {
  //public static void main(String[] args)  {   


    // Declare and instantiate a new ArrayList object
       ArrayList<String> data = new ArrayList<String>(); 

    //Declare the input file names
      String inFile = "data2415.txt";

     Scanner sc = new Scanner(new File(args[0]));

      if (0 < args.length){
         String filename = args[0];
          File file = new File(filename);
      }

        //h = reader.readLine();

      while (sc.hasNext()) data.add(sc.next());

      }


}

【问题讨论】:

    标签: java command-line heap command-line-arguments heapsort


    【解决方案1】:

    怎么样

      List<String> resultList = FileUtils.readlines("text.txt");
      Collections.sort(resultList);
      for (String s: resultList)
            System.out.println(s);
    

    【讨论】:

    • 问题说“每行打印一个单词。这由代码支持,该代码使用Scanner.next(),而不是nextLine()。此答案中的代码将排序 lines,而不是 words,因此它不一样,但对于排序行,这将是一个完美的答案。
    猜你喜欢
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    相关资源
    最近更新 更多