2015-10-14 85 views
0

我有一個需求,我需要做幾個Java調用並檢索必要的值。這些都是字符串。我需要用逗號分隔值將其寫入控制檯。如下圖所示:Java動態列標題

3,Till,,Till,Weiss,, 
3,ugilad,,ugilad,ugilad,, 
3,admintest,,admin,test,[email protected], 

預期應該是:

userid,firstname,lastname,email 
3,Till,,Till,Weiss,, 
3,ugilad,,ugilad,ugilad,, 
3,admintest,,admin,test,[email protected], 

現在我需要將列添加到這些值。說:userId,firstName,lastName是這樣的。我如何使用Java代碼動態實現這一點?下面是我寫的代碼:

   if (usersList.totalCount != 0 && usersList.totalCount >= 1) { 
       System.out.println("usersList.totalCount ----->" 
         + usersList.totalCount); 
       for (KalturaUser user : usersList.objects) { 
        if (user != null) { 
         if (user.id != null) { 
          String userRole = getUserRole(user.id); 
          String cnum = getUserUniqueId(user.email); 
          // if (userRole != null) { 
          // if (userRole.equals("adminRole") 
          // || userRole 
          // .equals("privateOnlyRole")) { 
          // sb1.append(action); 

          if (user.id != null) { 
           sb.append(user.id); 
          } else { 
           sb.append(","); 
          } 
          String action = "1"; 

          if (cnum != null) { 
           if (userRole == null) { 
            action = "3"; 
           } 
          } else { 
           action = "3"; 
          } 
          if (action != null) { 
           sb1.append(action); 
          } 
          if (cnum != null) { 
           sb1.append(",").append(cnum); 
          } else { 
           sb1.append(",").append(user.id); 
           sb1.append(","); 
          } 

          if (user.firstName != null) { 
           sb.append(",").append(user.firstName); 
           sb1.append(",").append(user.firstName); 
          } else { 
           sb.append(","); 
           sb1.append(","); 
          } 
          if (user.lastName != null) { 
           sb.append(",").append(user.lastName); 
           sb1.append(",").append(user.lastName); 
          } else { 
           sb.append(","); 
           sb1.append(","); 
          } 
          if (userRole != null) { 
           sb.append(",").append(userRole); 
           // sb1.append(",").append(userRole); 
          } else { 
           sb.append(","); 
           // action = "3"; 
           // sb1.append(action); 
          } 
          // sb1.append("1"); 
          if (user.email != null) { 
           sb.append(",").append(user.email); 
           sb1.append(",").append(user.email); 
          } else { 
           sb.append(","); 
           sb1.append(","); 
          } 

          if (userRole != null) { 
           sb1.append(",").append(userRole); 
          } else { 
           sb1.append(","); 
          } 

          // sb1.append("1"); 

          if (user.partnerData != null) { 
           if (user.partnerData.startsWith("pw") 
             && user.partnerData.length() == 43) { 
            sb.append(","); 
           } 
           if (user.partnerData.length() > 43) { 
            String partnerData = user.partnerData 
              .substring(44); 
            sb.append(",").append(partnerData); 
           } 
           if (!user.partnerData.startsWith("pw")) { 
            sb.append(",").append(user.partnerData); 
           } 
          } 
          sb.append(System.getProperty("line.separator")); 
          sb1.append(System.getProperty("line.separator")); 
         } 
        } 
       } 
      } 
      // System.out.println(sb); 
      System.out.println(sb1); 
+0

預期輸出的任何示例? –

+0

什麼專欄?你想在控制檯中以表格形式打印嗎? –

+0

@ Jean- UserID,FirstName,LastName,然後從下一行開始,像上面的示例輸出那樣,顯示相應列的值。我已經獲得了值,但如何添加標題是問題所在。 –

回答

1

根據你的代碼要附加一切字符串緩衝區(SB),並在一次打印。因此,在循環之間,您可以創建一個字符串header並根據條件分配值。在循環之外首先打印標題,然後打印緩衝區。這將是最簡單的靈魂。但是,如果數據量較大,最好使用文件。將所有內容寫入文件,構建標題,然後在循環後打印標題並打印文件。與虛設邏輯和類

import java.io.RandomAccessFile; 
import java.util.ArrayList; 
import java.util.List; 

