简体   繁体   中英

Is there a way to write and create a text file in the google drive?

I have a simple server I'm writing that writes all its actions to a log file. Write now the file is created in the current directory. Is there a way to create a file and write to in the google drive so I can see the actions of the server in remote locations??

My Current code

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();
    }
}

Yes, you can. Check out Google Doc's API documentation.

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

You can follow the sample on Quickstart: Run a Drive App in Java Here I just pasted the specific code segment from that

    //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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM