I could not find a good way to check if the file exists before downloading it.
It seems the API doesnt provide a way to check if the file exists by getting the file by ID.
Basically Im checking if the generated outPutStream size is > 0 to process the file, but I didnt like the solution.
Drive driveService = new Drive.Builder(buildHttpTransport(), JSON_FACTORY, googleCredential).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
driveService.files().get(this.fileId).executeMediaAndDownloadTo(outputStream);
if(outputStream.size() == 0)
processFile()
Ideas are welcome!
Well, if you just want to check if the file is existed or not, then you can use the Files: list to list the file on your Google Drive. You can get the id of the file in the results.
If you already know the file id, then you can verify it by using the Files: get.
GET https://www.googleapis.com/drive/v3/files/fileId
You will get a response 200 here if the File id is existed.
For the Java code that you want, check this related SO question if it can help you.
Related
I decided to post a new question on this since none of the existing posts lead to me a solution. Mine is a Spring Boot application and here is the service:
public String fetchPrediction(MultipartFile file) throws IOException, InterruptedException {
File convFile = new File( System.getProperty("user.dir")+"/"+file.getOriginalFilename());
convFile.setWritable(true);
file.transferTo(convFile);
INDArray array = new CustomerLossPrediction().generateOutput(convFile);
Files.delete(Paths.get(convFile.getPath()));
return array.toString();
}
File deletion isn't happening and it gets stored at user home directory:
Found that the file is being used by the Java process. How can I delete this file once execution is completed? Is there a better approach here rather than writing to a file? Some of you would bring up writing to OutputStream here, but note that I need to work with MultipartFile in order to have file upload functionality.
I don't know if that's possible but I think you can rename the file to a randomly generated string then afterwards lock, read, unlock then delete the renamed file. In theory, another program could guess the filename and read the file just after it's unlocked but before it is deleted. But in practice, you'll probably be fine.
I am new to S3 and I am trying to create multiple directories in Amazon S3 using java by only making one call to S3.
I could only come up with this :-
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket,
"test/tryAgain/", emptyContent, metadata);
s3.putObject(putObjectRequest);
But the problem with this while uploading 10 folders (when the key ends with "/" in the console we can see the object as a folder ) is that I have to make 10 calls to S3.
But I want to do a create all the folders at once like we do a batch delete using DeleteObjectsRequest.
Can anyone please suggest me or help me how to solve my problem ?
Can you be a bit more specific as to what you're trying to do (or avoid doing)?
If you're primarily concerned with the cost per PUT, I don't think there is a way to batch 'upload' a directory with each file being a separate key and avoid that cost. Each PUT (even in a batch process) will cost you the price per PUT.
If you're simply trying to find a way to efficiently and recursively upload a folder, check out the uploadDirectory() method of TransferManager.
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/TransferManager.html#uploadDirectory-java.lang.String-java.lang.String-java.io.File-boolean-
public MultipleFileUpload uploadDirectory(String bucketName,
String virtualDirectoryKeyPrefix,
File directory,
boolean includeSubdirectories)
Can anyone help me how to change a line in a file using java IO function. The file is located at SDCard. I have tried some solution from different blog, but failed.
I need to change one attribute wpa_oper_channel=1 to 2,3,4..... as user demand in the file sdcard/sample.txt.
I have also tried using SED command, but still not working. Please suggest if there any solution using java IO function.
The Command I have used using SED :
sed -i 's/wpa_oper_channel=[0-9]\\+/wpa_oper_channel=7/' sample.txt
If your configuration file is in the form of a Properties file. This means each line is in a format of key=value. You can easily read, modify and write it. Here is an example, I assume you have no problem with reading and writing a file through streams.
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"sample.txt");
InputStream is = new FileInputStream(file); // Read your existing file
Properties props = new Properties();
props.load(is);
props.setProperty("wpa_oper_channel", "4");
OutputStream out = new FileOutputStream(file); // Your output file stream
prop.store(output, null); // Save your properties file without header
By doing this, you may lose if there is another type of line like comments or else.
Update
I updated to source code. But still you need to get read and write permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I haven't test the code, It will work with some exception handling. If it is not work please specify your error.
You just have to
read the file's content into memory (there are millions of examples on how to do that with java),
change the content (finding the position could be done using e.g. indexOf("wpa_oper_channel="))
write the content back to the file (there are millions of examples for that too)
What android do you use?
From android 4.4+ user haven't access to edit and remove files from SD Card. You must copy them to external storage and use there. See this link - http://code.google.com/p/android/issues/detail?id=67570
I want to save a Unique ID (which is a String) which gets created when I launch my Java application. Now I want to save this somewhere (I think in some file on the disk) so that when I relaunch my application I should be able to read it and use that ID.
I want to know what is the good way to saving such ID. I am thinking of creating a Properties file and save it then retrieve it from it when I relaunch application. Is there a better or standard way for this?
EDIT :
Additionally what should be the folder location for storing on the disk. Should it be relative to my execution path or some Logged-in user specific path?
1. If its the same Java application that writes or reads this String, then use Serialization, it will be in non-readable form when saved.
2. If reading and writing is from different program, then use Text file.
3. Using Property file will be also a good approach.
If your app/program needs to store more data at some point sqlite3 might be the best option for you. It is easy to implement and use.
Download sqlite3
EDIT: How many IDs will be stored in the app? If there are just a few, a textfile or property file is enough.
EDIT2: Navigate to your Documents folder on your machine and you will see folders of programs/games. Thats where you should place the file/db. However you can also store it in the installation path on your hard drive. Also make sure your user launches the app trough a shortcut, not the actual execution file
Use the FileWriter and File classes from Java.
It should be something like that:
File f = new File(your path here);
if (f.exists()){
BufferedReader br = new BufferedReader(new FileReader(your path here));
String a = br.readLine();
br.close();
}else{
FileWriter fw = new FileWriter(your path here);
fw.write(your ID String);
fw.flush();
fw.close();
I hope this is want u meant.
Best regards
edit: just noticed too late that your edited your post....
I am working on android app.
What I need is directly path to traineddata file (to init tesseract).
Look like best option is to set the resource in raw.
I am getting resource ID this way (file name is : deu.traineddata):
int rID = resources.getIdentifier("deu", "raw", "my.code.package");
OK, 'rID' > 0, now getting Stream :
InputStream is = resources.openRawResource(rID);
ok, 'is' != null.
But now getting problem ,by reading 'is' IOException has been throw, with no stack trace :
byte[] bytes = new byte[is.available()];
is.read(bytes);
I try also to read file from asset , but is the same problem by reading from InputStream.
What i'am doing wrong, ist there any other way to get the resource path ?
thanx
andrej
If you look at the native code in tesseract-android-tools (under jni), you will see that the library will access a file. I am in the same boat at the moment. After some digging, my plan is to store the traineddata file as a resource along with the project, and write to the private file on load.
The pseudo code is something like this:
on load, check for private file,
if it doesn't exist, load the traineddata from raw dir and write to the private file.
initialize tesseract with the private file.
ref:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
http://developer.android.com/guide/topics/resources/providing-resources.html
Cheers