首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么从一个文本文件读取数据并写入数组并进行查询

2013-01-21 
如何从一个文本文件读取数据并写入数组并进行查询?文本abc.txt :12568 李明22467 张强里98654 方芳22376

如何从一个文本文件读取数据并写入数组并进行查询?
文本abc.txt :
12568 李明
22467 张强里
98654 方芳
22376 何玉宇
...


如何将其写入数组,并进行查询,例如快速将id和姓名对应起来。
[解决办法]
简单示例:

Scanner in;
try {
in = new Scanner(new FileReader("abc.txt"));
while (in.hasNextLine()) {
int id = in.nextInt();
String name = in.nextLine();
System.out.println(id + "  " + name);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

[解决办法]
放入到map中,直接通过get方法获取

class Reader{
public Map<String, String> getUser(File file){
Map<String, String> users = new LinkedHashMap<String, String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
while(true){
String line = reader.readLine();
if(line == null){
break;
}
String[] values = line.split("\\s+");
if(values.length == 2){
users.put(values[0], values[1]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return users;
}
}

[解决办法]
刚刚写的,可能不太好!


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <简简单单先这样吧,这种情况是这样的,不支持续写,如果想在后面添加,必须先全部拿出来,在重新存>
 * <功能详细描述>
 * 
 * @see  [相关类/方法]
 */
public class FileOutPut {

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
writeInfo();
readInfo();
}

/**
 * 
 * <读取文件>
 * <功能详细描述>
 * @throws Exception
 * @see [类、类#方法、类#成员]
 */
private static void readInfo() throws Exception{
// TODO Auto-generated method stub
File fList = new File("user.txt");
ObjectInputStream objInput = new ObjectInputStream(
new FileInputStream(fList));
List<User> userList= new ArrayList<User>();
userList = (List<User>) objInput.readObject();
User user = null;
for (int i = 0; i < userList.size(); i++) {


user = new User();
user =userList.get(i);
System.out.println(user.getId()+"----"+user.getName());
}
}

/**
 * 
 * <写文件>
 * <功能详细描述>
 * @throws Exception
 * @see [类、类#方法、类#成员]
 */
private static void writeInfo() throws Exception {
File fList = new File("user.txt");

List<User> userList = new ArrayList<User>();
User user = new User();
user.setId("12568");
user.setName("李明");
userList.add(user);

user = new User();
user.setId("22467");
user.setName("张强里");
userList.add(user);
FileOutputStream file = new FileOutputStream(fList);
ObjectOutputStream objout = new ObjectOutputStream(file);
objout.writeObject(userList);
objout.close();
}

}

class User extends Object implements Serializable {
private String id;
private String name;

/**
 * 获取 id
 * 
 * @return 返回 id
 */
public String getId() {
return id;
}

/**
 * 设置 id
 * 
 * @param 对id进行赋值
 */
public void setId(String id) {
this.id = id;
}

/**
 * 获取 name
 * 
 * @return 返回 name
 */
public String getName() {
return name;
}

/**
 * 设置 name
 * 
 * @param 对name进行赋值
 */
public void setName(String name) {
this.name = name;
}

}


热点排行