[英]Keep the delimiter at third position and rest of other split
我正在尝试在需要跳过的(逗号)定界符文件中分割(逗号)第三个位置,其余的(逗号)可以分割。
我的代码:
String st;
BufferedReader Br = null;
FileOutputStream outFile1 = new FileOutputStream(
new File("C:\\DATA\\data.xls"));
Workbook book = new HSSFWorkbook();
File objFile = new File(
"C:\\DATA\\user.txt");
Br = new BufferedReader(new FileReader(objFile));
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(objFile));
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
Sheet sheet = book.createSheet("UserData");
System.out.println("Total Rows in the File : " +lines);
int line = 0;
while ((st = Br.readLine()) != null) {
String value = st.replace("\"", "");
arraylist = value.split(",");
Row row = null;
Cell cell = null;
row = sheet.createRow(line);
for (int i = 0; i < arraylist.length; i++) {
// System.out.println(arraylist[i]);
cell = row.createCell(i);
cell.setCellValue(arraylist[i]);
}
line++;
// System.out.println("Line: " + line);
}
book.write(outFile1);
outFile1.close();
Br.close();
我的txt文件的外观:
"userid","Subscriberid ","HeadhouseFullname",
"167fgfg611","5904fds02","ABC, XYZ C"
"200fhskdhf","876fsgj25","ACD, NNP C"
"3893fjs956","502sgfgg3","ADC, KIO C"
"918shdfd71","1029gsg57","AED, JUI C"
当前,在执行代码后,它会打印以下文件值:
userid Subscriberid HeadhouseFullname
167fgfg611 5904fds02 ABC XYZ C
200fhskdhf 876fsgj25 ACD NNP C
3893fjs956 502sgfgg3 ADC KIO C
918shdfd71 1029gsg57 AED JUI C
应如何打印:
userid Subscriberid HeadhouseFullname
167fgfg611 5904fds02 ABC, XYZ C
200fhskdhf 876fsgj25 ACD, NNP C
3893fjs956 502sgfgg3 ADC, KIO C
918shdfd71 1029gsg57 AED, JUI C
在这里您可以注意到HeadhouseFullname
列值是全名。 例如"ABC, XYZ C"
,我不想在文件中用(逗号分隔符)来分隔全名。 我想保留它为"ABC, XYZ C"
。 目前,它会在任何地方分隔(逗号)分隔符。
我同意您应该使用如上所述的CSV库,但是如果您想继续沿当前路径前进,请尝试将拆分逻辑更新为:
while ((st = Br.readLine()) != null) {
arraylist = st.split(",");
Row row = null;
Cell cell = null;
row = sheet.createRow(line);
for (int i = 0; i < arraylist.length; i++) {
// System.out.println(arraylist[i]);
cell = row.createCell(i);
cell.setCellValue(arraylist[i].replace("\"", ""));
}
line++;
//System.out.println("Line: " + line);
}
您可以开始在"
字符,即st.split("\\"")
上分割行。 此时,结果数组将包含您感兴趣的条目以及另外两种字符串:empty和,
only character。
String[] values = str.split("\"");
完成此操作后,您可以仅考虑和处理条目,从而遍历结果数组,如下所示:
for (int valueIndex = 0; valueIndex < values.length; valueIndex++) {
if (values[valueIndex].length() > 0 && !values[valueIndex].equals(",")) {
// DO SOMETHING WITH values[valueIndex]...
}
}
因此,考虑到您发布的源代码,while循环将更改如下:
while ((st = Br.readLine()) != null) {
String[] values = st.split("\"");
Row row = sheet.createRow(line++);
for (int valueIndex = 0, cellIndex = 0; valueIndex < values.length; valueIndex++) {
if (values[valueIndex].length() > 0 && !values[valueIndex].equals(",")) {
Cell cell = row.createCell(cellIndex++);
cell.setCellValue(values[valueIndex]);
}
}
}
希望这可以帮助!
洛伦佐
我用以下更新的代码添加了一个附加循环,现在第三列填充了名字,姓氏和中间名首字母:
以下是我的更新代码:
String st;
BufferedReader Br = null;
FileOutputStream outFile1 = new FileOutputStream(
new File("C:\\DATA\\data.xls"));
Workbook book = new HSSFWorkbook();
File objFile = new File(
"C:\\DATA\\user.txt");
Br = new BufferedReader(new FileReader(objFile));
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(objFile));
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
Sheet sheet = book.createSheet("UserData");
System.out.println("Total Rows in the File : " +lines);
int line = 0;
while ((st = Br.readLine()) != null) {
arraylist = st.split("," + "\"");
for (int i = 0; i < arraylist.length; i++) {
arraylist[i] = arraylist[i].replace("\"", "");
}
Row row = null;
Cell cell = null;
row = sheet.createRow(line);
for (int i = 0; i < arraylist.length; i++) {
// System.out.println(arraylist[i]);
cell = row.createCell(i);
cell.setCellValue(arraylist[i]);
}
line++;
// System.out.println("Line: " + line);
}
book.write(outFile1);
outFile1.close();
Br.close();
我试过使用正则表达式,例如
字符串txt =“ 0,2,23131312,\\”这是一条消息\\“,1212312”; System.out.println(Arrays.toString(txt.split(“,(?=(?:[^ \\”] \\“ [^ \\”] \\“) [^ \\”] $)“)))));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.