且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java:排序文本文件行

更新时间:2023-12-04 15:20:28

你首先当然需要读取文件rong>,你可以在这里学习如何做。

Java:如何阅读文本文件

You will first of course need to read a file, which you can learn how to do here.
Java: How to read a text file

这个例子将提供一些方法,一旦你写文件已经对您的数据进行了排序。

如何创建文件并用Java写入?

This example will provide several ways you may write the file once you have sorted your data.
How do I create a file and write to it in Java?

至于排序,我建议创建一个类Movie,它看起来类似于

As for sorting, I recommend creating a class Movie, which would look similar to

public class Movie implements Comparable<Movie> {  
    private String name;
    private String leadActor;
    private Date releaseDate;

    public Movie(String name, String leadActor, String releaseDate) {

    }

    @Override
    public int compareTo(Movie other) {

    }
}  

我留给你填写构造函数和compareTo方法的其余部分。一旦你有了compareTo方法,你就可以通过你的电影列表调用Collections.sort(列表列表)。

Ill leave it to you fill in the rest of the constructor and compareTo method. Once you have your compareTo method you will be able to call Collections.sort(List list) passing your list of Movie.

以下是实现Comparable的一些资源

http:// docs.oracle.com/javase/tutorial/collections/interfaces/order.html

为什么Java类可以实现可比性?