How to create a folder in Windows by a Java Program? [duplicate] - java

This question already has answers here:
How to create a folder in Java?
(8 answers)
Closed 6 years ago.
I would like to write a Java Programm which should be able to create a Folder in Windows, but the Client should be a able to decide on the Name of the Folder. How can I do that?
Thanks for any suggestions!

A small google search would have given you the answer. If you want to name it a specific way, just grab the name from a JTextField or something like that.

Create a File object and use mkdir() to create a new folder.
File folder;
folder = new File("path/to/new/folder");
folder.mkdir();
Obviously you will need to check if the folder already exists and you will need to use an inputted string for the name of the folder.

Related

Cannot create java file in Intellij IDEA [duplicate]

This question already has answers here:
Problem with IntelliJ -> Cannot create class with name "Main"
(2 answers)
Closed 2 years ago.
I cannot create java class file with name TokenProperties.java. I also tried restart and clean cache but doesn't help. Class with different name I can crate.
Here is error pop-up.
File will added but marked as plain text file, and no java file.
Do you have idea whats is going on? Thank you.
It kind of looks like you create a TXT file with the name TokenProperties.java when you want to create a JAVA file with the name TokenProperties similar to how DemoAAplication was created.
Right click com.example.demoa package.
Select New
Select Java Class
I wasn't able to fix it so I reinstalled Intellij and now it works.

How to read from resource file outside jar [duplicate]

This question already has answers here:
How to read text file from classpath in Java?
(17 answers)
Closed 3 years ago.
I want to read the configuration file outside the jar, how should I do?
I tried classloader.getResourceAsStream("config.xml") and succeeded while I am running the code inside Intellij. Now I want to build the jars to a folder and place the config.xml under the same folder, not inside the jar, but the program fails to detect the config.xml.
Is there a graceful way of reading the config.xml instead of using File with relative path in the code, which doesn't work while debugging/running inside the IDE?
Yes, turn it into a system property, and provide it to anything running any Java process/application.
Let's say you have a config.xml file located inside /some/path/down/the/line/, then you can do: java --classpath ... -Dapp.config=/some/path/down/the/line/config.xml tld.domain.Application.
Then all you have to do in your Java code is to reference that name/path: final String configFile = System.getProperty("app.config");, and use any well-known routine to read it from there.
Basically, you have to make sure the file/path/location is provided somehow to the Java classpath.

How to get directory of a .java file [duplicate]

This question already has answers here:
How to get the current working directory in Java?
(26 answers)
Closed 3 years ago.
Part of my (java) code needs to access a database. When opening a connection it checks if the actual database file exists (I use sqlite). I want to make my code portable, so I want to avoid hard coding the path of the database file. Is there anyway in java to get the path of the .java file? Because I know exactly where the database is from the .java file accessing it.
I've tried using current directory with File but it doesn't give me the path of the actual .java file. When I use android studio the current directory is different than when I simply use a terminal.
The best way to get the path information would be using Paths and Path class from the java.nio. You can input an absolute path or relative path to the Paths.get(String str) to get the Path.
To get the project directory, you can use:
Paths.get(System.getProperty(“user.dir”))
Paths.get(“”)
It will get the complete absolute path from where your application was initialized.
The Paths.get() method will return a Path Object, on which you can call toString() or toUri() to get the path. Hope this helps.
Javadocs - Paths

How to run .exe file within program [duplicate]

This question already has answers here:
Run exe which is packaged inside jar file
(6 answers)
Closed 5 years ago.
In eclipse, if you have a .exe file in the package how would you run it? I know if it is outside you would just add in the path, but what would the path be for something inside the project?
Lets imagine a File f = new File(Path);
If in that case we have the file inside our project (same directory),then we dont need to add the path, just the file name and extension (ex: .txt)......I guess in your case it would be something similar.

eclipse workspace .location file format [duplicate]

This question already has answers here:
Programmatically list open projects in an eclipse workspace from outside of eclipse
(2 answers)
Closed 6 years ago.
I need a way to get the directory path of an eclipse project by deserializining the .location file found in the eclipe workspace at:
.metadata\.plugins\org.eclipse.core.resources.projects\myproject\.location
Its contents look something like:
#±‹#¼ %–磓¾ URI//file:/D:/proj/myproject ÀXûó#¼ QóŒ{»wÆ
so I would like to programmatically get the "D:/proj/myproject" string out of it.
Bonus points if the process doesn't use the Eclipse API.
Eclipse read this file in the method LocalMetaArea.readPrivateDescription(..).
It uses a SafeChunkyInputStream and DataInputStream for reading it.
It contains UTF8 strings and some integers.
For the complete code see
org.eclipse.core.internal.resources.LocalMetaArea at GrepCode.com

Categories

Resources