class UserList { 
    public int totalCount; 
    public List<KalturaUser> objects; 

    public UserList(List<KalturaUser> objects) { 
     this.objects = objects; 
     this.totalCount = (objects != null) ? objects.size() : 0; 
    } 
} 

class KalturaUser { 
    public String id; 
    public String email; 
    public String firstName; 
    public String lastName; 
    public String partnerData; 

    public KalturaUser(String id, String email, String firstName, 
      String lastName, String partnerData) { 
     this.id = id; 
     this.email = email; 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.partnerData = partnerData; 
    } 
} 

public class DynamicHeader { 

    private static final String NEW_LINE = System.getProperty("line.separator"); 

    public static void main(String[] args) throws Exception { 
     UserList usersList = init(); 
     RandomAccessFile csv = new RandomAccessFile("temp.csv","rw"); 
     csv.setLength(0); //Clears the file 
     String header = ""; 
     if (usersList.totalCount >= 1) { 
      for (KalturaUser user : usersList.objects) { 
       if (user != null && user.id != null) { 
        List<String> row = new ArrayList<String>(); 
        String userRole = getUserRole(user.id); 
        String cnum = getUserUniqueId(user.email); 
        row.add(getNullSafeValue(user.id)); 
        String action = "1"; 
        if (cnum == null || userRole == null) { 
         action = "3"; 
        } 
        row.add(action); 
        if (cnum != null) { 
         row.add(cnum); 
         header = "uniqueid,firstname,lastname,email"; 
        } else { 
         row.add(user.id); 
         header = "userid,firstname,lastname,email"; 
        } 
        row.add(getNullSafeValue(user.firstName)); 
        row.add(getNullSafeValue(user.lastName)); 
        row.add(getNullSafeValue(userRole)); 
        row.add(getNullSafeValue(user.email)); 
        if (user.partnerData != null) { 
         if (user.partnerData.startsWith("pw")) { 
          if (user.partnerData.length() == 43) { 
           row.add(""); 
          } else if (user.partnerData.length() > 43) { 
           row.add(user.partnerData.substring(44)); 
          } 
         } else { 
          row.add(user.partnerData); 
         } 
        } 
        csv.write(row.toString().replace("[", "").replace("]", "").replace(", ", ",").getBytes()); 
        csv.write(NEW_LINE.getBytes()); 
       } 
      } 
     } 
     csv.seek(0); 
     System.out.println(header); 
     String data; 
     while((data = csv.readLine()) != null){ 
      System.out.println(data); 
     } 
     csv.close(); 
    } 

    private static UserList init() { 
     List<KalturaUser> userObjs = new ArrayList<KalturaUser>(); 
     userObjs.add(new KalturaUser("1", null, "Till", "Till", "Weiss")); 
     userObjs.add(new KalturaUser("2", null, "ugilad", "ugilad", "ugilad")); 
     userObjs.add(new KalturaUser("3", "[email protected]", "admin", "test", "admintest")); 
     return new UserList(userObjs); 
    } 

    private static String getNullSafeValue(String str) { 
     return (str != null) ? str : ""; 
    } 

    private static String getUserUniqueId(String email) { 
     return (email != null) ? email.substring(0, email.indexOf("@")) : null; //Replace with proper logic 
    } 

    private static String getUserRole(String id) { 
     return ("2".equals(id)) ? "Role 2" : null; //Replace with proper logic 
    } 

} 

除了這個,你可以做你的代碼中的一些乾淨的UPS,如下面的樣本。還可以構建字符串,只需將其添加到列表即可。列表的toString給你一個逗號分隔值。

(usersList.totalCount != 0 && usersList.totalCount >= 1)  

可以減少到(usersList.totalCount > 0)

if (user != null) { 
    if (user.id != null) {} 
} 

如果你沒有做具體的事情時(user != null)那麼,這可以結合到

if (user != null && user.id != null) {} 

而且

if (cnum != null) { 
    if (userRole == null) { 
     action = "3"; 
    } 
} else { 
    action = "3"; 
} 

這可能會減少到

if (cnum == null || userRole == null) { 
    action = "3"; 
} 
+0

非常感謝你給我代碼。我需要實現這一點。請讓你知道這是否正常。 –