package my;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
public class readfile
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
// 文件存储位置
File f = new File("d:/example");
File d = new File(f, "abc.txt");
// 调用方法readTextFile()将文件由byte 转为string
String text = readTextFile(d);
// 调用方法parseText()将字符串分割
HashMap<String, String> values = parseText(text);
Student stu = new Student();
stu.id = Integer.valueOf(values.get("id"));
stu.name = values.get("name");
stu.phone = values.get("phone");
System.out.println("exit");
}
// 方法parseText()将字符串分割
public static HashMap parseText(String text)
{
HashMap<String, String> values = new HashMap();
// 以逗号将字符串分割
String[] kkk = text.split(",");
for (String k : kkk)
{
k = k.trim();
if (k.length() == 0)
continue;
// 以冒号将字符分割
String[] nv = k.split(":");
String name = nv[0];
String value = nv[1];
values.put(name, value);
}
return values;
}
// 方法readTextFile()将文件由byte 转为string
public static String readTextFile(File d)
{
// TODO Auto-generated method stub
try
{
FileInputStream inputstream = new FileInputStream(d);
int size = (int) d.length();
byte[] data = new byte[size];
// 把读到的数据放在data中
inputstream.read(data);
inputstream.close();
;
String text = new String(data, "UTF-8");
return text;
} catch (Exception e)
{
// TODO Auto-generated cach block
e.printStackTrace();
}
return null;
}
}
//学生类
package my;
public class Student
{
public int id;
public String name;
public String phone;
public Student()
{
}
public Student(int id, String name, String phone)
{
this.id = id;
this.name = name;
this.phone = phone;
}
}