- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要实现的是从 Excel 工作表(保存在 Txt 中,制表符分隔)中逐行读取数据,每个单独的列都是我想要存储在数组中的不同数据。
我尝试了不同的方法..我什至从互联网上下载了 CSVReader 类,但它不起作用。至少这一次它读的是真实的人物而不是胡言乱语。
我现在的版本是使用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 < dataArray.length ; i++)//System.out.println(dataArray[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
注意:我截断了输出。还有更多这样的“空 block ”
最佳答案
看来你把这件事搞得太复杂了。 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
希望这有帮助
关于java - 制表符分隔的字符串到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11904693/
我正在尝试从下拉列表中创建一个多选复选框,并通过 ; 连接所选结果。 我的代码是这样的: var myobject = { ValueA : 'Text A', ValueB : 'T
我有输入,我需要获取值并用 “|” 符号分隔。 我的输入: 输出我需要的: 00:00|00:00|00:00 我的代码是: (而且它不工作) var timesArray = $('table').
我正在尝试将超过 400 万行的列拆分为 4 个新列,问题是我不知道在哪里查看或我应该使用 Google 搜索哪个术语。 (是的,我已经在 Google 和 Stack 中搜索了一个类似的问题,但只在
我有一个很大的 csv 文件,其中充满了用“|”分隔的数字字符,例如: 432452 | 543634 4122442 | 41256512 64523 | 12416 然后我读入数据如下: fs
我有一个程序可以计算多个数字的阶乘。这些数字在 cmd 中作为参数传递: factorial.exe 3 4 5 这将分别计算 3、4 和 5 的阶乘。该程序的早期版本有一个百分比显示堆栈的完整性。我
这个问题已经有答案了: 奥 git _a (6 个回答) 已关闭 9 年前。 我有一个双数“547.123456” 我只想使用这个 double 作为“547.1”,就像“.”后面只有 1 个数字 我
我有一个程序可以计算多个数字的阶乘。这些数字在 cmd 中作为参数传递: factorial.exe 3 4 5 这将分别计算 3、4 和 5 的阶乘。该程序的早期版本有一个百分比显示堆栈的完整性。我
我有一个 ArrayList,其中包含一个 messageId、一个 -、一个用户名。 示例:E123-sam 我想划分 List 的每个元素,使得 - 之前的部分进入一个 ArrayList ,而之
我目前有一个“日期”列作为 pandas 数据框的索引,其格式为: January February .... Year2 January February ... Year3 (它来自 pdf 表格
我正在尝试对我的 .mdb 数据库进行 ODBC 查询。我正在使用 mdbtools 驱动程序。该代码是使用 Eclipse 用 C 语言编写的。唯一的问题是,当我写例如: "SELECT 'last
我需要知道如何将这两个if else 部分分开。 public static int NextBday(int Bdays, int days){ int daysleft = 0;
我想计算我的员工分开但合并在一起的出勤率 My target output 我的代码 SELECT count(employees_id) as numbers FROM attendance WH
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
下面是我的代码的一小段摘录,它从 API 添加了一行。总共有很多行。 每一行包含一行数据如 TY8tr,50,34,P,SB, 数据行在数据类型上是一致的。我如何通过 ,'s 拆分数组列表中
我想获取选中的元素并用逗号分隔它们,最后一个元素用“and”分隔 它的显示输出为: 我想删除最后一个元素后面的逗号 (,) 并在它前面添加 'and',例如 Sugar, Milk and Extra
我是 JSON 的新手,但在从已解析的 JSON 对象中提取数据时遇到问题: 我有一个 getstats.php 文件,它回显 mysql 查询的 json 编码结果。以下是 php 文件返回的示例:
我有一个像这样的数字/字符串(我不确定如何将 int 与字符串相互转换) 000000122310200000223340000700012220000011411000000011011271043
标签内的文本
我想尝试一些基本的网络抓取,但遇到了一个问题,因为我习惯了简单的 td-tags,在这种情况下,我有一个网页,其中包含以下预标记和其中的所有文本,这意味着刮掉它有点棘手。 11111111 1111
出于练习目的,我正在开发 TCP 客户端/服务器系统,我想在两者之间发送特定数据。 我已经能够发送字节并让它们显示为字符串。此外,我还可以发送一个特定的字符串(“mb”)并在服务器端弹出一个 Mess
在大量的 unicode 字符中,有一些实际上表示多个字符,例如两个 'f' 字符的 U+FB00 连字 ff。有什么方法可以轻松地将这样的字符转换为多个单个字符?最好是标准 Java API 中可用
我是一名优秀的程序员,十分优秀!