java - java中如何去掉单引号和双引号

标签 java regex string batch-file

我对正则表达式模式匹配一​​无所知,并且当我通过 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/

相关文章:

java - 无法从 IntelliJ IDEA 12.1.4 Ultimate (Windows 7) 启动 Apache Tomcat 6

java - 使用 Java Spark - 错误 : Cannot load main class from JAR

java - 正则表达式中的 StackOverflowError

java - 使用正则表达式从操作数中拆分并提取运算符

regex - 在csv doc中使用powershell,需要迭代并插入字符

c++ - boolean 函数

java - Maven Junit 测试用例因 java.lang.NoClassDefFoundError : org/slf4j/LoggerFactory 崩溃

C++文件输出只接受第一个字

java - 意外的字符串连接行为

java - 不关闭 hibernate session 时的奇怪行为