【发布时间】:2017-04-14 02:59:15
【问题描述】:
我有一个可以在 Windows 命令提示符和 Cygwin 终端中使用的 Java 应用程序。该程序使用和操作文件路径。拥有一个sep 变量非常有用,当程序从 Cygwin 启动时为 /,而当程序从 Windows 启动时为 \\。
看着here,我不确定这是否可能,但我想问一下。
我将在几分钟后发布一个可编译的小型应用程序来显示问题。现在,我只想说我想要一组类似的函数:
// in main
...
String sep = getSeparatorToUse();
...
// member functions
...
private boolean wasLaunchedFromWinCmd()
{
if (<something-here-that-knows-it-was-cmd-not-cygwin>)
return true;
return false;
}//endof: private boolean wasLaunchedFromWinCmd()
private String getSeparatorToUse()
{
if (wasLaunchedFromWinCmd)
return "\\"
return "/"
}//endof: private String getSeparatorToUse()
感谢@Raphael_Moita。这些非常有用,我可能会在我将使用的应用程序的 Linux 版本中使用它们。 @Luke_Lee,我没有意识到这一点感到很愚蠢。我想你们两个可能在我准备好可编译代码的时候解决了我的问题。当程序从批处理脚本运行时仍然存在一个问题 - 当它从 find 命令中获取文件名时。我希望我展示的内容能说明这一点。
示例
所有示例都是从 Cygwin 运行的。
Works:大多数志愿者使用代码的方式,只是与 java 代码位于同一目录中的文件名。
$ java FileSeparatorExample pic_4.jpg
Here, something will be done with the file,
C:\Users\bballdave025\Desktop\pic_4.jpg
作品:在文件名/文件路径中使用相对文件路径和空格
$ java FileSeparatorExample pretty\ pictures/pic\ 1.jpg
Here, something will be done with the file,
C:\Users\me\Desktop\pretty pictures/pic 1.jpg
$ java FileSeparatorExample ../pic_5.jpg
Here, something will be done with the file,
C:\Users\me\Desktop\../pic_5.jpg
不起作用。有时,find 命令的输出会附带 Cygwin/UNIX 格式的完整文件路径:
$ java FileSeparatorExample /cygdrive/c/David/example/pic.jpg
The file:
C:\Users\bballdave025\Desktop\/cygdrive/c/David/example/pic.jpg
doesn't exist
可编译代码
我只是从我的原始代码中删减,所以如果它看起来太大,我很抱歉。
/**********************************
* @file FileSeparatorExample.java
**********************************/
// Import statements
import java.io.File;
import java.io.IOException;
public class FileSeparatorExample
{
// Member variables
private static String sep;
public static void main(String[] args)
{
////****** DOESN'T WORK AS DESIRED ******////
sep = java.io.File.separator;
////** I want **////
// sep = getFileSeparator();
String imageToLoad = null;
boolean argumentExists = ( args != null && args.length != 0 );
if (argumentExists)
{
boolean thereIsExactlyOneArgument = ( args.length == 1 );
if (thereIsExactlyOneArgument)
{
imageToLoad = args[0];
}//endof: if (thereIsExactlyOneArgument)
else
{
// do some other stuff
}
}//endof: if (argumentExists)
String filenamePath = getFilenamePath(imageToLoad);
String filenameFile = getFilenameFile(imageToLoad);
imageToLoad = filenamePath + sep + filenameFile;
File f = new File(imageToLoad);
if (! f.exists())
{
System.err.println("The file:");
System.err.println(imageToLoad);
System.err.println("doesn\'t exist");
System.exit(1);
}//endof: if (! f.exists())
System.out.println("Here, something will be done with the file,");
System.out.println(imageToLoad);
}//endof: main
// member methods
/**
* Separates the filename arg into: full path to directory; bare filename
*/
private static String[] splitFilename(String imageToLoad)
{
String[] fileParts = new String[2];
int indexOfLastSep = imageToLoad.lastIndexOf(sep);
boolean fullFilenameHasSeparator = ( indexOfLastSep != -1 );
if (fullFilenameHasSeparator)
{
fileParts[0] = imageToLoad.substring(0, indexOfLastSep);
fileParts[1] = imageToLoad.substring(indexOfLastSep + 1);
}//endof: if (fullFilenameHasSeparator)
else
{
// Use the user's directory as the path
fileParts[0] = System.getProperty("user.dir");
fileParts[1] = imageToLoad;
}//endof: if/else (fullFilenameHasSeparator)
return fileParts;
}//endof: private static String[] splitFilename(String imageToLoad)
/**
* Gives the full path to the file's directory (from the filename arg)
* but not the bare filename
*/
private static String getFilenamePath(String imageToLoad)
{
String[] fileParts = splitFilename(imageToLoad);
return fileParts[0];
}//endof: private static String getFilenamePath(String imageToLoad)
/**
* Gives the bare filename (no path information)
*/
private static String getFilenameFile(String imageToLoad)
{
String[] fileParts = splitFilename(imageToLoad);
return fileParts[1];
}//endof: private static String getFilenamePath(String imageToLoad)
}//endof: public class FileSeparatorExample
【问题讨论】:
-
在有人问之前,我确实知道
cygpath。如果我知道将使用 UNIX 样式的路径输入文件路径,我可以使用:java $(cygpath -wp <file-argument>)这在我的情况下不起作用。无论如何,这没有任何意义,因为那些将使用 Cygwin 程序的人在将 UNIX 风格的路径转换为 Windows 风格的路径时不会遇到问题。 -
您可以随时使用
/。
标签: java windows cygwin command-prompt terminal-emulator