本帖最后由 江南雨 于 2013-5-24 13:00 编辑
- package com.xiaoye.random;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.TreeSet;
- public class RandomLine {
- public static void main(String[] args) throws FileNotFoundException {
- String filepath = "src\\a.txt";
- String filename = filepath.substring(filepath.lastIndexOf("\\") + 1);
- String pathname = "lib" + "\\" + filename;
- System.out.println(pathname);
- File file = new File(filepath);
- File outfile = new File(pathname);
- InputStream in = new FileInputStream(file);
- Scanner reader = new Scanner(in);
- TreeSet<String> set = new TreeSet<String>();
- while (reader.hasNextLine()) {
- String s = reader.next();//假如这里是到 bridle n.笼子;束缚 vt.抑制这一行,可是为什么变成了两行呢?在a.txt文件中它确实是只有一行啊 !!
- System.out.print(s + "\t\t");
- System.out.println(s.length());
- set.add(s);
- }
- System.out.println("===========================");
- for (String s : set) {
- System.out.println(s);
- }
- // OutputStream out=new FileOutputStream(outfile);
- }
- }
- class MyComparator<T> implements Comparator<T> {
- @Override
- public int compare(T o1, T o2) {
- String t1 = (String) o1;
- String t2 = (String) o2;
- if (t1.length() > t2.length()) {
- return 1;
- } else if (t1.length() < t2.length()) {
- return -1;
- }
- return 0;
- }
- }
- 下面是我的a.txt文件
- name: n:名字,姓名
- native:本地的
- ative:活跃
- bridegroom n.新郎
- bridge vt.架桥于,用桥连接
- bridle n.笼子;束缚 vt.抑制
- brightness n.明亮,辉煌,聪明
- broaden vt.&vi.放宽,变阔
- bronze n.青铜色
- brood vt.沉思vi.郁闭地沉思
- Buddhism n.佛教,释教
- budget n.预算,预算案
- buffalo n.水牛;水陆坦克
- bug n.虫子;臭虫
- bugle n.军号,喇叭
- bull n.买空的证券投机商
- bulletin 告示,公告,公报
- coke n.焦炭 vt.&vi.炼焦
- collaborate vi.协作,合作;协调
- collide vi.碰撞;冲突,抵触
- colonial a.殖民地的,殖民的
- colonist n.移民;殖民地居民
- combat vt.跟…战斗 vi.格斗
- combustion n.燃烧;氧化;骚动
- comedy n.喜剧;喜剧场面
- commence vt.开始 vi.获得学位
- commend vt.称赞,表扬;推荐
- commission n.委托,委任;委托状
- commodity n.日用品,商品,物品
- commonplace a.平凡的 n.平常话
- commonsense a.有常识的
- commonwealth n.共和国;联邦
- compact a.紧密的 vt.使紧凑
- comparable a.可比较的;类似的
- compatible a.一致的;兼容制的
复制代码 |