有没有办法在 google 驱动器中写入和创建文本文件?
Is there a way to write and create a text file in the google drive?
我正在编写一个简单的服务器,它将其所有操作写入日志文件。现在写入文件在当前目录中创建。有没有办法创建一个文件并写入 google 驱动器,这样我就可以在远程位置看到服务器的操作?
我的当前代码
void NewFile(String path, String data){
try{
File file =new File(path);
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}
//true = append file
FileWriter fileWritter = new FileWriter(file.getName());
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(data);
bufferWritter.write("\r" );
bufferWritter.write("\n" );
bufferWritter.close();
}catch(IOException e){
e.printStackTrace();
}
}
是的,你可以。查看 Google Doc 的 API 文档。
The Google Documents List API allows developers to create, retrieve,
update, and delete Google Docs (including but not limited to text
documents, spreadsheets, presentations, and drawings), files, and
collections. It also provides some advanced features like resource
archives, Optical Character Recognition, translation, and revision
history.
https://developers.google.com/google-apps/documents-list/#what_can_this_api_do
您可以在 Quickstart: Run a Drive App in Java 上关注示例
这里我只是粘贴了那个
中的具体代码段
//Create a new authorized API client
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
//Insert a file
File body = new File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain");
java.io.File fileContent = new java.io.File("document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
File file = service.files().insert(body, mediaContent).execute();
我正在编写一个简单的服务器,它将其所有操作写入日志文件。现在写入文件在当前目录中创建。有没有办法创建一个文件并写入 google 驱动器,这样我就可以在远程位置看到服务器的操作?
我的当前代码
void NewFile(String path, String data){
try{
File file =new File(path);
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}
//true = append file
FileWriter fileWritter = new FileWriter(file.getName());
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(data);
bufferWritter.write("\r" );
bufferWritter.write("\n" );
bufferWritter.close();
}catch(IOException e){
e.printStackTrace();
}
}
是的,你可以。查看 Google Doc 的 API 文档。
The Google Documents List API allows developers to create, retrieve, update, and delete Google Docs (including but not limited to text documents, spreadsheets, presentations, and drawings), files, and collections. It also provides some advanced features like resource archives, Optical Character Recognition, translation, and revision history.
https://developers.google.com/google-apps/documents-list/#what_can_this_api_do
您可以在 Quickstart: Run a Drive App in Java 上关注示例 这里我只是粘贴了那个
中的具体代码段 //Create a new authorized API client
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
//Insert a file
File body = new File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain");
java.io.File fileContent = new java.io.File("document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
File file = service.files().insert(body, mediaContent).execute();