Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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 使用PrintWriter将Google analytics数据提取到CSV文件_Java_Csv_Google Analytics_Filewriter_Printwriter - Fatal编程技术网

Java 使用PrintWriter将Google analytics数据提取到CSV文件

Java 使用PrintWriter将Google analytics数据提取到CSV文件,java,csv,google-analytics,filewriter,printwriter,Java,Csv,Google Analytics,Filewriter,Printwriter,大家好,我正在尝试使用printwriter通过JAVA从google analytics提取数据 原始代码 private static void printGaData(GaData results) { //csv printer PrintWriter pw = null; try { pw = new PrintWriter(new BufferedWriter(new FileWriter("data.csv"))); } catch

大家好,我正在尝试使用printwriter通过JAVA从google analytics提取数据

原始代码

private static void printGaData(GaData results) {

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

   //getting the queries to print

    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() + ", ");
      }
      pw.println();

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

      pw.println();

    }

  }
}
}

这是我编辑的代码,用于提取数据

    private static void printGaData(GaData results) {

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

   //getting the queries to print

    if (results.getRows() == null || results.getRows().isEmpty()) {
      pw.printf("No results Found.");
      pw.close();
    } else {

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

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

    }

  }
}
private static void printGaData(GaData结果){
//csv打印机
PrintWriter pw=null;
试一试{
pw=新的PrintWriter(新的BufferedWriter(新的FileWriter(“data.csv”)));
}
捕获(例外e){
e、 printStackTrace();
}
//获取要打印的查询
if(results.getRows()==null | | results.getRows().isEmpty()){
printf(“未发现结果”);
关闭();
}否则{
//打印列标题。
for(ColumnHeaders:results.getColumnHeaders()){
printf(header.getName()+“,”);
关闭();
}
println();
关闭();
//打印实际数据。
对于(列表行:results.getRows()){
for(字符串列:行){
pw.printf(列+“,”);
关闭();
}
println();
关闭();
} 
println();
关闭();
}
}
}
感谢所有的帮助,我对java/编程非常陌生

你们是作家和读者的混合体

  • 您需要一个Reader/InputStream来获取输入数据
  • 您需要一个Writer/OutputStream来写入输出数据
因此,首先需要从
data.csv
读取数据。您可以使用
输入流
读取器
,并包装在
缓冲读取器
缓冲输入流
中。在本例中,我将使用阅读器,因为我认为它们更容易处理文本文件

BufferedReader br = new BufferedReader (new FileReader (new File ("data.csv")));
然后,你读每一行

String buf;
while ((buf=br.readLine())!=null){
   String [] row = buf.split (",");
   //Do whatever you want with your row. You can also save it in a List
}
如果要打印文件内容,只需打印每一行-
System.out.println(行)

如果要将行写入另一个文件,则需要编写器。用BufferedWriter包装的文件写入程序应该可以。 当然,你可以使用你想要的每一位作家。这取决于你在做什么。PrintWriter也可以。无论如何,记住刷新并关闭每个Writer/OutputStream,以及关闭每个Reader/InputStream

希望这有帮助。
Giacomo

基于到目前为止的讨论,您的(第二个)代码的一个改编版本,重点是解决我可以发现的紧迫问题:

  • pw.close()
    只需在最后一次打印操作后执行一次
  • 如果文件打开失败,则无法继续
  • 将“无数据”消息写入csv文件可能不是最佳解决方案
  • 因为您的代码不使用printf的工具,所以最好只使用print/println

private static void printGaData(GaData结果){
//csv打印机
PrintWriter pw=null;
试一试{
pw=新的PrintWriter(新的BufferedWriter(新的FileWriter(“data.csv”)));
}捕获(例外e){
System.out.println(“我无法打开输出csv文件,请参阅下面的stacktrace:”);
e、 printStackTrace();
}
//如果pw在这里仍然为null,则PrintWriter不可用,继续将毫无意义
如果(pw==null){
返回;
}
//获取要打印的查询
if(results.getRows()==null | | results.getRows().isEmpty()){
//虽然技术上没有“错误”,但在这里使用printf并不是最好的解决方案
println(“未发现结果”);
//但最好只在输出窗口中写入该消息,而不是在csv文件中
System.out.println(“未找到结果”);
}否则{
//打印列标题。
for(ColumnHeaders:results.getColumnHeaders()){
打印(header.getName()+“,”);
}
println();
//打印实际数据。
对于(列表行:results.getRows()){
for(字符串列:行){
pw.print(column+“,”;//后面的空格是不需要的,甚至可能会导致一些应该读取csv文件的程序出现问题
}
println();
}
//好的,全部完成,关闭打印机
关闭();
}
}

您将System.out和pw作为写入操作的目标,这两种方法严重混淆,据我所知,您只需在pw上执行println(即,向其写入换行符)…好吧,我想关闭它会有帮助吗?pw.close()再次阅读我的评论:如果执行一组
System.out.printf(column+“,”)
后跟一个
pw.println()您认为数据将实际写入何处?是的,您需要
pw.close()文件,但在您实际写入该文件之前,您将只关闭一个空文件……这样做如何:只需替换
System.out.printf(column+“,”)带有
pw.printf(列+“,”)并添加
pw.close()在编写完成后。好的,我这样做了,但我唯一的问题是,只有第一个值显示在csv文件上,其余的值没有显示任何想法?OP希望来自
GaData results
的数据最终显示在该csv文件中。。。他只需要把它们写到
pw
而不是
System.out
。嗯,是的,这确实帮助我理解了这种写作方式是如何工作的,但我很难将你的解释纳入我的代码中,在代码中我打印了一些东西,尽管OP接受了这个答案:OP在问题标题中清楚地说明了这一点“使用PrintWriter将谷歌分析数据提取到CSV文件”。除了OP混淆了
pw
System.out
和缺少
close()
OP的代码实际上是正确的。你的答案事实上是正确的,但与OP的问题无关,而且似乎已经设法比OP更多地使用了OP。嗯,我经常使用Google Analytics,我知道OP想要屏幕打印分析报告,这些报告已在年导出。
String buf;
while ((buf=br.readLine())!=null){
   String [] row = buf.split (",");
   //Do whatever you want with your row. You can also save it in a List
}
private static void printGaData(GaData results) {

    //csv printer
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new BufferedWriter(new FileWriter("data.csv")));
    } catch (Exception e) {
        System.out.println("I could not open the output csv file, see stacktrace below:");
        e.printStackTrace();
    }

    // If pw is still null here, the PrintWriter is not available and continuing would be pointless
    if (pw==null) {
        return;
    }

    //getting the queries to print
    if (results.getRows() == null || results.getRows().isEmpty()) {
        // Although technically not ''wrong'', using printf here is not the best solution
        pw.println("No results Found.");
        // But it might be better to just write that message in the output window, and not in the csv file
        System.out.println("No results Found.");
    } else {

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

        // Print actual data.
        for (List<String> row : results.getRows()) {
            for (String column : row) {
                pw.print(column + ", "); // that space after , is not needed and might even cause problems for some programs that are supposed to read the csv file
            }
            pw.println();
        }
        // ok, all done, close that PrintWriter
        pw.close();

    }

}