Last active
December 24, 2015 01:53
-
-
Save v0o0v/0fbf8bf439ad5f4daca3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.Writer; | |
import java.util.List; | |
import com.google.api.client.http.FileContent; | |
import com.google.api.client.http.GenericUrl; | |
import com.google.api.client.http.HttpResponse; | |
import com.google.api.services.drive.Drive; | |
import com.google.api.services.drive.model.File; | |
public class GoogleUtil { | |
public static void MakeListToFileAsUTF8(List<String> strList, String filename) throws Exception { | |
Writer writer = new BufferedWriter( | |
new OutputStreamWriter(new FileOutputStream(new java.io.File(filename), false), "UTF-8")); | |
for (String temp : strList) { | |
writer.write(temp); | |
} | |
writer.close(); | |
} | |
public static void MakeStreamToFileAsUTF8(InputStream is, String filename) throws Exception { | |
InputStreamReader ir = new InputStreamReader(is, "UTF-8"); | |
BufferedReader reader = new BufferedReader(ir); | |
Writer writer = new BufferedWriter( | |
new OutputStreamWriter(new FileOutputStream(new java.io.File(filename), false), "UTF-8")); | |
while (true) { | |
String temp = reader.readLine(); | |
if (temp == null) | |
break; | |
writer.write(temp); | |
} | |
reader.close(); | |
writer.close(); | |
} | |
public static void updateFile(Drive service, File file, String fileName) throws Exception { | |
service.files().delete(file.getId()); | |
service.files().update(file.getId(), file, new FileContent("text/plain", new java.io.File(fileName))).execute(); | |
} | |
public static void printStream(InputStream is) throws IOException { | |
InputStreamReader isr = new InputStreamReader(is); | |
BufferedReader br = new BufferedReader(isr); | |
while (true) { | |
String temp = br.readLine(); | |
if (temp == null) | |
break; | |
System.out.println(temp); | |
} | |
} | |
public static InputStream downloadFile(Drive service, File file) { | |
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) { | |
try { | |
HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())) | |
.execute(); | |
return resp.getContent(); | |
} catch (IOException e) { | |
// An error occurred. | |
e.printStackTrace(); | |
return null; | |
} | |
} else { | |
// The file doesn't have any content stored on Drive. | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment