import java.io.*;
import java.util.*;
public class SortTest{
public static void main(String args[]) throws IOException, ClassNotFoundException {
FileReader InWord = new FileReader(new File("words.txt"));
BufferedReader in = new BufferedReader(InWord);
String ws[] = new String[100];
String input;
int index = 0;
while((input=in.readLine())!=null)
ws[index++]=input;
Arrays.sort(ws, 0, index); BufferedWriter out = new BufferedWriter(new FileWriter(new File("SortWords.txt"))); for(String s : ws){
if(s==null)
break;
System.out.println(s);
out.write(s, 0, s.length());
out.newLine();
}
in.close();
out.close(); myTest myArray[] = new myTest[20];
in = new BufferedReader(new FileReader(new File("words.txt")));
index=0;
while((input=in.readLine())!=null){
ws[index++]=input;
} for(int i=0; i<index; ++i){
String str[]=ws[i].split(" ");
myArray[i] = new myTest(Integer.parseInt(str[0]), str[1]);
/*
开始的时候是这样写的, 奥心死了, 作死的节奏啊.....半天没找出来哪里出现了空指针
myArray[i].x=Integer.parseInt(str[0]);
myArray[i].name=str[1];
*/
}
//1. 利用 自定义的 Comparator类中的compare 方法进行排序
Arrays.sort(myArray, 0, index, new myComparator());
//2. 利用 接口Comparable中的compareTo进行排序
//Arrays.sort(myArray, 0, index); DataOutputStream dOut = new DataOutputStream(new FileOutputStream(new File("SortWords.txt")));
for(myTest tmp : myArray){
if(tmp==null) break;
System.out.println(tmp.x + " " + tmp.name);
dOut.writeInt(tmp.x);
dOut.writeChar(' ');
dOut.writeChars(tmp.name);
dOut.writeChar('\n');
} //如果想要利用ObjectIputStream反串行化构造对象,就必须保证源文件已经是 利用ObjectOutputStream 写入的,否则出现错误
ObjectOutputStream ObjOut = new ObjectOutputStream(new FileOutputStream(new File("SortWords.txt")));
for(int i=0; i<index; ++i)
ObjOut.writeObject(myArray[i]);
//通过这种方法,可以避免ObjcetInputStream.readObject()中产生EOFException异常
//ObjOut.writeObject(null); index=0;
myTest inputTmp;
ObjectInputStream ObjIn = new ObjectInputStream(new FileInputStream(new File("SortWords.txt")));
try{
while((inputTmp=(myTest)ObjIn.readObject())!=null){
myArray[index++]=inputTmp;
}
}catch(IOException e){e.printStackTrace(); }finally{ ///放生的EOFException异常时IOException的子类
System.out.println("EOFException处理完毕!");
}
System.out.println("Finish!");
}
} class myTest implements Comparable<myTest>, Serializable{
int x;
String name;
public myTest(){}
public myTest(int x, String name){
this.x=x;
this.name=name;
} public int compareTo(myTest tmp){
if(x==tmp.x)
return name.compareTo(tmp.name);
else return x-tmp.x;
} private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
//in.defaultReadObject();
x=(int)in.readInt();
name=(String)in.readObject();
}
private void writeObject(ObjectOutputStream out) throws IOException{
//out.defaultWriteObject();
out.writeInt(x);
out.writeObject(name);
}
} class myComparator implements Comparator<myTest>{
public int compare(myTest o1, myTest o2){
if(o1.x==o2.x)
return o1.name.compareTo(o2.name);
else return o1.x - o2.x;
} public boolean equals(Object o1){
return true;
}
}

  

