這是讀取文件並將差異寫入excel的每個單元格的程序。
我面臨的一個問題是,控制檯中的輸出顯示i的遞增值,但它不會將值寫入所有索引,而只寫入最後一個索引。
我是新來的java,並嘗試對代碼進行更改,但沒有任何工作。無法在Excel的單元格中打印。 JAVA
下面是我的代碼:在控制檯上
FileInputStream fstream = new FileInputStream("C:\\Users\\Vishal\\workspace\\timestampAutomation\\bin\\com\\time\\output\\myoutput1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String timestamp="";
String value="";
int count = 0;
int i = 0;
ArrayList words=new ArrayList<String>();
Pattern p = Pattern.compile("\\bSYSTEM:TIMESTAMP\\b", Pattern.CASE_INSENSITIVE);
while ((strLine = br.readLine()) != null)
{
String[] words1=strLine.split(",");
words.addAll(Arrays.asList(words1));
}
System.out.println("WORDS LENGTH:"+words.size());
for (String word : (ArrayList<String>)words)
{
Matcher m=p.matcher(word);
count++;
if (m.find())
{
if(count<words.size()-1)
{
String tmp=(String)words.get(count);
String[] tmpArr=tmp.split("=");
timestamp=tmpArr[1];
String val=(String)words.get(count+1);
String[] valArr=val.split("=");
value=valArr[1];
}
System.out.println("Timestamp:"+timestamp+"\tValue:"+value);
//Splitting output into data format given
//Splitting output into data format given
String year=value.substring(0, 4);
String mnt=value.substring(4, 6);
String day=value.substring(6, 8);
String hr=value.substring(8, 10);
int hours=Integer.parseInt(hr)-2;
String min=value.substring(10, 12);
String sec=value.substring(12, 14);
String valueCon=year+"/"+mnt+"/"+day+" "+String.valueOf(hours)+":"+min+":"+sec;
long newtime= Long.parseLong(timestamp);
Date currentDate = new Date(newtime - TimeUnit.MINUTES.toMillis(330));
String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(currentDate);
String dateStart = timeStamp;
String dateStop = valueCon;
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
long duration = d1.getTime() - d2.getTime();
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
System.out.println("hbase Timestamp "+timeStamp);
System.out.println("Value: "+valueCon);
System.out.println("Difference of Timestamp in Seconds:"+diffInSeconds);
//printing values in excel
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheet");
Row row = sheet.createRow((short) 0);
row.createCell(i).setCellValue(diffInSeconds);
FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Vishal\\workspace\\timestampAutomation\\bin\\com\\time\\output\\helloworl.xls");
wb.write(fileOut);
fileOut.close();
i++;
}
}
}
輸出:
WORDS LENGTH:123
Timestamp:1504767614024 Value:20170907090000
hbase Timestamp 2017/09/07 07:00:14
Value: 2017/09/07 7:00:00
Difference of Timestamp in Seconds:14
current value of i 0 Timestamp:1504767614025 Value:20170907090000
hbase Timestamp 2017/09/07 07:00:14
Value: 2017/09/07 7:00:00
Difference of Timestamp in Seconds:14
current value of i 1 Timestamp:1504767614029 Value:20170907090000
hbase Timestamp 2017/09/07 07:00:14
Value: 2017/09/07 7:00:00
Difference of Timestamp in Seconds:14
current value of i 2 Timestamp:1504767614030 Value:20170907090000
hbase Timestamp 2017/09/07 07:00:14
Value: 2017/09/07 7:00:00
Difference of Timestamp in Seconds:14
current value of i 3
不過,這並不在所有四個指標進行打印(細胞)在Excel中。
我失去了一些東西,請大家幫我:
哦,這是該死的愚蠢的錯誤的邏輯,謝謝你@蒂莫西指出的錯誤。 – Neha