1

I'm currently working on a project that is done in Java, on google appengine. i have above 2000 records

Appengine does not allow files to be stored so any on-disk representation objects cannot be used. Some of these include the File class.

I want to write data and export it to a few csv files, the user to download it.

How may I do this without using any File classes? I'm not very experienced in file handling so I hope you guys can advise me.

Thanks.

user1686253
  • 21
  • 1
  • 5

3 Answers3

3

Just generate the csv in memory using a StringBuffer and then use StringBuffer.toString().getBytes() to get a byte array which can then be sent to your output stream.

For instance if using a servlet in GAE:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
  StringBuffer buffer = new StringBuffer();
  buffer.append("header1, header2, header3\n");
  buffer.append("row1column1, row1column2, row1column3\n");
  buffer.append("row2column1, row2column2, row2column3\n");
  // Add more CSV data to the buffer

  byte[] bytes = buffer.toString().getBytes();

  // This will suggest a filename for the browser to use
  resp.addHeader("Content-Disposition", "attachment; filename=\"myFile.csv\"");

  resp.getOutputStream().write(bytes, 0, bytes.length);
}

More information about GAE Servlets

More information about Content-Disposition

Community
  • 1
  • 1
rancidfishbreath
  • 3,944
  • 2
  • 30
  • 44
  • Hi thanks for the replay.I am new with google app please tell me how to add the csv to buffer and after writing the csv file i want to download that file how can i do – user1686253 Dec 10 '12 at 19:10
  • thanks for the replay i added data to StringBuffer but where i create the file after write file i want to downalodd the file.pleae help me – user1686253 Dec 10 '12 at 19:25
1

You can store data in memory using byte arrays, stings and streams. For example,

ByteArrayOutputStream csv = new ByteArrayOutputStream();
PrintStream printer = new PrintStream(csv);
printer.println("a;b;c");
printer.println("1;2;3");
printer.close();
csv.close();

Then in your Servlet you can serve your csv.toByteArray() as a stream. Some example is given here: Implementing a simple file download servlet.

Community
  • 1
  • 1
Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132
-1

You can use OpenCSV library in Google App Engine.

JR Galia
  • 17,229
  • 19
  • 92
  • 144
  • hi thanks for the replay by using OpenCSV can i write the csv file in google app? – user1686253 Dec 11 '12 at 19:56
  • you can save it in BlobStore. – JR Galia Dec 11 '12 at 23:32
  • take a look at https://developers.google.com/appengine/docs/java/blobstore/overview#Writing_Files_to_the_Blobstore – JR Galia Dec 11 '12 at 23:35
  • ya ..thanks for the replay meanas i want to read the user information from database and store the information in the for of csv file .now how can i create the csv file means google app not allow to create own file. – user1686253 Dec 12 '12 at 03:34
  • ya.thanks for the replay .i want to store the user information in csv file but in google app we can't write our own files .now how can i write the csv file – user1686253 Dec 12 '12 at 03:35
  • hi now i am using this code CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t'); but here yourfile.csv means create my own file .but google app doesn't support to write csv files how can solve my problem – user1686253 Dec 12 '12 at 03:37