java.util.Arrays.sort两种方式的排序(及文件读写练习)的更多相关文章

  1. 对Java代码加密的两种方式,防止反编译

    使用Virbox Protector对Java项目加密有两种方式,一种是对War包加密,一种是对Jar包加密.Virbox Protector支持这两种文件格式加密,可以加密用于解析class文件的j ...

  2. Java新建线程的两种方式

    Java新建线程有两种方式,一种是通过继承Thread类,一种是实现Runnable接口,下面是新建线程的两种方式. 我们假设有个竞赛,有一个选手A做俯卧撑,一个选手B做仰卧起坐.分别为两个线程: p ...

  3. 《连载 | 物联网框架ServerSuperIO教程》- 10.持续传输大块数据流的两种方式(如:文件)

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  4. (转)DLL中导出函数的两种方式(dllexport与.def文件)

    DLL中导出函数的两种方式(dllexport与.def文件)http://www.cnblogs.com/enterBeijingThreetimes/archive/2010/08/04/1792 ...

  5. 【转】DLL中导出函数的两种方式(dllexport与.def文件)

    DLL中导出函数的两种方式(dllexport与.def文件) DLL中导出函数的声明有两种方式: 一种方式是:在函数声明中加上__declspec(dllexport):另外一种方式是:采用模块定义 ...

  6. Java找N个数中最小的K个数,PriorityQueue和Arrays.sort()两种实现方法

    最近看到了 java.util.PriorityQueue.刚看到还没什么感觉,今天突然发现他可以用来找N个数中最小的K个数. 假设有如下 10 个整数. 5 2 0 1 4 8 6 9 7 3 怎么 ...

  7. [Java] HashMap遍历的两种方式

    Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml第一种: Map map = new HashMap( ...

  8. Java实现深克隆的两种方式

    序列化和依次克隆各个可变的引用类型都可以实现深克隆,但是序列化的效率并不理想 下面是两种实现深克隆的实例,并且测试类对两种方法进行了对比: 1.重写clone方法使用父类中的clone()方法实现深克 ...

  9. java 实现websocket的两种方式

    简单说明 1.两种方式,一种使用tomcat的websocket实现,一种使用spring的websocket 2.tomcat的方式需要tomcat 7.x,JEE7的支持. 3.spring与we ...

随机推荐

  1. ssh端口转发

    http://blog.sina.com.cn/s/blog_47094def0101fvge.html

  2. php跨域请求

    跨域api服务器设置 header('content-type:application:json;charset=utf8'); header('Access-Control-Allow-Origin ...

  3. 有用的php函数

    filter系列函数 filter_input   通过名称获取特定的外部变量,并且可以通过过滤器处理它 filter_input(INPUT_GET, 'a', FILTER_SANITIZE_NU ...

  4. php+Mysqli利用事务处理转账问题实例

    本文实例讲述了php+Mysqli利用事务处理转账问题的方法.分享给大家供大家参考 <?php /**php+Mysqli利用事务处理转账问题实例 * author http://www.lai ...

  5. Silverlight用户自定义控件件中增加属性和方法

    下面的例子在用户控件MyCpmzSelect中增加了一个myCaption属性 public static readonly DependencyProperty myCaptionProperty ...

  6. [转载]sql语句练习50题

    Student(Sid,Sname,Sage,Ssex) 学生表 Course(Cid,Cname,Tid) 课程表 SC(Sid,Cid,score) 成绩表 Teacher(Tid,Tname) ...

  7. centos 6.5 X64 安装 mongodb 2.6.1 (笔记 实测)

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:Centos-6.5-x86_64 *** Centos编译安装mongodb 2.6 系统最好是64位的,才 ...

  8. WPF快速入门系列(8)——MVVM快速入门

    一.引言 在前面介绍了WPF一些核心的内容,其中包括WPF布局.依赖属性.路由事件.绑定.命令.资源样式和模板.然而,在WPF还衍生出了一种很好的编程框架,即WVVM,在Web端开发有MVC,在WPF ...

  9. Web Essentials之样式表StyleSheets

    返回Web Essentials功能目录 本篇目录 智能感知 视觉提示 验证 Web标准 转换器 Web Essentials中大多数的CSS功能也适用于LESS. 智能感知 生成供应商特定的属性 如 ...

  10. Linux 网络编程(多路复用)

    服务器端代码 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/soc ...