0

我想知道是否有人可以帮我解决这个问题。我想通过 Linux 使用 java Eclipse 创建文件 .png 后立即更改/重命名文件的名称。因此,当文件保存到文件夹中时,我希望用户能够将其名称更改为用户想要的任何名称。我怎么做?这是我的代码...

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?

}
}

}
4

2 回答 2

2

您可以使用File#renameTo

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

笔记。如果您更改文件存储的位置,这将像一个移动

于 2013-09-13T23:11:17.457 回答
1
String fileRenamed = JOptionPane.showInputDialog(null, "Please Input New Name"); 


Process proc = Runtime.getRuntime().exec("mv "+ pathToFile+namestuff+" "+pathToFile+fileRenamed);
于 2013-09-13T23:17:39.073 回答