unable to create a file in new directory in java - java-io

I am trying to create a file in a new directory for which I had written a code in Java such that firstly a directory is made after that a file is created in that directory but while executing the code I found that the directory is created but the file is not and it is giving the error that The system cannot find the path specified.
java.io.FileNotFoundException: C:\Users\Ankit\workspace\SP_CentralSubPub\src\Publishers\0\qw.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.example.doing.mains.receiveFile(mains.java:65)
at com.example.doing.mains.PublisherIdlFile(mains.java:41)
at com.example.doing.mains.main(mains.java:21)

Basically, what's happening is, you might be creating a directory called "DirectoryName\filename.txt", then trying to create a new file called the same thing, this obviously isn't going to work.
So, instead of...
File file = new File("DirectoryName\\filename.txt");
file.mkdir();
file.createNewFile();
Try. .
File file = new File("DirectoryName\\filename.txt");
file.getParentFile().mkdir();
file.createNewFile();
hope it helps.

Related

google-drive-ruby: upload file to a folder duplicates the file in the folder and root path

I have writen a Ruby script to lookup for documents with a given date and upload them to Google Drive by using the google-drive-ruby gem. I have a folder inside of the gdrive root path where I want to place the files, and I access it using collection_by_title and then uploading the file by using the .add method.
The problem is that the files are being uploaded each one two times, one to the folder I want and another one to the root path of my GDrive. Any thoughts?
This is the method where the file gets uploaded:
def upload_document(file, folder_code)
folder = #session.collection_by_title("#{folder_code}")
path = "#{#basedir}/#{folder_code}/#{file}"
folder.add(#session.upload_from_file(path, file, convert: false))
end
EDIT: Methods and variables translated to english.
Each time the method upload_document is triggered, one copy of the file gets uploaded to the folder and another copy gets uploaded to the root path of gdrive.
Example: Method upload_document gets called providing the file (454327.pdf) and the code of the folder where it has to be uploaded in gdrive ("1"). I build the folder object by using collection_by_title, I build the path where the file is located in my local network, and finally the file gets uploaded using upload_from_file. At this point, two copies of the file had been uploaded, one to the root path of gdrive (which I don't want) and another one to the right folder in gdrive.
I received an answer from the gem creator explaining what is happening and my script is finally working as I expected.
https://github.com/gimite/google-drive-ruby/issues/260
The thing is that the file is firstly uploaded to the root by default and then .add just moves the file to the selected collection, so the file needs to be removed from root after the move operation is completed.
#session.root_collection.remove(file)

How to move jar file from one folder to another in Ruby?

In my Ruby script I have to copy one jar file from lib to a different location. Below is the piece of code where I am trying to do so:
FileUtils.mv("#{$HOME_PATH}/lib/myjar1.jar", "#{$HOME_PATH}")
But it is not able to move the jar file to destination. I am seeing the following error:
No such file or directory - file:/home/path/lib/myjar1.jar or file:/home/path/myjar1.jar
I double checked that myjar1.jar is present in source directory. Not sure why I am seeing that error.
I also tried using FileUtils.cp_r("#{$HOME_PATH}/lib/myjar1.jar", "#{$HOME_PATH}") and I am seeing the following error: unknown file type: file:/home/path/lib/myjar1.jar.
Any guidance in copying or moving a jar file is appreciated.
Im assuming you are starting from your home directory (hence the $HOME_PATH). I prefer to use the given ENV hash in IRB.
In IRB this works in MAC OSX:
>> FileUtils.mv("#{ENV['HOME']}/Desktop/my.jar", "#{ENV['HOME']}")
0
The FileUtils#mv method returns 0 as that is the return code of the Unix version of mv which just means it was successful.

YarnRuntimeException file doesnt exist

I'm trying to run a Pig script, getting the error org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.yarn.exception.YarnRuntimeException): java.io.FileNotFoundException: File /tmp/hadoop-yarn.staging/history/done/2015/01/08/000000 does not exits !!!
what does that mean ? shoudl I create it ? where should I put it ?
I ran into the same problem today and found out YarnRuntimeException is included in the hadoop-yarn-api-2.6.0.jar file. Make sure it's in your class path and you should be good.

Specifying index path with Hibernate Search and Spring

I have problem setting the correct path for my index. It would be great if it was inside my spring application, since it would work even after I deploy my application to Cloudbees I guess.
This is my obejct that I trying to index:
#Entity
#Table(name="educations")
#Indexed(index="educations")
public class Education {
I have the following in servlet-context.xml:
<resources mapping="/resources/**" location="/resources/"/>
I specify the lucene index path like this:
Properties props = new Properties();
props.put("hibernate.search.default.indexBase", "resources/lucene/indexes");
entityManagerFactory.setJpaProperties(props);
Which doesnt give me any error but I cant find the folder either, which I dont understand. I tried searching for it.
I also tried:
props.put("hibernate.search.default.indexBase", "classpath:/lucene/indexes");
and
props.put("hibernate.search.default.indexBase", "/resources/lucene/indexes");
But still cant find the folder. However after a while of struggling with this I try to put it in my home directory. (which might give me problem later when deploying to the cloud):
props.put("hibernate.search.default.indexBase", "/lucene/indexes");
I getting the following
Cannot write into index directory: /lucene/indexes for index educations
So I assume its a permission error. I try the following in terminal (OSX):
sudo chmod -R u+rwX /lucene/indexes/
and
sudo chmod -R 755 /lucene/indexes/
But still the same error. Can someone spread some light on this?
Thank you!
Edit:
After some more investigation I am sure it is a problem of permissions. If I specify the full path to my root of the Spring application, it works. I still don't know how to specify this without giving it the full path.
Relative paths are relative to the directory the Java process is launched from. If you have some startup script or similar look in the directory of this script. Absolute paths work fine, but of course you need permissions to write to it.
If you want a more generic solution for your case, you could for example set the right directory as a system property when starting the application and read it from there when creating your Properties. Or you try in another way to determine the full path of your app at runtime.
I couldn't find where the folders were located either so i came up with the following solution:
First i get the location of the working directory by calling System.getProperty("user.dir"). This is OS independent so it works both on linux and windows. The working directory is the directory from which your application is loaded. Next i simply append the relative path that i want as a location for my lucene indexes to the working directory folderpath. Then i use that as the value for hibernate.search.default.indexBase. Now i'll always know where to look for the lucene indexes.
Heres the code:
String luceneFilePath = System.getProperty("user.dir") + "/resources/lucene/indexes";
Properties props = new Properties();
props.put("hibernate.search.default.indexBase", luceneFilePath);
entityManagerFactory.setJpaProperties(props);

Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lex failed with exit code 1

I am trying to include a file in my resource bundle the files contains the following:
LeadPunc="({[`'
TrailPunc=}:;-]!?`,.)"'
NumLeadPunc=#({[#$
NumTrailPunc=}):;].,%
Operators=*+-/.:,()[]
Digits=0123456789
Alphas=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
*extra line*
The files is called:
eng.cube.lm
The error I get is:
IExpenseReporter/tessdata/eng.cube.lm:6: premature EOF
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lex failed with exit code 1
This file goes along with the newest version of tesseract (OCR). Does anyone have an idea what is causing this error and how to fix it?
Make sure you select the "Create folder references" option when adding the tessdata folder to your project.
From the documentation:
NOTE: This library currently requires the tessdata folder to be linked
as a referenced folder instead of a symbolic group. If Tesseract can't
find a language file in your own project, it's probably because you
created the tessdata folder as a symbolic group instead of a
referenced folder. It should look like this if you did it correctly:
Note how the tessdata folder has a blue icon, indicating it was
imported as a referenced folder instead of a symbolic group.
Trashing the current folder and adding it again as a folder reference should solve the problem.
XCode "thinks" this is a lex file and try to process it by calling lex. However, lex finds and unbalanced quote and thus a premature end of file.
You should try to call the designated tool explicitly.
I had this issue and I found that copying the folder "tessdata" from the language zip into the project directory rather then into Xcode fixed the issue.

Resources