File library that help creating paths - java

I'm looking for a file utilities library that help creating paths, i.e. if I want to create a file in "a/b/c/x/y/z", it will check if each directory exists and creates it if not.

How about reading the File javadoc
Specifically, given File foo which represents a text file you want to create, you can execute foo.getParentFile().mkdirs().

File.mkdirs()

Related

How to get all directories inside given directory that contains file with specific name

I have program where input parameter is name of directory.
I need to get all subdirectories of this directory, which contains specially named file (if file not present, skip), other files and directories must be ignored. Is there simple way to achieve this?
I found DirectoryFileFilter.DIRECTORY used while file.listFiles method is called, but it is not what i need. Thanks for help.
I have answered similar question here:
https://stackoverflow.com/a/20680556/3115098
You have to implement FileFilter to match specific requirements for your folder.
Hope it helps.

FileWriter - Write to .txt within project but outside current directory

So I am generating data and appending it to a text file.
Instead of using FileWriter fstream = new FileWriter("addressList.txt"); I need the file path to be in another package where I have created the addressList.txt file. It says System cannot find path error.
depending on how you are running this the path may be readable but not be writable. Resources in JAR files are generally static and should be treated as readonly.
If you are trying to create a list of per user settings (addresses stored in a list) you should consider using the System objects getenv(string) method to locate the users temporary storage and copy the default file there. After copying the default file you will be free to update the file exactly as normal.
Reference:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#getenv(java.lang.String)

Extracting a file from the currently running JAR through code

Are there any built-in methods I can use to allow users to extract a file from the currently running JAR and save it on their disk?
Thanks in advance.
File file = new File("newname.ext");
if (!file.exists()) {
InputStream link = (getClass().getResourceAsStream("/path/resources/filename.ext"));
Files.copy(link, file.getAbsoluteFile().toPath());
}
Use getResourceAsStream (docs), you can do whatever you want with it after that.
For a two-liner you could use one of the Commons IO copy methods.
I am not sure whether you will get know from which jar your class is getting executed but you can try this to extract resources from jar :
How to write a Java program which can extract a JAR file and store its data in specified directory (location)?

Calling a path in java method

I have a small problem calling a path(that has the python file, that I need to run) in the following code:
Process p = Runtime.getRuntime().exec(callAndArgs,env,
new java.io.File("C:\\Users\\Balkishore\\Documents\\NetBeansProjects\\Testinstrument_Rest\\build\\web"));//excuting python file
As it can be seen from the above code, the python file is called using the path specified in java.io.file function. But it is very specific, as it can be run only in my computer. How can i make it generic, so that it is possible to run this piece of code in any computer?
Any help would be very much appreciated.
Put your python script to the location relative to your working directory and use relative path. Alternatively use configuration file or property to read the path from.
If this file is already exist in the app then you need to do
ServletContext.getRealPath("/");
which will give you the path to web root now from here you need to move relatively to reach to your file
If this is an external file
put it in ${user.home}/appname/
String filePath = System.getProperty("user.home")+File.separator+"APP_NAME"
and instruct your users to put the file in this path, or read the path from some configuration file (.properties, .conf)

proper file path of a text file to read

I want to manipulate a file in my java program.The file to read must be paralled to my src folder.
What should I give as file path?
An elaborated example might help. From your question, what I get is,
Source Path : /home/user/project1/src/
File Path : /home/user/project1/src/
If this is the case, then once you build the project, the file path is not going to remain the same. So if you say that relative path for the file to open remains the same in built code, then you can use Class.getResourceAsStream(String path) which returns you the InputStream for given file. You can then construct the File object using it.
Refer this for details.
You should have a File object representing your src folder, and then create a new File object using that:
File textFile = new File(srcFolder, relativePath);
How you determine srcFolder really depends on the context.
EDIT: If you're just trying to read a file which is present at build time, you should include it in your built jar file and use either ClassLoader.getResourceAsStream or Class.getResourceAsStream to load it at execution time.
For example, if you have this structure:
src\
com\
xyz\
Foo.class
data\
input.txt
Then you could use Foo.class.getResourceAsStream("/data/input.txt") or Foo.class.getClassLoader.getResourceAsStream("data/input.txt"). Both will give you an InputStream you can use to load the data.

Categories

Resources