문제

Hi all after getting some advice, I am attempting to use the filewriter method in order to export my google analytics queries that i got to a CSV file format here is what i have so far

   private static void printGaData(GaData results) {

   try {
     PrintWriter pw = new PrintWriter(BufferedWriter(new FileWriter("data.csv")));
     }
     catch(Exception e) {
     e.printStackTrace();
     }

    System.out.println(
        "printing results for profile: " + results.getProfileInfo().getProfileName());

    if (results.getRows() == null || results.getRows().isEmpty()) {
      System.out.println("No results Found.");
    } else {

      // Print column headers.
      for (ColumnHeaders header : results.getColumnHeaders()) {
        System.out.printf(header.getName() + ", ");
      }
      System.out.println();

      // Print actual data.
      for (List<String> row : results.getRows()) {
        for (String column : row) {
          pw.printf(row + ", ");
        }
        pw.println();
      }

     pw.println();

    }

  }
}

doesnt output any data and keeps saying that the pw is non extent and stuff like that

도움이 되었습니까?

해결책

Your PrintWriter is inside the try catch block. If you define it outside like

PrintWriter pw = null; try { pw = new PrintWriter(BufferedWriter(new FileWriter("data.csv"))); } catch (Exception e) { // handle exception }

then it will be available to the rest of your code.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top