Java 将命令行命令添加到文件重命名批处理
我有一个工作文件重命名java工具,现在我想添加一个if条件来运行命令行命令,如果我选中一个复选框作为重命名过程的一部分,它将重命名每个文件 稍后我将更改dos代码,但我发现这是一个有效的示例。我的部分问题是我的filerename是它自己的类,所以我还需要弄清楚如何组合这个类或从我的主rename类中以某种方式引用dos命令 更新 我用答案中的更改更新了代码,但是命令行命令不起作用,并且它在没有错误的情况下崩溃了java。该命令在cmd行中不起作用Java 将命令行命令添加到文件重命名批处理,java,windows,Java,Windows,我有一个工作文件重命名java工具,现在我想添加一个if条件来运行命令行命令,如果我选中一个复选框作为重命名过程的一部分,它将重命名每个文件 稍后我将更改dos代码,但我发现这是一个有效的示例。我的部分问题是我的filerename是它自己的类,所以我还需要弄清楚如何组合这个类或从我的主rename类中以某种方式引用dos命令 更新 我用答案中的更改更新了代码,但是命令行命令不起作用,并且它在没有错误的情况下崩溃了java。该命令在cmd行中不起作用 import java.io.IOExcep
import java.io.IOException;
import java.io.InputStream;
public class doscommandrun {
public static void run() {
final String dosCommand = "cmd converter.exe file.doc -android -o file.txt";
try {
final Process process = Runtime.getRuntime().exec(
dosCommand + " ");
final InputStream in = process.getInputStream();
int ch;
while((ch = in.read()) != -1) {
System.out.print((char)ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
在此处输入代码
文件重命名代码:
private void renameFile(){
boolean operationResult = false;
boolean overallResult = true;
int failCount = 0;
/* the operation of this part is ensured by the chooseDirectory()
* WE get the list of files in the directory
* get the conditions set by users
* and perform the file rename operation.
*/
//Let's get all the information from user
String[] fileList = directory.list(); //the list of files in the directory
String Prefix = txtPrefix.getText();
String Rename = txtRename.getText();
String Suffix = txtSuffix.getText();
String digits = (String) cboSequence.getSelectedItem();
int StartingNum;
String generatedSequence;
File oldFile;
//let's call the output frame
if(cbxOutput.isSelected() && OUTPUT_ON == false){
buildOutput();
OUTPUT_ON = true;
}
//display the list of files and readability of each file
for(int i = 0; i < fileList.length; i++){
oldFile = new File(directory.getPath()+"/"+ fileList[i]);
String readability = fileList[i] +" - readable?: "+oldFile.canRead();
System.out.println(readability);
if(OUTPUT_ON)
txaOutput.append("\n"+readability);
}
for(int i = 0; i < fileList.length; i++){
/* get the file extension that we need, and form a new name,
* we would check if the Ignore File Extension is selected
*/
oldFile = new File(directory.getPath()+"/"+ fileList[i]);
String fileExtension;
if(cbxIgnoreExtension.isSelected() == true ){
fileExtension = "";
}
else
fileExtension = getFileExtension(fileList[i]);
//this part get the original filename
String fileName = getFileName(fileList[i]);
String inputInfo = "The input filename->"+ fileList[i] + "\nfile name->" + fileName + "\nextension->" + fileExtension;
System.out.println(inputInfo);
if(OUTPUT_ON)
txaOutput.append("\n"+inputInfo);
/* generate sequence for the Name
*if the digits selection is NONE, we ignore it
*/
if(digits.equals("None") == true){
generatedSequence = "";
}
else{
StartingNum = Integer.parseInt(txtSequence.getText());
generatedSequence = nameSequence(StartingNum + i, digits);
}
//this is affected by the RenameOption, if Rename has something then only we RENAME
if(cbxRename.isSelected() == true){
fileName = Rename + generatedSequence; //the fileName will change.
}
else{
//if Rename has nothing, but the txtSequence has some Value, we take it to the naming too
fileName = fileName.substring(0,4)+ generatedSequence;
if(cbxAndroid.isSelected() == true ){
doscommandrun.run();
}
//the New File Name
String newFileName = Prefix + fileName.substring(0,4) + Suffix + fileExtension;
String tentativeName = "new Filename will be ->"+newFileName+"\n";
System.out.println(tentativeName);
if(OUTPUT_ON)
txaOutput.append("\n"+tentativeName);
// ! Perform the file rename, if the Experimental Mode is not selected
if(cbxExperiment.isSelected() == false){
operationResult = oldFile.renameTo(new File(directory.getPath()+"/"+newFileName));
String renameResult = "\t*Rename successfully?: " + operationResult+"\n\n";
System.out.println(renameResult);
if(operationResult == false)
failCount++;
if(OUTPUT_ON)
txaOutput.append("\n"+renameResult);
//make up the overall result
overallResult = (operationResult && overallResult);
}
}
if(cbxExperiment.isSelected() == false){
System.out.println("Overall Result: "+overallResult);
if(overallResult)
JOptionPane.showMessageDialog(null, "All files renamed successfully!");
else
JOptionPane.showMessageDialog(null, "File renamed with "+ failCount+ " failure(s)");
}//end if
}
}//end renameFile
private void重命名文件(){
布尔运算结果=false;
布尔整体结果=真;
int故障计数=0;
/*此部分的操作由所选目录()确保
*我们得到目录中的文件列表
*获取用户设置的条件
*并执行文件重命名操作。
*/
//让我们从用户那里获取所有信息
String[]fileList=directory.list();//目录中的文件列表
字符串前缀=txtPrefix.getText();
String Rename=txtRename.getText();
字符串后缀=txtSuffix.getText();
字符串数字=(字符串)cboSequence.getSelectedItem();
int StartingNum;
字符串生成序列;
文件oldFile;
//让我们调用输出帧
if(cbxOutput.isSelected()&&OUTPUT_ON==false){
buildOutput();
输出_ON=真;
}
//显示文件列表和每个文件的可读性
对于(inti=0;i”+文件列表[i]+“\n文件名->”+文件名+“\nextension->”+文件扩展名;
系统输出打印LN(输入信息);
如果(输出_打开)
txaOutput.append(“\n”+inputInfo);
/*为名称生成序列
*如果数字选择为“无”,我们将忽略它
*/
if(数字等于(“无”)==真){
generatedSequence=“”;
}
否则{
StartingNum=Integer.parseInt(txtSequence.getText());
generatedSequence=nameSequence(StartingNum+i,数字);
}
//这受Rename选项的影响,如果Rename有某些内容,则只有我们重命名
if(cbxRename.isSelected()==true){
fileName=Rename+generatedSequence;//文件名将更改。
}
否则{
//如果Rename没有任何内容,但是txtSequence有一些值,那么我们也将其带到命名中
fileName=fileName.substring(0,4)+generatedSequence;
if(cbxAndroid.isSelected()==true){
doscommandrun.run();
}
//新文件名
字符串newFileName=前缀+文件名。子字符串(0,4)+后缀+文件扩展名;
String tentiveName=“新文件名将为->”+newFileName+“\n”;
系统输出打印LN(tentiveName);
如果(输出_打开)
txaOutput.append(“\n”+tentiveName);
//!如果未选择实验模式,请执行文件重命名
if(cbxExperiment.isSelected()==false){
operationResult=oldFile.renameTo(新文件(directory.getPath()+“/”+newFileName));
字符串重命名结果=“\t*重命名成功?”:“+operationResult+”\n\n”;
System.out.println(重命名结果);
if(operationResult==false)
故障计数++;
如果(输出_打开)
txaOutput.append(“\n”+重命名结果);
//总结结果
总体结果=(操作结果&总体结果);
}
}
if(cbxExperiment.isSelected()==false){
System.out.println(“总体结果:+总体结果”);
如果(总体结果)
showMessageDialog(null,“所有文件重命名成功!”);
其他的
showMessageDialog(null,“文件重命名为“+failCount+”failure(s)”);
}//如果结束
}
}//结束重命名文件
您可以在doccommandrun类中创建一个名为run()
的静态方法,该方法在调用时将执行主方法中当前的内容
public class DosCommandRun
{
public static void run()
{
//..do stuff from main
}
}
现在,只要您想调用dos命令,就可以在代码中插入
doscondrun.run()
。我在renametool类中的cmd之后添加/c
我仍然有一些问题,但有一个不同的问题。请把你的问题说得更具体一些。很难确切地理解你在说什么asking@jerhynsoen您必须从流程的输入流和错误流中读取数据。否则,当错误发生时,您将遇到问题。为此,您将不得不使用线程。我使用我的代码来执行您建议的操作,但这不起作用。我更新了此问题中的代码以反映这些更改。