I m试图将多份数据清单输出到CSV档案中,时间长短不一。 每一份清单都应是CSV输出文档中的一栏。 是否有直截了当的做法? 如果我把每个名单逐个编成一行,那么我就只按每个名单排一个,在我打到最后的时候,结果就是回报,但这种办法在工作一栏时并不奏效。
我认为,一劳永逸地逐项逐项逐项逐项逐项逐项逐项逐项逐项处理,但这也会失败,因为有些清单比其他清单长。 为了纠正这种情况,我不得不在每次检查中检查该柜台是否是每份清单的结尾,在计算方面费用相当高。
感谢任何想法!
I m试图将多份数据清单输出到CSV档案中,时间长短不一。 每一份清单都应是CSV输出文档中的一栏。 是否有直截了当的做法? 如果我把每个名单逐个编成一行,那么我就只按每个名单排一个,在我打到最后的时候,结果就是回报,但这种办法在工作一栏时并不奏效。
我认为,一劳永逸地逐项逐项逐项逐项逐项逐项逐项逐项逐项处理,但这也会失败,因为有些清单比其他清单长。 为了纠正这种情况,我不得不在每次检查中检查该柜台是否是每份清单的结尾,在计算方面费用相当高。
感谢任何想法!
我认为这很简单:
public static void main(String... args) throws IOException {
ArrayList<ArrayList<String>> rows = getRandomData();
if (rows.size() == 0)
throw new RuntimeException("No rows");
// normalize data
int longest = 0;
for (List<String> row : rows)
if (row.size() > longest)
longest = row.size();
for (List<String> row : rows)
while (row.size() < longest)
row.add("");
if (longest == 0)
throw new RuntimeException("No colums");
// fix special characters
for (int i = 0; i < rows.size(); i++)
for (int j = 0; j < rows.get(i).size(); j++)
rows.get(i).set(j, fixSpecial(rows.get(i).get(j)));
// get the maximum size of one column
int[] maxColumn = new int[rows.get(0).size()];
for (int i = 0; i < rows.size(); i++)
for (int j = 0; j < rows.get(i).size(); j++)
if (maxColumn[j] < rows.get(i).get(j).length())
maxColumn[j] = rows.get(i).get(j).length();
// create the format string
String outFormat = "";
for (int max : maxColumn)
outFormat += "%-" + (max + 1) + "s, ";
outFormat = outFormat.substring(0, outFormat.length() - 2) + "
";
// print the data
for (List<String> row : rows)
System.out.printf(outFormat, row.toArray());
}
private static String fixSpecial(String s) {
s = s.replaceAll("(")", "$1$1");
if (s.contains("
") || s.contains(",") || s.contains(""") ||
s.trim().length() < s.length()) {
s = """ + s + """;
}
return s;
}
private static ArrayList<ArrayList<String>> getRandomData() {
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
String[] rand = { "Do", "Re", "Song", "David", "Test", "4", "Hohjoh", "a "h" o", "tjo,ad" };
Random r = new Random(5);
for (int i = 0; i < 10; i++) {
ArrayList<String> row = new ArrayList<String>();
for (int j = 0; j < r.nextInt(10); j++)
row.add(rand[r.nextInt(rand.length)]);
data.add(row);
}
return data;
}
Re , 4 , "tjo,ad" , "tjo,ad" ,
"tjo,ad" , "a ""h"" o" , , ,
Re , "a ""h"" o" , Hohjoh , "tjo,ad" , 4
4 , David , , ,
4 , Test , "tjo,ad" , Hohjoh , Re
Do , Hohjoh , Test , ,
Hohjoh , Song , , ,
4 , Song , , ,
4 , Do , Song , Do ,
Song , Test , Test , ,
http://commons.apache.org/sand Box/csv/“rel=“nofollow” http://commons.apache.org/sand Box/csv/。
这还提到了一些CSV图书馆。
请注意,许多答复没有考虑含有 com体的插图。 因此,图书馆比自己做得更好。
您可使用Sting.format():
System.out.println(String.format("%4s,%4s,%4s", "a", "bb", "ccc"));
System.out.println(String.format("%4s,%4s,%4s", "aaa", "b", "c"));
其结果将是4个特性的固定栏目,只要使用的数值较短。 否则,布局就会中断。
a, bb, ccc
aaa, b, c
我根本不熟悉 Java,但如果您有matrix
导向型数据类型,你可以使用方便的排位填充,然后进行转换,然后用方便的排位书写。 如果你愿意的话,你的日常印刷工作可以产生一种无效的插播或固定的宽度。
建立一系列的主持人(每个名单各设一个)。 然后对阵列进行校对,检查代号<>hasNext();如果是的话,则输出代码
你们可以这样做:
List<List<?>> listOfLists = new LinkedList<List<?>>();
List<Iterator<?>> listOfIterators = new LinkedList<Iterator<?>>();
for (List<?> aList : listOfLists) {
listOfIterators.add(aList.iterator());
}
boolean done = false;
while(!done)
{
done = true;
for (Iterator<?> iter : listOfIterators)
{
if (iter.hasNext())
{
Object obj = iter.next();
//PROCESS OBJ
done = false;
}
else
{
//PROCESS EMPTY ELEMENT
}
}
}
CSV处理 我曾多次使用这一图书馆:。 简单易懂。
Cheerz!
每次核对时,我必须检查对照表是否是每份清单的结尾,从计算来看,这种核对费用相当昂贵。
Get over it. This will, realistically, be small compared to the cost of actually doing the iteration, which in turn will be tiny compared to the cost of writing any given bit of text to the file. At least, assuming you have random access containers.
但是,在反向和指数化方面,你不应有思维;你应当从变体的角度来思考(这排除了随机接触的问题,简化了守则)。
如果你想用一种 lo和一种方法做到这一点,你可以做如下工作。
public static void writeCSV(PrintWriter pw, List<List<String>> columnsRows) {
for(int i=0;;i++) {
StringBuilder line = new StringBuilder();
boolean empty = true;
for (List<String> column : columnsRows) {
String text = i < column.size() ? column.get(i) : "";
found &= i >= column.size();
if (text.contains(",") || text.contains(""") || text.contains("
") || text.trim() != text)
text = " + text.replaceAll(""", """") + " ;
line.append(text).append( , );
}
if (empty) break;
pw.println(line.substring(0, line.length()-1));
}
}
作为一项行动,你可以用一只 lo子来做,但不清楚它做什么。
采用@dacwe的抽样数据,这一方法需要10个我们(micro-seconds)。
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...