How to Create the Directory Error

Cannot Create Directory error

You need to have a share name after the @"\\192.168.22.2".

Something like @"\\192.168.22.2\MySharedFolder".

You cannot create a subfolder from that root dir

Failed to create directory error in Node.js

As you are running on windows machine, this directory is invalid.

Please replace this line callbacks: tf.node.tensorBoard('/tmp/fit_logs_1') with this

callbacks: tf.node.tensorBoard('C:\\Users\\BCG04')

Cannot create directory: System.UnauthorizedAccessException Error

You're getting that error because your app isn't running as administrator and is trying to write to Program Files which is for admins only.

Unless you're writing an installer, your application will never need to run as administrator so it can write to Program Files. Program files is not intended for applications to write runtime data to. It's a secure location intended for an administrator to install applications into. This process protects trusted application files from being maliciously changed.

You should not be writing your data to the ProgramFiles directory. Windows has a variety of directories for writing data to, such as %TEMP%, %APPDATA%, and %LOCALAPPDATA%.

Windows Environment Variables

Create a directory if it does not exist and then create the files in that directory as well

This code checks for the existence of the directory first and creates it if not, and creates the file afterwards. Please note that I couldn't verify some of your method calls as I don't have your complete code, so I'm assuming the calls to things like getTimeStamp() and getClassName() will work. You should also do something with the possible IOException that can be thrown when using any of the java.io.* classes - either your function that writes the files should throw this exception (and it be handled elsewhere), or you should do it in the method directly. Also, I assumed that id is of type String - I don't know as your code doesn't explicitly define it. If it is something else like an int, you should probably cast it to a String before using it in the fileName as I have done here.

Also, I replaced your append calls with concat or + as I saw appropriate.

public void writeFile(String value){
String PATH = "/remote/dir/server/";
String directoryName = PATH.concat(this.getClassName());
String fileName = id + getTimeStamp() + ".txt";

File directory = new File(directoryName);
if (! directory.exists()){
directory.mkdir();
// If you require it to make the entire directory path including parents,
// use directory.mkdirs(); here instead.
}

File file = new File(directoryName + "/" + fileName);
try{
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(value);
bw.close();
}
catch (IOException e){
e.printStackTrace();
System.exit(-1);
}
}

You should probably not use bare path names like this if you want to run the code on Microsoft Windows - I'm not sure what it will do with the / in the filenames. For full portability, you should probably use something like File.separator to construct your paths.

Edit: According to a comment by JosefScript below, it's not necessary to test for directory existence. The directory.mkdir() call will return true if it created a directory, and false if it didn't, including the case when the directory already existed.



Related Topics



Leave a reply



Submit