I’m working with the Google Drive API to download files but running into a strange issue. My code runs successfully without throwing any exceptions, however I cannot locate the downloaded file on my system afterwards.
The code completes without any error messages but I can’t figure out where the file actually gets saved. Does anyone know what might be going wrong here?
yeah this caught me too lol. bytearrayoutputstream just keeps everything in ram, doesnt actualy write to disk. you gotta do something like byte[] data = ((ByteArrayOutputStream)fileStream).toByteArray(); then write that byte array to a real file. or just use FileOutputStream from the start and skip the extra step
Your download is actually working perfectly, you just need to save the ByteArrayOutputStream content to disk. After the executeMediaAndDownloadTo call completes, you can write the stream data to a file like this: Files.write(Paths.get("document.pdf"), ((ByteArrayOutputStream) fileStream).toByteArray()); The ByteArrayOutputStream holds all your PDF data in memory but doesn’t automatically create a physical file. I made this mistake early on and was convinced the API was broken until I realized I needed that extra step to persist the data. Alternatively, you could skip the intermediate step entirely and pass a FileOutputStream directly to executeMediaAndDownloadTo if you know the target path beforehand.
The issue is that you’re using ByteArrayOutputStream which only stores the data in memory, not on disk. Your code downloads the file content successfully but it remains in the output stream buffer rather than being written to an actual file. You need to either write the ByteArrayOutputStream content to a file manually using FileOutputStream, or better yet, use FileOutputStream directly as your output stream parameter. Something like FileOutputStream fileStream = new FileOutputStream("path/to/your/file.pdf"); would write the downloaded content directly to the specified location. I had this exact same problem when I first started working with the Drive API and spent hours looking for non-existent files before realizing the data was just sitting in memory.