Writing a file to a specific directory - java

I've created a simple program that would write a file with a directory using the following codes:
String nameProve = nameField.getText();
String employee = ("C:\\Users\\ALLEN\\workspace32bit\\RETRIEVE_CHECKER1\\RETRIEVE_CHECKED1" + nameProve + ".txt");
PrintWriter outputStream1 = null;
try
{
outputStream1 = new PrintWriter(employeeName);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Can not write to " + employeeName);
System.exit(0);
}
outputStream1.println("Employee Retrieve Executed");
outputStream1.close();
I've already exported my code to a .jar file and my code works just fine if i execute it in the computer were I've develop my program but when I copied the jar file to other computer and I also created the forlder (manually) that corresponds with the directory in my codes my program doesn't work and my catch block will show the message Can not write to "C:\Users\ALLEN\workspace32bit\RETRIEVE_CHECKER1\RETRIEVE_CHECKED1" + nameProve + ".txt"
Can anybody give me some advice on how to solve this error? Thanks!

Obviously you are the user ALLEN on your machine and run the program as that user, so everithing works fine, because you are the owner of the directory C:\Users\ALLEN.
Then you copy the jar to another machine, where the user ALLEN does not exist and you are logged in as antoher user let's say his name is Bob and create that directory unter C:\Users. You may have noticed that you when you wanted to create that directory as the user Bob windows has warned you that you need admin priviledges in order to compleate this action.
Now you try to run your program with the user Bob who has access only to his own directory C:\Users\Bob and try to write to ALLEN's own directory. So what happens is that you get an IOException telling you access denied which is good so!
You should not attepmt to write to other users private direcotries, this is a security issue.
In your code when dealing with filesystem, never hard code absolute path, always use relative paths, or if you need to defined a direcotry where all the data needed yb you program should be located, then pass ist as argument.
The simplest to do, ist use the following and work with the current working directory
String employee = "RETRIEVE_CHECKER1\\RETRIEVE_CHECKED1" + nameProve + ".txt";
You need to create the directory RETRIEVE_CHECKER1 either by hand in location where you run the program, or better yet in your program using File#mkdir and use it like this:
File employee = new File(dir, "RETRIEVE_CHECKED1" + nameProve + ".txt");
PrintWriter outputStream1 = new PrintWriter(employeeName);

Exception you catch will have all details- like access denied, directory not exist, etc. Currently you loose exception data (message, stacktrace) when catching it.
Please do something like
Do not catch just java.lang.Exception. This is too wide- will catch possible NullPointerException and present misleading message '"Can not write to'. Catch only specific exception instead- ones that are actually thrown from try block. In your case, that will be java.io.FileNotFoundException
Do not loose exception details. Print it to the standard error log, or even better use loging framework
catch block that adresses those issues:
catch(FileNotFoundException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
}

Related

File creating under root directory, strange response

