我对正则表达式模式匹配一无所知,并且当我通过 exec() 命令运行批处理文件时,我遇到文件路径中的单引号问题,即出现以下错误,即
错误是-
Windows 找不到“C:\Program”。
当 CMD 尝试进入所需目录时,我遇到单引号问题。 那么,任何人都可以告诉我在这里做什么?
我创建了一个批处理文件来编译和运行java程序我有一个名为createrunbat(String,String)的函数,以及以下代码:
private File createrunbat(String str,String par)
{
if(str.startsWith("Text Editor-",0))
{
str=str.replaceFirst("Text Editor-","");
}
String sng,s2;
File fe;
try{
FileOutputStream fos;
DataOutputStream dos;
sng=str;
int a=sng.indexOf(".");
sng=sng.substring(0,a);
file=new File(jfc.getSelectedFile().getParent(),sng+".bat");
fd=file.getAbsoluteFile();
str=fd.getParent().substring(0, 2);
fos=new FileOutputStream(file);
dos=new DataOutputStream(fos);
dos.writeBytes("@echo off \n");
dos.writeBytes("cd\\"+"\n");
if(fd.getParentFile().isDirectory())
{
dos.writeBytes(str+"\n");
}
s2=jfc.getSelectedFile().getParent();//I am having single quote problem from here
dos.writeBytes("cd "+s2+"\\"+"\n");
dos.writeBytes("javac "+sng+".java"+"\n");
dos.writeBytes("java "+sng+" "+par+"\n");
dos.writeBytes("pause \n");
dos.writeBytes("exit \n");
dos.close();
}
catch(FileNotFoundException ex)
{
}
catch(IOException ex2)
{
JOptionPane.showMessageDialog(this,ex2.toString());
}
return fd;
}
最佳答案
我认为这更多的是路径名中的空格引起了麻烦,您需要在路径周围加上引号
dos.writeBytes ("cd \"" + s2 +"\""+"\n");
关于java - java中如何去掉单引号和双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23005851/