1
由於某些原因,我的程序不會寫入該文件,即使我能夠使try catch塊無誤並運行。是否有一個原因,我無法訪問try塊並創建此文件?在try catch塊中訪問和運行代碼的問題
public static void main(String[] args) {
BufferedReader crunchifyBuffer = null;
try {
String crunchifyLine;
crunchifyBuffer = new BufferedReader(new FileReader("/Users/moo/Desktop/test.csv"));
// How to read file in java line by line?
while ((crunchifyLine = crunchifyBuffer.readLine()) != null) {
ArrayList<String> parsedData = new ArrayList<String>();
parsedData.add(crunchifyCSVtoArrayList(crunchifyLine).get(0));
parsedData.add(crunchifyCSVtoArrayList(crunchifyLine).get(7));
parsedData.add(crunchifyCSVtoArrayList(crunchifyLine).get(8));
System.out.println("\n"+ parsedData + "\n");
String ID = parsedData.get(0);
String[] aVPayload = new String[3];
aVPayload[0] = ID;
String holder = sdkCall(aVPayload);
try {
JSONObject jsonObj = new JSONObject(holder);
String Lat = jsonObj.getJSONObject("geo").getString("latitude");
String Long = jsonObj.getJSONObject("geo").getString("longitude");
try {
PrintWriter writer = new PrintWriter("addressValidityReport.txt", "UTF-8");
writer.println("ID" + "\t" + "CSV Lat" + "\t" + "CSV Long" + "\t" + "Lat" + "\t" + "Long" + "\t" + "Status");
while ((crunchifyLine = crunchifyBuffer.readLine()) != null) {
writer.println((crunchifyCSVtoArrayList(crunchifyLine).get(0)) + "\t" + (crunchifyCSVtoArrayList(crunchifyLine).get(7)) + "\t" + (crunchifyCSVtoArrayList(crunchifyLine).get(8)) + "\t" + Lat + "\t" + Long + "\t" + "FLAGGED" + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("ERROR, CHECK FILE PRINTING!");
}
} catch (JSONException e) {
System.out.println("unexpected JSON exception");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (crunchifyBuffer != null) crunchifyBuffer.close();
} catch (IOException crunchifyException) {
crunchifyException.printStackTrace();
}
}
}
它運行,但沒有創建文件。有兩個捕獲異常會導致這種情況嗎?
如果」重新得到沒有例外拋出,那麼你的CSV文件是空的? – Makoto
nope它是填充的,當我使包含PrintWriter的try catch部分獨立時,它會創建文件,但不能訪問'Lat''Long'字符串變量,我必須將它們清零。 – kastsabug
您是否嘗試過使用您的調試器單步執行代碼?這將使您能夠精確地查看代碼中每個點所設置的每個變量。 – Jason