将filename与整数进行比较

时间:2013-12-21 20:42:36

标签: java mysql

我在mysql中有一个数据库,其中包含字段userId, userName etc,.,我正在根据某些条件检索userid

我有一个包含文件的文件夹,文件名是userId。我想将检索到的userId与这些文件进行比较,如果匹配则进一步处理这些文件。

我尝试了这段代码,但它的数字格式异常。不明白为什么?

package read;

 import java.io.*;
 import java.util.*;
 import java.sql.*;
 public class read1 {
 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
 static final String DB_URL = "jdbc:mysql://localhost/test";
 static final String USER = "root";
 static final String PASS = "root";
 public static void main(String args[])throws Exception{
    Connection conn = null;
    Statement stmt = null;
    try{

  Class.forName("com.mysql.jdbc.Driver");
  System.out.println("Connecting to database...");
  conn = DriverManager.getConnection(DB_URL,USER,PASS);
  System.out.println("Creating statement...");
  stmt = conn.createStatement();
  String sql;
  sql = "SELECT userId FROM profile1 where friendCount >200 AND statusCount > 200";
  ResultSet rs = stmt.executeQuery(sql);
  processing p = new processing();
    File directory = new File("/home/abc/tweet");

    //get all the files from a directory
    File[] fList = directory.listFiles();

    while(rs.next()){
     int uid = rs.getInt("userId");
     System.out.print("ID: " + uid +"\n");
      for (File file : fList){
        if(file.isFile())
        System.out.println(file.getName());
        if(Integer.parseInt(file.getName())== uid)
        p.process(file.getName());    

                 }
            }
    }
    catch(SQLException e){
  e.printStackTrace();
 }
}
}

class processing{

    void process(String filename)throws Exception{
     FileReader fr = new FileReader("/home/abc/tweet/"+filename);
         BufferedReader br = new BufferedReader(fr);

      FileWriter wr = new FileWriter("/home/abc/newtweet/"+filename);
                String s;
                    String str="Text:";
                    String a[];
            BufferedWriter bw = new BufferedWriter(wr);
                while ((s = br.readLine()) != null) {
                         if (s.startsWith("Text:"))  { // worked
                         a= s.split(" ");
                         for(int k=1;k<a.length;k++)
                           bw.write(a[k]+"\n");          //   worked

                        }
                     }
                    br.close();
                   bw.close();

    } 
  }

1 个答案:

答案 0 :(得分:0)

原因之一可能是文件夹/home/abc/tweet/中还有其他文件的名称无法解析为Integer