我试图实现的是从Excel工作表(保存在Txt中,制表符分隔)中逐行读取数据,并且每个单独的列都是我想要存储在数组中的不同数据。
我尝试了不同的方法..我甚至从网上下载了CSVReader class,但它不能用。至少这次它读的是真实的人物,而不是讽刺。
我现在的版本是使用bufferedReader和String Tokenizer。但它没有正确阅读。
下面是代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import com.csvreader.CsvReader;
import au.com.bytecode.opencsv.CSVReader;
public class excelToText{
public static void main(String[] args) throws IOException {
try
{
//csv file containing data
BufferedReader CSVFile = new BufferedReader(new FileReader("C:/Users/nhajjar/workspace/MB/src/yoyo.txt"));
// Read first line.
String dataRow;
int lineNumber = 0;
while ((dataRow = CSVFile.readLine() )!= null)
{
String [] dataArray;
lineNumber++;
String delimiter = "\t";
/* given string will be split by the argument delimiter provided. */
dataArray = dataRow.split(delimiter);
/* print substrings */
for(int i =0; i
String PrinterName = dataArray[0];
String Model = dataArray[1];
String IP = dataArray[2];
String Location = dataArray[3];
String Department = dataArray[4];
String PrimServer = dataArray[5];
String SecServer = dataArray[6];
String ShareName = dataArray[7];
String GroupNamePrefix = dataArray[8];
String GroupNameSuffix = dataArray[9];
String GroupNameFinal = dataArray[10];
String WSPPrefix = dataArray[11];
String WSPFull = dataArray[12];
String PrimWSP = dataArray[13];
String SecWSP = dataArray[14];
System.out.println("PrinterName is : " + PrinterName);
System.out.println("Model is : " + Model);
System.out.println("IP is : " + IP);
System.out.println("Location is : " + Location);
System.out.println("Department is : " + Department);
System.out.println("PrimServer is : " + PrimServer);
System.out.println("SecServer is : " + SecServer);
System.out.println("ShareName is : " + ShareName);
System.out.println("GroupNamePrefix is : " + GroupNamePrefix);
System.out.println("GroupNameSuffix is : " + GroupNameSuffix);
System.out.println("GroupNameFinal is : " + GroupNameFinal);
System.out.println("WSPPrefix is : " + WSPPrefix);
System.out.println("WSPFull is : " + WSPFull);
System.out.println("PrimWSP is : " + PrimWSP);
System.out.println("PrimWSP is : " + PrimWSP);
System.out.println("SecWSP is : " + SecWSP);
/*//writing file for ones that were not sent out.
File file = new File("write.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(file));
*/
}
CSVFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e) {
System.out.println("Exception while reading/writing csv file: " + e);
}// end Exceptions
}// end try block
}
输入为:
Printer name Model IP Location Department Primary Server Secondary Server Share Name
Boundary Sprinter Techs Lexmark E360dn Boundary Sprinter s173m928site s173mho1site 928-Sprinter-techs-L-E360dn
Boundary Sprinter Xerox 7232 Xerox WorkCentre 7232 Boundary Sprinter s173m928site s173mho1site 928-Sprinter-WC7232
Boundry Parts HP LaserJet P2055dn Boundary Parts s173m928site s173mho1site 928-Parts-LJ-P2055dn
Boundry Sales HP Color LaserJet CP4005 Boundary Sales s173m928site s173mho1site 928-Sales-Main-CP4005
Boundry Techs East HP LaserJet P3015 Boundary Techs East s173m928site s173mho1site 928-Techs-east-LJ-P3015
Boundry Techs West Lexmark E352dn Boundary Techs West s173m928site s173mho1site 928-Techs-west-L-E352dn
Concord Lexmark E360dn Concord s173mho1site
Dundas Parts Xerox WorkCentre 7232 Dundas Parts s173m910site s173mho1site 910-Parts-WC-7232
Dundas Preowned Xerox WorkCentre 7425 Dundas Preowned s173m910site s173mho1site 910-PreOwned-WC-7425
Dundas Sales 2nd Floor HP Color LaserJet CP4025 Dundas Sales s173m910site s173mho1site 910-Sales-2nd-CP4025
Dundas Sales Main Floor HP Color LaserJet CP4025 Dundas Sales s173m910site s173mho1site 910-Sales-Main-CP4025
output im getting is :
PrinterName is : Printer name
Model is : Model
IP is : IP
Location is : Location
Department is : Department
PrimServer is : Primary Server
SecServer is : Secondary Server
ShareName is : Share Name
GroupNamePrefix is : Group Name Prefix
GroupNameSuffix is : Group Name Suffix
GroupNameFinal is : Group Name Final
WSPPrefix is : WSP Prefix
WSPFull is : WSP Full
PrimWSP is : Primary WSP
PrimWSP is : Primary WSP
SecWSP is : Secondary WSP
PrinterName is : Boundary Sprinter Techs
Model is : Lexmark E360dn
IP is : 53.254.177.138
Location is : Boundary
Department is : Sprinter
PrimServer is : s173m928site
SecServer is : s173mho1site
ShareName is : 928-Sprinter-techs-L-E360dn
GroupNamePrefix is : D173_PRINTER-
GroupNameSuffix is : 928-Sprinter-techs-L-E360dn
GroupNameFinal is : D173_PRINTER-928-Sprinter-techs-L-E360dn
WSPPrefix is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\
WSPFull is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\D173_PRINTER-928-Sprinter-techs-L-E360dn.wsp;D173_PRINTER-928-Sprinter-techs-L-E360dn
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Sprinter-techs-L-E360dn
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Sprinter-techs-L-E360dn
SecWSP is : ;>;%;\\s173mho1site.cambc.corpintra.net\928-Sprinter-techs-L-E360dn
PrinterName is : Boundary Sprinter Xerox 7232
Model is : Xerox WorkCentre 7232
IP is : 53.254.177.136
Location is : Boundary
Department is : Sprinter
PrimServer is : s173m928site
SecServer is : s173mho1site
ShareName is : 928-Sprinter-WC7232
GroupNamePrefix is : D173_PRINTER-
GroupNameSuffix is : 928-Sprinter-WC7232
GroupNameFinal is : D173_PRINTER-928-Sprinter-WC7232
WSPPrefix is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\
WSPFull is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\D173_PRINTER-928-Sprinter-WC7232.wsp;D173_PRINTER-928-Sprinter-WC7232
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Sprinter-WC7232
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Sprinter-WC7232
SecWSP is : ;>;%;\\s173mho1site.cambc.corpintra.net\928-Sprinter-WC7232
PrinterName is : Boundry Parts
Model is : HP LaserJet P2055dn
IP is : 53.254.193.222
Location is : Boundary
Department is : Parts
PrimServer is : s173m928site
SecServer is : s173mho1site
ShareName is : 928-Parts-LJ-P2055dn
GroupNamePrefix is : D173_PRINTER-
GroupNameSuffix is : 928-Parts-LJ-P2055dn
GroupNameFinal is : D173_PRINTER-928-Parts-LJ-P2055dn
WSPPrefix is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\
WSPFull is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\D173_PRINTER-928-Parts-LJ-P2055dn.wsp;D173_PRINTER-928-Parts-LJ-P2055dn
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Parts-LJ-P2055dn
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Parts-LJ-P2055dn
SecWSP is : ;>;%;\\s173mho1site.cambc.corpintra.net\928-Parts-LJ-P2055dn
PrinterName is : Boundry Sales
Model is : HP Color LaserJet CP4005
IP is : 53.254.193.117
Location is : Boundary
Department is : Sales
PrimServer is : s173m928site
SecServer is : s173mho1site
ShareName is : 928-Sales-Main-CP4005
GroupNamePrefix is : D173_PRINTER-
GroupNameSuffix is : 928-Sales-Main-CP4005
GroupNameFinal is : D173_PRINTER-928-Sales-Main-CP4005
WSPPrefix is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\
WSPFull is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\D173_PRINTER-928-Sales-Main-CP4005.wsp;D173_PRINTER-928-Sales-Main-CP4005
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Sales-Main-CP4005
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Sales-Main-CP4005
SecWSP is : ;>;%;\\s173mho1site.cambc.corpintra.net\928-Sales-Main-CP4005
PrinterName is : Boundry Techs East
Model is : HP LaserJet P3015
IP is : 53.254.193.220
Location is : Boundary
Department is : Techs East
PrimServer is : s173m928site
SecServer is : s173mho1site
ShareName is : 928-Techs-east-LJ-P3015
GroupNamePrefix is : D173_PRINTER-
GroupNameSuffix is : 928-Techs-east-LJ-P3015
GroupNameFinal is : D173_PRINTER-928-Techs-east-LJ-P3015
WSPPrefix is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\
WSPFull is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\D173_PRINTER-928-Techs-east-LJ-P3015.wsp;D173_PRINTER-928-Techs-east-LJ-P3015
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Techs-east-LJ-P3015
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Techs-east-LJ-P3015
SecWSP is : ;>;%;\\s173mho1site.cambc.corpintra.net\928-Techs-east-LJ-P3015
PrinterName is : Boundry Techs West
Model is : Lexmark E352dn
IP is : 53.254.193.221
Location is : Boundary
Department is : Techs West
PrimServer is : s173m928site
SecServer is : s173mho1site
ShareName is : 928-Techs-west-L-E352dn
GroupNamePrefix is : D173_PRINTER-
GroupNameSuffix is : 928-Techs-west-L-E352dn
GroupNameFinal is : D173_PRINTER-928-Techs-west-L-E352dn
WSPPrefix is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\
WSPFull is : #;\D173\_GLOBALRESOURCES\GROUPS\Printers\D173_PRINTER-928-Techs-west-L-E352dn.wsp;D173_PRINTER-928-Techs-west-L-E352dn
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Techs-west-L-E352dn
PrimWSP is : >;%;\\s173m928site.cambc.corpintra.net\928-Techs-west-L-E352dn
SecWSP is : ;>;%;\\s173mho1site.cambc.corpintra.net\928-Techs-west-L-E352dn
Exception while reading/writing csv file: java.lang.ArrayIndexOutOfBoundsException: 7
注意:我截断了输出。三个以上的这些“空块”
我建议使用<code>字符串。split()函数,因为这将大大简化代码。
代币仅按空格和加号划分。这是因为 StringTokenizer
的第二个参数不允许正则表达式。尝试使用“\t”
,字段似乎实际上是分开的。
顺便说一下,你真的不需要每行100个字符串数组。
看起来你把事情复杂化了。CSV文件包含逗号分隔的值。因此,您的文件数据应该类似于:
第1行:单元格1、单元格2、单元格3、单元格4等。。。
第2行:行2的单元格1、行2的单元2等。。。
您使用了制表符分隔的文件,所以您需要在制表符上分割
//Read first line
String dataRow;
int lineNumber = 0;
while((dataRow = CSVFile.readline()) != null)
{
String [] dataArray;
lineNumber++;
String delimiter = "\t";
dataArray = dataRow.split(delimiter); //Now dataArray contains all the tab delimited cells that were on line one of the .txt file
//Start assigning the information to your variables since it is stored in dataArray
String PrinterName = dataArray[0];//This would assign the first cell (row 1, column 1 in excel) that was read from the text file. From looking at your input this should be "Printer name"
String Model = dataArray[1];
String IP = dataArray[2];
//etc...Assign the rest
//Print your output
//Do anything else you need to do
}//end the while loop
以下是一些来源:
http://www.java-examples.com/java-string-split-example
注意:这个例子更接近你正在做的只是使用"\t"而不是",":http://www.daniweb.com/software-development/java/threads/17262/reading-in-a-.csv-file-and-loading-the-data-into-an-array
希望这有帮助
问题内容: 我想知道我是否打算以正确的方式分割字符串?我的代码是: 我只需要字符串的第一部分,这就是为什么我返回第一项的原因。我问是因为我在API中注意到这意味着任何字符,所以现在我陷入了困境。 问题答案: 接受正则表达式,因此你需要转义以免将其视为正则表达式元字符。这是一个例子:
问题内容: 这个问题已经在这里有了答案 : 8年前关闭。 可能重复: PHP / MYSQL在WHERE子句中使用数组 我有一个ID值为[1,5,2,6,7 …]的数组,我需要在MySQL item_id IN(1,5,2,6,7 …)语句中使用该数组以仅选择在阵列中列出ID的行。如何将$ arrIDs转换为可插入SQL查询的内容? EDIT-通话内容: 问题答案: 使用;
如何将过滤器列表拆分为单个过滤器元件?split2String在线程“main”java.util.regex中导致:异常。PatternSyntaxException:索引10或(|和)附近的未闭合组(
我是 Perl 的新手,但根据我阅读的文档,看起来 Perl 中的 split 函数要求正则表达式模式而不是字符串分隔符作为第一个参数,但我发现使用 之类的东西仍然可以正确拆分字符串。 基于此,我尝试使用可变分隔符(例如。< code>print (split($var,$ string))[0] where < code > $ var = ' ' )并发现它不起作用。我做错了什么? 谢谢! 编
问题内容: 我有一个类似下面的字符串行: A:B:C:D:E:F:G:H:I:J:K:L:M 这意味着定界符(:)的计数为12。这行是有效的。 现在假设您有以下一行: A:B:C:D:E:F:G:H ::::: 这行也是有效的,因为它包含12个定界符。其中存在8个值,而4个值为空白。 现在,以下行应该无效: A:B:C:D:E:F:-无效-因为它仅包含6个值,但预期为12个。 这该怎么做 .. ?
问题内容: 我有一个像这样的字符串: 我想拆分该字符串并选择作为分隔符。 我的代码如下所示: 我得到的是一个包含所有字符作为一个条目的数组: 有人知道为什么吗? 我不能用分割字符串吗? 问题答案: 在RegEx中被视为。因此,您需要对其进行转义: