论坛首页 入门技术论坛

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

浏览 32455 次
该帖已经被评为新手帖
作者 正文
   发表时间:2009-01-12  
boboism 写道
哎…… 这种还是保留在C语言的思想上。JavaSE 这么多的类库都不会用,用个arraylist包着hashmap不就得了。再匿名一个compararor接口不就得了。干嘛这么麻烦


大哥,你这个方法不就是前面那个嘛,hashmap不能解决重复key值的问题
0 请登录后投票
   发表时间:2009-01-12  
解决重复问题的groovy解法

 def data = []  
 new File('d:/test.txt').eachLine{  
     def line = it.tokenize()  
     data+=new Expando(key1:line[0],key2:line[1] as Integer) 
    
 }  
 def outFile = new File('d:/sun.txt')  
 def printWriter = outFile.newPrintWriter()  
 data.sort{it.key2}.each{  
     printWriter.println("${it.key1} ${it.key2}")  
 }  
 printWriter.flush()  
 printWriter.close() 
0 请登录后投票
   发表时间:2009-01-14  
用String的split
没有用StringTokenizer好!
0 请登录后投票
   发表时间:2009-01-15  
你的程序写的很精彩,然而我们换一种角度,我们会发现您想多了。既然sort()是按key排序的,我们就可以让那些数字变成key嘛!!!!!!!!

/*TreeMap<Integer, String> map = new TreeMap<Integer, String>(
new MyIntComparator());*/   改成  TreeMap map = new TreeMap();

我的输出是:(我注视了两句输出)
------------------读文件开始-------------------------------

以行为单位读取文件内容,每次读取一整行
line number is 1::小李 4564
line number is 2::小王 3234
line number is 3::阿斯顿 23444
line number is 4::小张 2342
line number is 5::小强 1030
line number is 6::小周 1020
------------------读文件结束-------------------------------

------------------写文件开始-------------------------------
写文件开始:E:\Javapractice\2008-12-04-01\FirstJava\bin\FirstJava\test.txt
key is :1020
vlaue is :小周
key is :1030
vlaue is :小强
key is :2342
vlaue is :小张
key is :3234
vlaue is :小王
key is :4564
vlaue is :小李
key is :23444
vlaue is :阿斯顿
写文件E:\Javapractice\2008-12-04-01\FirstJava\bin\FirstJava\test.txt成功!
---------------------------写文件结束--------------------------
0 请登录后投票
   发表时间:2009-01-15  
MicroClimber 写道
你的程序写的很精彩,然而我们换一种角度,我们会发现您想多了。既然sort()是按key排序的,我们就可以让那些数字变成key嘛!!!!!!!!

/*TreeMap<Integer, String> map = new TreeMap<Integer, String>(
new MyIntComparator());*/   改成  TreeMap map = new TreeMap();

我的输出是:(我注视了两句输出)
------------------读文件开始-------------------------------

以行为单位读取文件内容,每次读取一整行
line number is 1::小李 4564
line number is 2::小王 3234
line number is 3::阿斯顿 23444
line number is 4::小张 2342
line number is 5::小强 1030
line number is 6::小周 1020
------------------读文件结束-------------------------------

------------------写文件开始-------------------------------
写文件开始:E:\Javapractice\2008-12-04-01\FirstJava\bin\FirstJava\test.txt
key is :1020
vlaue is :小周
key is :1030
vlaue is :小强
key is :2342
vlaue is :小张
key is :3234
vlaue is :小王
key is :4564
vlaue is :小李
key is :23444
vlaue is :阿斯顿
写文件E:\Javapractice\2008-12-04-01\FirstJava\bin\FirstJava\test.txt成功!
---------------------------写文件结束--------------------------



呵呵 你看下所有回复贴吧,有两位写的很好很值得收藏,其中一位仁兄给了面向对象思想的程序段。
你说的要是就本题而言是对的,但是如果被读取文件包含数字重复字段就不适用了,我当时也是没注意到,
看来一些问题还是需要摆出来献丑的,经过高人的修正确实能学到许多
0 请登录后投票
   发表时间:2009-01-15  
nba520pz 写道
用String的split
没有用StringTokenizer好!


恩,我不敢说 用String的split没有用StringTokenizer好,但是值得肯定的是他们都能实现不假。
我在百度摆了下,看了几篇相关文章感觉看后只是看到它们的些许不同,还是不能区别出来哪个好与坏。
还望牛牛们详细讲解下吧
0 请登录后投票
   发表时间:2009-01-20  
建立一个对象

public class Person{
String name;
int number;
Person(String line){
String[] ss = line.split(" ");
name = ss[0];
number = Integer.parseInt(ss[1]);
}
}

能让你的程序简单一些
0 请登录后投票
   发表时间:2009-01-20  
sort -nk 2
0 请登录后投票
   发表时间:2009-08-12  

也来练练手

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test2 {
    public static void main(String[] args) {
        try {
            //读取
            List<Person> persons = new ArrayList<Person>();
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("e:/test/intel.txt"),"GBK"));
            String line = null;
            while ((line = in.readLine()) != null) {
                String[] temp = line.split(" ");
                persons.add(new Person(Integer.valueOf(temp[1]),temp[0]));
            }
            in.close();
            //排序
            Collections.sort(persons, new Comparator<Person>(){
                public int compare(Person o1, Person o2) {
                    return o1.getId().compareTo(o2.getId());
                }
            });
            //写入
            BufferedWriter out = new BufferedWriter(new FileWriter("e:/test/sun.txt"));
            for (Person person : persons) {
                String s = person.getName() + " " + person.getId();
                System.out.println(s);
                out.write(s+"\r\n");
            }
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Person {
    Integer id;
    String name;
    public Person(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 

0 请登录后投票
   发表时间:2009-08-13  
把每行封装成一个对象,实现Comparable接口。
0 请登录后投票
论坛首页 入门技术版

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