大家好,由于本人水平很一般,所以遇到一个难题,在这请教大家!
我想用BufferedReader中的readline()方法读一个文档文件,然后把这些东西(String)循环赋值给变量s1,s2,s3,s4````sn;因为我要把其中的某些东西(其中一些不是全部,比如s2,s4,s5,s6)插入数据库,请问该如何实现呢,如果有其他比BufferedReader更好的方法,也可以,比如正则表达式等等;在线求救!

解决方案 »

  1.   

    问题不明确
    s1,s2,...sn是文本的每一行信息还是对于文本的每一行都有s1,s2...sn这样的信息
    既然已经知道怎么读取,那就自己判断满足条件的的时候插入数据库就可以了
      

  2.   

    s1,s2那些是读出来的每一行的String,比如读出的s1是导演,s2是周星驰,s3是音乐制作人,我唯一能写的就是我要条件匹配s2,提取;唯一有规律的就是s1和s3这样的东西
      

  3.   

    for example
    List<Integer> targetLine = Arrays.asList(new Integer[]{2, 4, 5, 6});
    int currentLine = 0;
    List<String> data = new ArrayList<String>();
    Scanner sc = new Scanner(new FileInputStream("yourfile"));
    while (sc.hashNextLine()) {
        String s = sc.nextLine();
        currentLine++;
        if (targetLine.contains(currentLine)) data.add(s);
        if (currentLine > targetLine.get(targetLine.size()-1)) break;
    }
    sc.close();String sql = "insert xxx values(?, ?, ?, ?);"
    PreparedStatement ps = yourConnection.prepareStatement(sql);
    for (int i=0; i<data.size(); i++) {
        ps.setString(i+1, data.get(i));
    }
    ps.executeUpdate();
      

  4.   

    readline赋值给一个变量,写JDBC程序,执行insert就可以了