I'm trying to get the content of a Google Docs with the Google Drive API. I successfully fetched folder and got the Google Docs files id.
On the API documentation, it is said that I can download a file like this :
private String getGdoc(String id) throws Exception {
if(service == null) throw new Exception();
OutputStream outputStream = new ByteArrayOutputStream();
service.files().export(id, "text/plain")
.executeMediaAndDownloadTo(outputStream);
return null;
}
My problem is that I don't want to write it into a file with an OutputStream but I want to get the content of the file into a String.
So I tried to use an InputStream :
private String getGdoc(String id) throws Exception {
if(service == null) throw new Exception();
InputStream inputStream = service.files().export(id, "text/plain")
.executeMediaAsInputStream();
System.out.println(IOUtils.toString(inputStream, StandardCharsets.UTF_8));
return null;
}
With an InputStream I get the content of the file, however I have some Japanese characters in my documents and they are not displayed. I only get ? for these Japanese characters in the console. I don't know where it can come from since I specified to use the UTF-8 charset.
When I try to write with the OutputStream into a txt file, I don't have any problem of characters not recognized.
I don't know what is the best way to achieve that. Can you help me ?
Stringcontains correct data and that the console you print it to can't display the Japanese character (and thus replaces it with?). Try serializing theStringto some other format (like a JSON file or XML, using the appropriate libraries) and see if that's sufficient).