This is not actually a question, but I need explanation. I was trying to create directory under C:// drive. So;
try {
File f_paz = new File("c://Ekap_Pazarlik_xml" + file_currentDate + "//");
File parent_z = f_paz.getParentFile();
if (null != parent_z) {
parent_z.mkdirs();
}
if (!f_paz.exists()) {
f_paz.createNewFile();
}
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
I tried that and it gave me "Access Denied" error, so I tried all the solutions in the web. Nothing happened.
Strangely, I decided to put "space" after there;
File f_paz = new File("c://Ekap_Pazarlik_xml" + file_currentDate + "// ");
here
just after "// ", and it worked. I was able to crate directory like that. I have no idea why it worked like that. Please provide me some explanation. Thanks !
f_paz.createNewFile(); will try to create a new file, not a directory. Many Windows systems won't allow you to create files in directly in C: so that's why you get the exception.
Since you want to create a directory and not a file use f_paz.mkdir(); instead. You don't need the "//" at the end, btw.
What happens if you add the space, i.e. use path "C:/Ekap_xxx/ "? The system will see a path with a blank filename " " and a parent directory name "Ekap_xxx". Now parent_z will refer to "C:/Ekap_xxx" and thus parent_z.mkdirs(); will create directory "Ekap_xxx". Now f_paz.exists() will return true since the filesystem seems to decide a file with an invalid name still exists (I'd have to look for some information on why that is so, it's hidden in the native code).

How to check whether file is open or not in java [duplicate]

I need to write a custom batch File renamer. I've got the bulk of it done except I can't figure out how to check if a file is already open. I'm just using the java.io.File package and there is a canWrite() method but that doesn't seem to test if the file is in use by another program. Any ideas on how I can make this work?
Using the Apache Commons IO library...
boolean isFileUnlocked = false;
try {
org.apache.commons.io.FileUtils.touch(yourFile);
isFileUnlocked = true;
} catch (IOException e) {
isFileUnlocked = false;
}
if(isFileUnlocked){
// Do stuff you need to do with a file that is NOT locked.
} else {
// Do stuff you need to do with a file that IS locked
}
(The Q&A is about how to deal with Windows "open file" locks ... not how implement this kind of locking portably.)
This whole issue is fraught with portability issues and race conditions:
You could try to use FileLock, but it is not necessarily supported for your OS and/or filesystem.
It appears that on Windows you may be unable to use FileLock if another application has opened the file in a particular way.
Even if you did manage to use FileLock or something else, you've still got the problem that something may come in and open the file between you testing the file and doing the rename.
A simpler though non-portable solution is to just try the rename (or whatever it is you are trying to do) and diagnose the return value and / or any Java exceptions that arise due to opened files.
Notes:
If you use the Files API instead of the File API you will get more information in the event of a failure.
On systems (e.g. Linux) where you are allowed to rename a locked or open file, you won't get any failure result or exceptions. The operation will just succeed. However, on such systems you generally don't need to worry if a file is already open, since the OS doesn't lock files on open.
// TO CHECK WHETHER A FILE IS OPENED
// OR NOT (not for .txt files)
// the file we want to check
String fileName = "C:\\Text.xlsx";
File file = new File(fileName);
// try to rename the file with the same name
File sameFileName = new File(fileName);
if(file.renameTo(sameFileName)){
// if the file is renamed
System.out.println("file is closed");
}else{
// if the file didnt accept the renaming operation
System.out.println("file is opened");
}
On Windows I found the answer https://stackoverflow.com/a/13706972/3014879 using
fileIsLocked = !file.renameTo(file)
most useful, as it avoids false positives when processing write protected (or readonly) files.
org.apache.commons.io.FileUtils.touch(yourFile) doesn't check if your file is open or not. Instead, it changes the timestamp of the file to the current time.
I used IOException and it works just fine:
try
{
String filePath = "C:\sheet.xlsx";
FileWriter fw = new FileWriter(filePath );
}
catch (IOException e)
{
System.out.println("File is open");
}
I don't think you'll ever get a definitive solution for this, the operating system isn't necessarily going to tell you if the file is open or not.
You might get some mileage out of java.nio.channels.FileLock, although the javadoc is loaded with caveats.
Hi I really hope this helps.
I tried all the options before and none really work on Windows. The only think that helped me accomplish this was trying to move the file. Event to the same place under an ATOMIC_MOVE. If the file is being written by another program or Java thread, this definitely will produce an Exception.
try{
Files.move(Paths.get(currentFile.getPath()),
Paths.get(currentFile.getPath()), StandardCopyOption.ATOMIC_MOVE);
// DO YOUR STUFF HERE SINCE IT IS NOT BEING WRITTEN BY ANOTHER PROGRAM
} catch (Exception e){
// DO NOT WRITE THEN SINCE THE FILE IS BEING WRITTEN BY ANOTHER PROGRAM
}
If file is in use FileOutputStream fileOutputStream = new FileOutputStream(file); returns java.io.FileNotFoundException with 'The process cannot access the file because it is being used by another process' in the exception message.

Issue with checking permission of a folder in java

I am creating simple function to check weather I have write/delete permission to a folder
I want to know whether the operating system will allow writing into the folder or not ?
public static String PermissionCheck(String FilePath) {
//File f = new File(FilePath);
String actions = "read,write";
try
{
AccessController.checkPermission(new FilePermission(FilePath, actions));
return ("You have read/write permition to use : " + FilePath);
}
catch (SecurityException e)
{
return ("You don't have read/write permition to use : " + FilePath);
}
}
When I call either with correct path or incorrect path the method it always returns message from cathc.
I know its duplicate question I have already gone through many links but no luck !!
Similar Question
Similar Question 1
Example :
PreChecks.PermissionCheck("C:/TEST/G2");
PreChecks.PermissionCheck("C:/Program Files/SAP");
Both calls return the message from catch block where I have all permission on "C:/TEST/G2" and no write permission on "C:/Program Files/SAP".
I have also tried canWrite but it says I have write permissions to "C:/Program Files/SAP" but I know I don't have those.
The class java.io.File is limited in its capabilities. E.g.
File f=new File("C:\\Program Files\\Java");
System.out.println(f.canWrite());
prints true on my machine though a user process is not allowed to write at this location.
In contrast,
Path p=Paths.get("C:\\Program Files\\Java");
System.out.println(Files.isWritable(p));
correctly prints false.
So the solution is to use the NIO API.
The method AccessController.checkPermission has an entirely different purpose. It helps implementing security managers. It throws a SecurityException because you don’t have an explicitly granted permission to access that directory, but as long as you don’t have a SecurityManager installed, that is irrelevant.

Java: store configuration variables in a file

I'm developing a program with NetBeans 8.0 and JavaFX Scene Builder 2.0 that need store some variables in a file, where admin users can modify it when needed, (like change server IP address, or a number value from a no editable textfield) and if they close and load again the program, the changes made in variables are kept. Like any settings section of a program.
I just try do it with the Properties file, but i have problems to store it in the same folder as .jar file. When the program execute the line new FileOutputStream("configuration.properties"); the file is created at root of the disk. As the folder of the file can be stored anywhere, i not know how indicate the right path.
Creating the properties file in the package of the main project and using getClass().getResourceAsStream("configuration.properties"); i can read it but then i can not write in for change values of variables.
Is there a better method to create a configuration file? Or properties file is the best option for this case?
My other question is whether it is possible to prevent access to the contents of the file or encrypt the content?
PD: I've been testing this part of the code in Linux operating system currently, but the program will be used in Windows 7 when ready.
If you use Maven, you can store your property files in your resources folder, say resources/properties/. When you need to load them, do this:
private Properties createProps(String name)
{
Properties prop = new Properties();
InputStream in = null;
try
{
in = getClass().getResourceAsStream(name);
prop.load(in);
}
catch (IOException ex)
{
System.err.println("failed to load \"" + name + "\": " + ex);
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
System.err.println("failed to close InputStream for \"" + name + "\":\n" + FXUtils.extractStackTrace(ex));
}
}
return prop;
}
Where name is the full path to your properties file within your resources folder. For example, if you store props.properties in resources/properties/, then you would pass in properties/props.properties.
I am not 100% sure if you can carry over this exact procedure to a non-Maven project. You'd need to instruct whatever compiler tool you are using to also include your property files.
As far as your final question goes, in regards to encrypting your properties, I would consider posting that as a separate question (after having done thorough research to try to discover an existing solution that works for you).
At last i found how obtain the absolute path from folder where is .jar file to create properties file in, and read/write it. Here is the code:
File file = new File(System.getProperty("java.class.path"));
File filePath = file.getAbsoluteFile().getParentFile();
String strPath = filePath.toString();
File testFile = new File(strPath+"/configuration.properties");
Tested in Ubuntu 13.04 And Windows 7 and it works.
For encrypt the properties values i found this thread that answer how do it.

Path Doesn't Exist

I decided to test the file creating possibilities Java has to offer:
I tried to make a program that creates a file on a systems desktop.
To get the location, I did the following:
String targetLoc = System.getProperties("user.home") + "/Desktop"; //Returns /Users/targetUser/Desktop
And to then create the file:
File file = new File(targetLoc + "/testfile.txt"); //I'm aware of the slash before the name :)
try{
file.createNewFile();
}catch(Exception exception){
exception.printStackTrace();
}
And even though I don't see any errors above, I get the InvalidPathException.
Why would I get that error?
StackTrace On Request:
java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at com.Code0.FileCreater.Main.MainFF.main(MainFF.java:41)
Answer:
It was a simple screw-up where I assigned the wrong value to the home String variable.
It was a simple screw-up where I assigned the wrong value to the home String variable.
Thank all of you very much for your support.
Regards,
Code0

Categories

Resources