Switch the name of the file [duplicate] - java

This question already has answers here:
Rename a file using Java
(15 answers)
Closed 9 years ago.
I have file names like 323423233.
I want to add the last 2 digits of the file name and add it to the front and
make it 33/323423233 and add extension to it(like .doc).
What's a simple statement that I can use to achieve this?

This is 2013. This is Java 7. This is the time for Files and Path.
Base directory:
final Path baseDir = Paths.get("/path/to/baseDir");
Determine subdirectory for a file:
final String s = name.substring(name.length() - 2, name.length());
Create that directory:
final Path subDir = baseDir.resolve(s);
// Will not do anything if directory already exists...
// But will throw exception if unable to create
Files.createDirectories(subDir);
Write to the file:
final Path dst = subDir.resolve(name + ".doc");
Files.copy(src, dst);
Remove original:
Files.delete(src);
Or in one operation:
Files.move(src, dst);

Related

Class use txt file in the same jar NPE [duplicate]

This question already has answers here:
How to read a file from jar in Java?
(6 answers)
How to read a text file inside a JAR? [duplicate]
(4 answers)
Closed 3 years ago.
I have class files and a text file wrapped up in a jar. This problem has been solved on the internet before, but when I try it I get a null pointer exception, or File f.exists() returns false. It should be noted that my code is not in a package. It should be noted that when help.txt is dropped in the same folder as the jar, then it works.
`MyClass z = new MyClass();
String helpPath = z.getClass().getClassLoader().getResource("help.txt").toString();
File f = new File(helpPath);
if (f.exists()){
Desktop d = Desktop.getDesktop();
d.open(f);`
It should also be noted that I have code written to open powershell and then java my class file, with no specified classpath.
` Runtime.getRuntime().exec(new String[]{"cmd","/c","start","powershell","-noexit","/c","java -jar \"" + filename + "\""});`

Getting the full path of parent directory [java] [duplicate]

This question already has answers here:
How to get just the parent directory name of a specific file
(10 answers)
Closed 4 years ago.
It should be rather easy, but I could not find a clean way to do so. I'm trying to get the parent full path of parent directory in a path. Consider the following path: /bin/src/config/file, I would like to get /bin/src/config in Java. So I get a string and need to get the full path (absolute path, not just the name and not relative) of the parent directory. What is the cleanest way to do so?
You can use this, which will print the folder your file is in where fileName is your filename:
Path f = Paths.get(fileName);
System.out.println(f.getParent());
Eg for String fileName = "C:\\Users\\Me\\Documents\\video.html" the output is C:\Users\Me\Documents.

How to open a file in the same directory as the .jar file of the application? [duplicate]

This question already has answers here:
How to get the real path of Java application at runtime?
(15 answers)
Closed 6 years ago.
I'm trying to load a .txt file into an arrayList in java using a combination of relative paths.
My jar file is in /usr/tool/dist/tool.jar
The file I want to load is in /usr/tool/files/file.txt
I think I was able to retrieve the path of my tool.jar, but how can I go from that path to the one where my file is?
I have the following code
// String path should give me '/usr/tool'
File f = new File(System.getProperty("java.class.path"));
File dir = f.getAbsoluteFile().getParentFile();
String path = dir.toString();
String table1 = this should represent /usr/tool/files/file.txt
BufferedReader buf_table1 = new BufferedReader(new FileReader(new File(table1)));
To find the path of your jar file being executed, java.class.path is not the right property. This property may contain more than one file, and you cannot know which is the right one. (See the docs.)
To find the path of the correct jar file, you can use this instead:
URL url = MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Where MainClass the main class of your tool, or any other class in the same jar file.
Next, the parent File of a file is its directory. So the parent File of /usr/tool/dist/tool.jar is /usr/tool/dist/. So if you want to get to /usr/tool/files/file.txt, you need to get the parent of the parent, and then from there files/file.txt.
Putting it together:
File jarFile = new File(MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
File file = new File(jarFile.getParentFile().getParent(), "files/file.txt");

File.Separator in Properties [duplicate]

This question already has answers here:
Java properties file specs [duplicate]
(2 answers)
Closed 7 years ago.
I am using java.util.Properties. However, it cannot read File.separator inside the config file.
As an example, I add this line to the config file. source.dir = D:/workspace/Temp1\Temp2 (Note that File.separator is used to separate Temp1 and Temp2)
The below line is used to load propertis:
Properties properties = new Properties ();
properties.load(new FileInputStream("configFileAddress"));
The result is: source.dir = D:/workspace/Temp1Temp2 (File.Separator is removed).
Any one knows, how can I fix that?
Replace:
source.dir = D:/workspace/Temp1\Temp2
To:
source.dir = D:\\workspace\\Temp1\\Temp2
This field is initialized to contain the first character of the value
of the system property file.separator. On UNIX systems the value of
this field is '/'; on Microsoft Windows systems it is '\'.

How to create empty folder in java? [duplicate]

This question already has answers here:
How to create a folder in Java?
(8 answers)
Closed 3 years ago.
I tried to use the File class to create an empty file in a directory like "C:/Temp/Emptyfile".
However, when I do that, it shows me an error : "already made folder Temp". Otherwise, it won't create one for me.
So, how do I literally create folders with java API?
Looks file you use the .mkdirs() method on a File object: http://www.roseindia.net/java/beginners/java-create-directory.shtml
// Create a directory; all non-existent ancestor directories are
// automatically created
success = (new File("../potentially/long/pathname/without/all/dirs")).mkdirs();
if (!success) {
// Directory creation failed
}
You can create folder using the following Java code:
File dir = new File("nameoffolder");
dir.mkdir();
By executing above you will have folder 'nameoffolder' in current folder.

Categories