user2367924
user2367924

Reputation: 75

How to Change a .PNG file name through Java/Eclipse

I was wondering if anyone can help me solve this. I want to change/rename the name of a file .png as soon as I create it using java Eclipse through Linux. So when the file is saved into a folder I want the user to be able to change the name of it to whatever the user wants. How do I do that? Here is my code...

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


public class adbPicture implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {


// exec.(points at whatever .sh file you want to run in the folder)
try {
Process proc = Runtime.getRuntime().exec("sh /home/local/ANT/arthm/Desktop/stuff/pics1.sh);
BufferedReader read = new BufferedReader(new InputStreamReader(             proc.getInputStream()));
try {
proc.waitFor();
} catch (InterruptedException e) {
 System.out.println(e.getMessage());
}
while (read.ready()) {
System.out.println(read.readLine());
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error 5! You Messed Up!");
}



int picreply = JOptionPane.showConfirmDialog(null, "Would you like to change the name?", null, JOptionPane.YES_NO_OPTION);
if(picreply == JOptionPane.YES_OPTION){
String namestuff = JOptionPane.showInputDialog(null, "Please Input Name"); 

// Here in this area is where I want to add the code to change the name of the file...
}
else {
System.exit(0); // also this makes me exit the whole program, how do I only exit the //showConfirmDialog box?

}
}

}

Upvotes: 0

Views: 714

Answers (2)

anguyen
anguyen

Reputation: 477

String fileRenamed = JOptionPane.showInputDialog(null, "Please Input New Name"); 


Process proc = Runtime.getRuntime().exec("mv "+ pathToFile+namestuff+" "+pathToFile+fileRenamed);

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347214

You can use File#renameTo

File original = new File("...");
original.renameTo(new File("..."));

Note. If you change the location that the file is stored in, this will act like a move

Upvotes: 2

Related Questions