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