使用cmd打开具有多个空格的文件的问题 [英] problem in opening file with multiple blank spaces using cmd

查看:333
本文介绍了使用cmd打开具有多个空格的文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用java调用以下命令



这是java初始化

 code> String fileName =C:\\temp\\ a axtxt; 
String sCmd =cmd / c start \\\+ fileName +\;

这是我打印sCmd时得到的

  cmd / c start''C:\temp\A a.txt'

这是我如何运行命令

  Runtime.getRuntime ); 

文件名包含多个空格,当我从Java运行此命令时抛出一个错误,它不能识别多个空格。当没有空格或一个空格时有效?如何通过Windows命令处理多个空格的文件



示例java程序

  import java.io.File; 
import java.io.IOException;

public class A
{
public static void main(String [] args)
{
String fileName =C:\\temp\\a dfdfd f.txt;
文件file = new File(fileName);
String sCmd =cmd / c start \\\+ file.getAbsolutePath()+\;

System.out.println(exec cmd =<+ sCmd +>);
try
{
Runtime.getRuntime()。exec(sCmd);
}
catch(IOException e)
{
// TODO自动生成的catch块
e.printStackTrace();
}
}
}



这是java输出

  exec cmd =< cmd / c startC:\temp\a dfdfd f.txt> 

我在Windows XP中运行,进一步这不是打开任何文件留下一个空格。 / p>

解决方案:

  import java.io.File; 
import java.io.IOException;

public class A
{
public static void main(String [] args)
{
// String fileName =C:\\\ \\temp\\a.txt;
String fileName =C:\\temp\\a dfdfd f.txt;
文件file =新文件(fileName);
String sCmd =cmd / c start \\\+ file.getAbsolutePath()+\;

System.out.println(exec cmd =<+ sCmd +>);
try
{
Runtime.getRuntime()。exec(sCmd.split());
}
catch(IOException e)
{
// TODO自动生成的catch块
e.printStackTrace();
}
}
}


解决方案>

cmd / c startC:\temp\A a.txt从命令行工作。
如果通过Java的 Runtime.getRuntime()调用此命令,则需要使用反斜杠转义上述命令中的双引号exec(...)


I am calling the below command using java

This is the java initialization

String fileName="C:\\temp\\A  a.txt";
String  sCmd = "cmd /c start \"\" \"" + fileName + "\"";

This is what I get when I print sCmd

 cmd /c start '" 'C:\temp\A   a.txt'

This is how I run the command

 Runtime.getRuntime().exec(sCmd);

The file name contains multiple spaces and when I run this command from Java its throwing an error because its not recognizing the multiple spaces.It works when no space or one space is there?How to handle files with multiple spaces through windows command

Sample java program

   import java.io.File;
import java.io.IOException;

public class A
{
    public static void main(String[] args)
    {
        String fileName = "C:\\temp\\a  dfdfd   f.txt";
        File file = new File(fileName);
        String sCmd = "cmd /c start \"\" \"" + file.getAbsolutePath() + "\"";

        System.out.println("exec cmd=<" + sCmd + ">");
        try
        {
            Runtime.getRuntime().exec(sCmd);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This gives me error when from windows side when I run the java code.

This is the java output

exec cmd=<cmd /c start "" "C:\temp\a  dfdfd   f.txt">

and I am running in Windows XP further this is not opening any file leave aside one with spaces.

Solution:

import java.io.File;
import java.io.IOException;

public class A
{
    public static void main(String[] args)
    {
        // String fileName = "C:\\temp\\a.txt";
        String fileName = "C:\\temp\\a  dfdfd   f.txt";
        File file = new File(fileName);
        String sCmd = "cmd /c start \"\" \"" + file.getAbsolutePath() + "\"";

        System.out.println("exec cmd=<" + sCmd + ">");
        try
        {
            Runtime.getRuntime().exec(sCmd.split(" "));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

解决方案

cmd /c start "C:\temp\A a.txt" works from the command line. You would need to escape the double quotes in the above command with a back slash if this command is called via Java's Runtime.getRuntime().exec(...)

这篇关于使用cmd打开具有多个空格的文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