Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java无法在windows上运行的ubuntu机器上读取csv文件?_Java - Fatal编程技术网

Java无法在windows上运行的ubuntu机器上读取csv文件?

Java无法在windows上运行的ubuntu机器上读取csv文件?,java,Java,下面的java代码只是读取csv文件并将数据发送到localhost上的端口2003。我在我的Windows机器上试过,效果很好。然而,当我试图在我的Ubuntu14.04机器上运行这个文件时,我得到运行时错误,说ArrayIndexOutOfBoundsException。不确定是什么问题,我甚至给了ubuntu上csv文件的正确路径 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 a

下面的java代码只是读取csv文件并将数据发送到localhost上的端口2003。我在我的Windows机器上试过,效果很好。然而,当我试图在我的Ubuntu14.04机器上运行这个文件时,我得到运行时错误,说ArrayIndexOutOfBoundsException。不确定是什么问题,我甚至给了ubuntu上csv文件的正确路径

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
        at parseFile.run(parseFile.java:39)
        at parseFile.main(parseFile.java:19)

parseFile.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");

 }

}
Sum.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];
if(data==null | | data.length<3){
继续;
}
字符串env=数据[2];
字符串日期=数据[0];
字符串时间=数据[1];

Windows使用Linux以外的其他换行符,您知道吗?因为你能逐行阅读。另外,
data
如何查找异常场景?@Smutje这是我第一次在linux机器上运行java文件。。你能详细解释一下换行符吗?不,但维基百科可以:你不需要担心换行符,算了吧——java会帮你处理的。你的CSV文件结尾可能有一行空行。拆分此空行不会返回包含2个元素的数组。使用调试器一步一步地执行代码并查看问题来自何处是很简单的。甚至可以添加跟踪,以了解是哪一行导致了问题。
if (data == null || data.length < 3) {
  continue;
}
String env = data[2];
String date = data[0];
String time = data[1];