我有一个要求,我需要进行少量Java调用并检索必要的值。这些都是字符串。我需要用逗号分隔值将它写入控制台。如下所示:
3,Till,,Till,Weiss,,
3,ugilad,,ugilad,ugilad,,
3,admintest,,admin,test,abc@sample.com,
预计应该是:
userid,firstname,lastname,email
3,Till,,Till,Weiss,,
3,ugilad,,ugilad,ugilad,,
3,admintest,,admin,test,abc@sample.com,
现在我需要为这些值添加列。说: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 :(得分: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", "abc@sample.com", "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
}
}
除此之外,您可以在代码中进行一些清理,如下所示。也可以将构造字符串添加到列表中。列表的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";
}