论坛首页 入门技术论坛

一笔试小题---文件读写操作(希望对刚毕业找工作的朋友有所帮助)

浏览 32438 次
该帖已经被评为新手帖
作者 正文
   发表时间:2009-01-05   最后修改:2009-01-05
OO
题目:文件intel.txt,内容如下:

小王【空格】10000【回车】
小强【空格】12345【回车】
小张【空格】2342 【回车】
小强【空格】1030 【回车】
小周【空格】1020 【回车】

请编写一程序从test.txt中读取数据,并按数字大小排序后写入另一文件sun.txt(写入格式同上)

下面是自己编写的程序代码,小弟刚毕业所学有限所幸要求功能是实现了,然必定有不规范不合理之处还望大家多多指教修正!!(希望大家提供些更合理的排序)
------------------------------------------------------------------
import com.j2se.file.MyIntComparator;//自定义比较器
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.TreeMap;


/***
*读文件test.txt内容(文件内容格式: 小王【空格】10000【回车】
*                                小强【空格】12345【回车】
*并对该文件内容按存款数排序,然后将其写入另一文件内
*
*****/
public class fileSort {

/*****
* 以行为单位读写文件
            *@param fileName  源文件
            *@param toFileName  目标文件
* ****/
public static void  read_writeFile(String fileName,String toFileName){
File file = new File(fileName);
BufferedReader reader=null;
TreeMap<Integer,String> map = new TreeMap<Integer,String>(new MyIntComparator());
File toFile = new File(toFileName);
PrintWriter writer = null;
try{
System.out.println("------------------读文件开始-------------------------------");
System.out.println();
System.out.println("以行为单位读取文件内容,每次读取一整行");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
String[] ss = null;
int line = 1;

//一次读一行,直到读取为null
while((tempString = reader.readLine()) != null){
//显示行号
System.out.println("line number is "+line+"::"+tempString);
line++;

ss = tempString.split(" ");
int i = 0;
String value = ss[i];
int key =Integer.parseInt(ss[i+1]);
System.out.println("key is:"+key);
System.out.println("value is:"+value);
map.put(key, value);

}
System.out.println("------------------读文件结束-------------------------------");
System.out.println();
System.out.println("------------------写文件开始-------------------------------");
System.out.println("写文件开始:"+file.getAbsolutePath());
writer = new PrintWriter(new FileOutputStream(toFile));
if(map !=null){
Object key = null;
Object value = null;
// 使用迭代器遍历Map的键,根据键取值
Iterator it = map.keySet().iterator();
while (it.hasNext()){
key = it.next();
value = map.get(key);
System.out.println("key is :"+key);
System.out.println("vlaue is :"+value);
//能写各种基本类型数据
writer.print(value+" ");
writer.print(key+" ");
//换行
writer.println();
//写入文件
writer.flush();
}

System.out.println("写文件"+file.getAbsolutePath()+"成功!");
System.out.println("---------------------------写文件结束--------------------------");
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader != null){
try{
reader.close();
}catch(IOException e1){
e1.printStackTrace();
}
}
if(writer !=null){
writer.close();
}
}

}

public static void main(String[] args){
String fileName = "D:/test.txt";
String toFileName = "D:/test1.txt";
fileSort.read_writeFile(fileName, toFileName);
}
}

----------------自定义比较器--------------------------------------
import java.util.Comparator;

/**
* 整数比较器,将整数按降序排列
*/
class MyIntComparator implements Comparator{

/**
* o1比o2大,返回-1;o1比o2小,返回1。
*/
public int compare(Object o1, Object o2) {
int i1 = ((Integer)o1).intValue();
int i2 = ((Integer)o2).intValue();
if (i1 < i2){
return 1;
}
if (i1 > i2){
return -1;
}
return 0;
}
}
   发表时间:2009-01-05  
没什么思想
纯碎考api~

0 请登录后投票
   发表时间:2009-01-05  
之所以自定义比较器是因为TreeMap自排序功能是按key值排序的,如果不自定义的话输出结果是这样:
小李 4564
小王 3234
阿斯顿 23444
小张 2342
小强 1030
小周 1020
貌似TreeMap按key排序是先排首位再排次位依次类推的,所以只有自定义比较器了。
望指正
0 请登录后投票
   发表时间:2009-01-05  
spyker 写道



Java代码

public&nbsp;class&nbsp;TestSort&nbsp;{ &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{ &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;Integer&gt;&nbsp;intList&nbsp;=&nbsp;new&nbsp;ArrayList&lt;Integer&gt;(); &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intList.add(123); &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intList.add(122223); &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intList.add(1233); &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intList.add(1234); &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intList.add(125); &nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer[]&nbsp;intArr&nbsp;=&nbsp;intList.toArray(new&nbsp;Integer[intList.size()]); &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Arrays.sort(intArr); &nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;:&nbsp;intArr)&nbsp;{ &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(i); &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;
}&nbsp;&nbsp;public class TestSort {
public static void main(String[] args) {
List&lt;Integer&gt; intList = new ArrayList&lt;Integer&gt;();
intList.add(123);
intList.add(122223);
intList.add(1233);
intList.add(1234);
intList.add(125);

Integer[] intArr = intList.toArray(new Integer[intList.size()]);
Arrays.sort(intArr);

for (int i : intArr) {
System.out.println(i);
}
}
}
jdk1.5下面的一个sort 呵呵 熟悉下api



题目:文件intel.txt,内容如下:

小王【空格】10000【回车】
小强【空格】12345【回车】
小张【空格】2342 【回车】
小强【空格】1030 【回车】
小周【空格】1020 【回车】

请编写一程序从test.txt中读取数据,并按数字大小排序后写入另一文件sun.txt(写入格式同上)
正确结果是:
阿斯顿 23444
小李 4564
小王 3234
小张 2342
小强 1030
小周 1020
不是只对名字后面的数字排序是整体(名字和数字一起)排序,
希望spyker给个好点的排序方法
0 请登录后投票
   发表时间:2009-01-05  
czx566 写道

没什么思想 纯碎考api~

恩,谢谢  我目标是写自己特色的代码,可现在我是新人
0 请登录后投票
   发表时间:2009-01-05  
jiagyao 写道
czx566 写道

没什么思想 纯碎考api~

恩,谢谢  我目标是写自己特色的代码,可现在我是新人


我没说你的代码不好~
我只是觉得如果公司喜欢考这种纯Api的题
不去也罢~


0 请登录后投票
   发表时间:2009-01-06  
恩,名字和数字做为一个字段排序。
希望高手不要吝啬给点新方法!
0 请登录后投票
   发表时间:2009-01-06  
并且顺序是按数字字段大小排的
0 请登录后投票
   发表时间:2009-01-06  
czx566 写道
jiagyao 写道
czx566 写道

没什么思想 纯碎考api~

恩,谢谢  我目标是写自己特色的代码,可现在我是新人


我没说你的代码不好~
我只是觉得如果公司喜欢考这种纯Api的题
不去也罢~




呵呵
0 请登录后投票
   发表时间:2009-01-06  
czx566 写道
jiagyao 写道
czx566 写道

没什么思想 纯碎考api~

恩,谢谢  我目标是写自己特色的代码,可现在我是新人


我没说你的代码不好~
我只是觉得如果公司喜欢考这种纯Api的题
不去也罢~




我就碰到过类似的上机题,没写,直接走人了。
笔试已经够烦人了,还要上机,更烦人 呵呵
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics