This question already has answers here:
How do I rename (not move) a file in Java 7?
(6 answers)
Closed 8 years ago.
I've got a list of files:
"f1.txt"
"f2.txt"
"f3.txt"
"f4.txt"
and want to batch rename to:
"file1.txt"
"file2.txt"
"file3.txt"
"file4.txt"
Ideally I would like to do this as a little java program but don't mind it being done in something like Windows PowerShell.
Thanks in advance
Lee
Just buil a function that get two parametrs oldname,and newname and put it inside
// File with old name
File file = new File("oldname");
// File with new name
File file2 = new File("newname");
if(file2.exists()) throw new java.io.IOException("file exists");
// Rename file
boolean success = file.renameTo(file2);
if (!success) {
}
java.io.FileWriter out= new java.io.FileWriter(file2, true );//append=yes
Powershell, will rename all files in $path:
$path = "C:\pathToFiles"
cd $path
ls | % { Rename-Item $_.Name $_.Name.replace("f","file") }
Related
This question already has answers here:
How to handle ~ in file paths
(5 answers)
Closed 3 years ago.
File f = new File("~/NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
cat ~/NetBeansProjects/ChatApp/src/chatapp/Server.java, prints the content of the file.
But the above program doesn't print "File exist".
The ~ is resolved by the shell, whereas Java do not resolve it. Try something like this:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
The "home" wildcard (~) cannot be resolved in the JVM. You need to load that property via the Java API:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
This question already has an answer here:
Any way to get a File object from a JAR
(1 answer)
Closed 5 years ago.
I'm trying to implement a button into my project which, when clicked, automatically loads a specific file. Currently there are buttons for users selecting a file from their hard disk.
So, I downloaded the specific file and inserted it into the project. When using File f = new File("demofile") or something like this
getClass().getResource("/resources/file.txt").getFile(); the code WORKS locally.
However, when the project is packaged, a FileNotFoundException is thrown.
After much research online, there are suggestions to use something like:
InputStream is = getClass().getResourceAsStream("/resources/file.txt");
However, for this project, I need the file to be referenced as a file object so that it can be passed as an argument to other functions, such as:
in = new TextFileFeaturedSequenceReader(TextFileFeaturedSequenceReader.FASTA_FORMAT, file, DiffEditFeaturedSequence.class);
Any ideas on how I can solve this, or read a stream into a file object?
Thanks!
If you absolutely must pass a File, copy your resource to a temporary file:
Path path = Files.createTempFile(null, null);
try (InputStream stream =
getClass().getResourceAsStream("/resources/file.txt")) {
Files.copy(stream, path, StandardCopyOption.REPLACE_EXISTING);
}
in = new TextFileFeaturedSequenceReader(
TextFileFeaturedSequenceReader.FASTA_FORMAT,
path.toFile(),
DiffEditFeaturedSequence.class);
// Use the TextFileFeaturedSequenceReader as needed
// ...
Files.delete(path);
This question already has answers here:
How do I run a batch file from my Java Application?
(12 answers)
Closed 5 years ago.
I am facing below error:
java.io.IOException: Cannot run program "C:\abc\man\b\manu.bat C:\Users\12x\test\testFiles\abc.properties" (in directory "C:\Users\12x\test\testFiles\abc.properties"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048).
Please find the code i am using:
public class TestProcess {
public TestProcess(Path workPath, Path exe, Path logbackConfig,
Path propertyfile) throws IOException {
String exeSuffix = "";
if (OS.indexOf("win") >= 0) {
exeSuffix = ".bat";
}
builder = new ProcessBuilder()
.directory(workPath.toFile())
.command(workPath.resolve(exe).toAbsolutePath().toString() + exeSuffix+ " " + propertyfile)
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT);
My aim is to run a bat file(which is present in C:\abc\man\b folder) followed by abc.properties(which is in another folder C:\Users\12x\test\testFiles).
In code above, workPath has the value
C:\abc\man\b
and propertyfile has
C:\Users\12x\test\testFiles
You can't exec() a .bat file directly in Windows. You have to interpose cmd /c.
You do not use the correct syntax: you can not concatenate the program and its arguments in a string, since ProcessBuilder is not a parser.
Instead, build a String named propertyfile_path_string corresponding to the properties file, and replace your line .command(...) by this one:
.command(workPath.resolve(exe).toAbsolutePath().toString() + exeSuffix, propertyfile_path_string)
This question already has answers here:
create a text file in a folder
(4 answers)
Closed 9 years ago.
Okay, updated this right now
As of now I have this:
File saveGame = new File(Config.saveDir,Config.saveName);
Now how would I create that into a text file? My saveDir has already been created (mkDir), and my saveName is defined as "xyz.txt", so what method do I use to create this file (and later add text into it)?
new File(...) doesn't crete a file on disk, it only creates a path for a Java program to use to refer to a file that may or may not exist on disk (hence the File.exists() method).
Try userFile.createNewFile(); to actually create the file on disk.
To make the directory you would need to use the File.mkdirs() method, but don't call it on userFile or it will make a directory with the Savegame.txt in it.
Edit:
File dir = new File(Config.userpath + "/test/");
File file = new File(dir, "," + Config.name + " Savegame.txt");
dir.mkdir(); // should check to see if it succeeds (if(dir.mkdir())...)
file.createNewFile(); // should also check that this succeeds.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In java under Windows, how do I find a redirected Desktop folder?
How to get the Desktop path in java
I want to write my results to the desktop of the user rather than to the same directory as file class that I am running.
I am using Mac OS.. How about in Window?1
Thanks
The user's home directory is:
System.getProperty("user.home")
In general +"/Desktop" would do, but is not portable.
String userHomeFolder = System.getProperty("user.home");
File textFile = new File(userHomeFolder, "mytext.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(textFile));
try {
...
} finally {
out.close();
}
This would write the file "mytext.txt" to the home directory.