日期:2014-05-20  浏览次数:20871 次

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

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

------解决方案--------------------
for example
Java code
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();