我要把TXT里的内容插入到数据库。TXT格式是这样:
device=55555555
id=6
userid=0
time=2009.05.17 12:48:05
log_pic=00000000-1242564485.jpg
device=55555555
id=5
userid=0
time=2009.05.17 12:46:55
log_pic=00000000-1242564415.jpg
device=55555555
id=4
userid=0
time=2009.05.17 12:46:24
log_pic=00000000-1242564384.jpg最好给段代码。

解决方案 »

  1.   

    发了两个啊,public static void main(String[] args) {
    File ft = new File("E:\\temp\\test.txt");
    StringBuffer sb = new StringBuffer();
    try {
    String str = "";
    String device = "";
    String id = "";
    String userid = "";
    String time = "";
    String log_pic = "";
    InputStream is = new FileInputStream(ft);
    BufferedReader in = new BufferedReader(new InputStreamReader(is, "GBK"));
    while (null != (str = in.readLine())) {
    if(str.indexOf("device") != -1) {
    device = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
    }else if(str.indexOf("id") != -1) {
    id = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
    }else if(str.indexOf("userid") != -1) {
    userid = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
    }else if(str.indexOf("time") != -1) {
    time = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
    }else if(str.indexOf("log_pic") != -1) {
    log_pic = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
    //在这里进行写数据库操作
    System.out.println(device + "#" + id + "#" + userid + "#" + time + "#" + log_pic);
    }
    }
    } catch (Exception e) {
    System.out.println("读数据错误");
    }
    }
      

  2.   

    截取字符串 应该不难吧  看看API 什么方法都有
      

  3.   

    对 rascalboy520 补充
    create database Test;
    create table test(
       id int(11) not null  primary key,
       useid int(11) null,
       device varchar(50) null,
       time TimeStamp null,
        log_pic varchar(100),null
    )
    JDBC 编程
    import java.sql.*
    class MyJDBC {
    String driver = "com.mysql.jdbc.Driver"; String uri = "jdbc:mysql://localhost:3306/Test?username='root'&password=''"; Statement st = null; Connection con = null; public MyJDBC() throws Exception { Class.forName(driver);
    try {
    con = DriverManager.getConnection(uri);
    st = con.createStatement();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } public void insert(String id, String useid, String device, String time,
    String log_pic) {
    try {
    st.executeQuery("insert into test values(" + id + "," + useid + ","
    + device + "," + time + "," + log_pic + ")");
    } catch (SQLException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    } public void close() {
    try {
    st.close();
    con.close();
    } catch (Exception e) {
    // TODO: handle exception
    }
    }
    }
    public class Test {
    public static void main(String[] args) {
            File ft = new File("E:\\temp\\test.txt");
            StringBuffer sb = new StringBuffer();
            MyJDBC myjc=new MyJDBC();
            try {
                String str = "";
                String device = "";
                String id = "";
                String userid = "";
                String time = "";
                String log_pic = "";
                InputStream is = new FileInputStream(ft);
                BufferedReader in = new BufferedReader(new InputStreamReader(is, "GBK"));
                while (null != (str = in.readLine())) {
                    if(str.indexOf("device") != -1) {
                        device = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
                    }else if(str.indexOf("id") != -1) {
                        id = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
                    }else if(str.indexOf("userid") != -1) {
                        userid = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
                    }else if(str.indexOf("time") != -1) {
                        time = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
                    }else if(str.indexOf("log_pic") != -1) {
                        log_pic = str.substring(str.lastIndexOf("=") + 1, str.length()).trim();
                        myjc.insert(id,useid,device,time,log_pic);
                        System.out.println(device + "#" + id + "#" + userid + "#" + time + "#" + log_pic);
                    }
                }
            } catch (Exception e) {
                System.out.println("读数据错误");
            }
        }
    }