下面的 java 代码只是读取 csv 文件并将数据发送到本地主机上的端口 2003。我在我的 Windows 机器上尝试过,效果很好。但是,当我尝试在 ubuntu 14.04 计算机上运行此文件时,出现运行时错误,提示 ArrayIndexOutOfBoundsException。不确定出了什么问题,我什至为 ubuntu 上的 csv 文件提供了正确的路径?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at parseFile.run(parseFile.java:39)
at parseFile.main(parseFile.java:19)
.
Line 37 String[] data = line.split(cvsSplitBy);
Line 38 System.out.println(line);
Line 39
Line 40 String env = data[2];
解析文件.java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class parseFile {
public static void main(String[] args) throws Exception {
parseFile obj = new parseFile();
System.out.println("In Main Method");
obj.run();
}
public void run() {
String csvFile = "/root/javafiles/sum.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = " +";
System.out.println(csvFile);
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use space as separator
String[] data = line.split(cvsSplitBy);
System.out.println(line);
String env = data[2];
String date = data[0];
String time = data[1];
int assigned = Integer.parseInt(data[3]);
int pooled = Integer.parseInt(data[4]);
int dbavailable = assigned + pooled;
int max = Integer.parseInt(data[6]);
long epoch = 0;
try {
String str = "2014-07-04 04:05:10";
String appenddate= date +" " +time;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date datenew = df.parse(appenddate);
epoch = datenew.getTime();
System.out.println(epoch); // 1055545912454
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(env.equals("yy")){
System.out.println("Sending");
Socket conn = new Socket("127.0.0.1", 2003);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes("sqldbyp.capacity.assigned "+assigned+" "+epoch+"\n");
conn.close();
System.out.println("Sent");
try {
Thread.sleep(500000);
System.out.println("Sleeping for 5 minutes");
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}
总和.csv
2014-07-04 04:05:10 yy 499 303 24 831 825 0 24 757 858 7
2014-07-04 05:14:09 yy 501 301 24 831 825 0 24 769 865 7
2014-07-04 06:14:47 yy 505 295 24 830 824 0 24 773 870 8
2014-07-04 07:14:09 yy 511 289 24 831 825 0 24 781 875 9
2014-07-04 08:14:10 yy 511 292 23 831 825 0 23 786 881 7
2014-07-04 09:15:02 yy 512 288 23 829 823 0 23 790 886 10
2014-07-04 10:14:10 yy 516 283 24 829 823 0 24 792 897 8
2014-07-04 11:14:10 yy 517 280 24 830 823 0 24 809 906 11
2014-07-04 12:14:09 yy 519 284 23 831 825 0 23 821 915 7
2014-07-04 13:14:10 yy 519 284 23 831 825 0 23 822 922 7
最佳答案
让我们添加一些检查,并记录您收到的数据 -
System.out.println(line);
String[] data = line.split(cvsSplitBy);
System.out.println(Arrays.toString(data));
String date = (data != null && data.length > 0) ? data[0] : null;
String time = (data != null && data.length > 1) ? data[1] : null;
String env = (data != null && data.length > 2) ? data[2] : null;
不要忘记您现在必须检查您的字段是否为空。您可能也更喜欢
if (data == null || data.length < 3) {
continue;
}
String env = data[2];
String date = data[0];
String time = data[1];
关于Java 无法在 Windows 上的 ubuntu 机器上读取 csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25273732/