To create a txt filte in the appData folder
Modify the Name
and filePath
of sample code snippet in the documentation from json
to txt
and the mimeType
from application/json
to text/plain
:
File fileMetadata = new File();
fileMetadata.setName("config.txt");
fileMetadata.setParents(Collections.singletonList("appDataFolder"));
java.io.File filePath = new java.io.File("files/config.txt");
FileContent mediaContent = new FileContent("text/plain", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute();
System.out.println("File ID: " + file.getId());
To manipulate the content of the file
See here for official information on how to upload the content.
Basically, for a single request you need to
- obtain the resumable URI
- perfom
PUT
request to it containing the file content in the request body
- Specify the content length (
mediaContent.setLength()
)
I do not have a Java snippet for this part, but I have done it before in Apps Script - I hope this is helpful to understand how you can manipulate the content of the file you crated in the appData fodler.
See also here for a detailed sample on how to upload a file with content in Java.