2013-12-21 62 views
0

我有一個數據庫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(); 

    } 
    } 
+0

請添加整個異常(包括調用堆棧)到您的文章。^ –

+0

^也突出了行號。導致異常 ... –

+0

我最好的猜測是,這是違規行:'if(Integer.parseInt(file.getName())== uid)'。你可能想用'try-catch'模塊來包圍它,以捕獲['NumberFormatException'](http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html)並跳過文件名(如果這是你想要的)。 –

回答

0

一個原因可能是,也有與不能被解析爲Integer名稱的文件夾中/home/abc/tweet/其他文件。