在Java Path-String中使用File.separator和普通/有什么区别?
与双反斜杠\\平台相比,独立性似乎不是原因,因为两个版本都可以在Windows和Unix下运行.
public class SlashTest {
    @Test
    public void slash() throws Exception {
        File file = new File("src/trials/SlashTest.java");
        assertThat(file.exists(), is(true));
    }
    @Test
    public void separator() throws Exception {
        File file = new File("src" + File.separator + "trials" + File.separator + "SlashTest.java");
        assertThat(file.exists(), is(true));
    }
}
要重新解释这个问题,如果/适用于Unix和Windows,为什么要使用File.separator?
因为我的Java源代码和目标必须是JRE 1.6兼容的,所以我需要设置options.bootClasspath一个包含1.6版本rt.jar和的路径jce.jar.它必须建立在Windows和Unix(Linux/Solaris)上.这样做的正确方法是什么?我现在在我的顶层使用以下方法build.gradle,它可以工作,但它似乎远非优雅,尤其是依赖于os的分隔符:或;:
import org.apache.tools.ant.taskdefs.condition.Os
subprojects {
  apply plugin: 'java'
  compileJava {
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
    def java6_home = System.getenv("JAVA_HOME_6")
    def java6_lib = "C:/localdata/Program Files (x86)/Java/jdk1.6.0_45/jre/lib/"
    if (java6_home != null) {
      java6_lib = java6_home + "/jre/lib/"
    }
    def sep = ':'
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
      sep = ';'
    }
    options.bootClasspath = java6_lib + "rt.jar" + sep + java6_lib + "jce.jar"
  }
}
我正在尝试将路径分成父级和名称。
当尝试时
String path = "/root/file"
File file = new File(path)
println("Name: " + file.name)
println("Parent: " + file.parent)
我们得到
Name: file
Parent: /root
通过 Windows 路径C:\\root\\file.exe我们得到
Name: C:\root\file.exe
Parent: null
这是预期的行为吗?如果是的话我如何获得 Windows 路径的相同结果?(如果可能请不要使用正则表达式)
我有一个类,它读取特定位置的可用列表,
以下是我的代码,
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExceptionInFileHandling {
   @SuppressWarnings({ "rawtypes", "unchecked" })
   public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
       try {
           File l_Directory = new File(a_Path);
           File[] l_files = l_Directory.listFiles();
           for (int c = 0; c < l_files.length; c++) {
               if (l_files[c].isDirectory()) {
                   a_folders.add(l_files[c].getName());
               } else {
                   a_files.add(l_files[c].getName());
               }
           }
       } catch (Exception ex){
           ex.printStackTrace();
       }
   }
   @SuppressWarnings("rawtypes")
   public static void main(String args[]) throws IOException {
       String filesLocation = …我目前包含以下无法编译的代码.该else if声明报告说' ;' expected.我不明白为什么我不能使用else if这种情况?
public class FileConfiguration {
    private String checkOs() {
        String path = "";        
        if (System.getProperty("os.name").startsWith("Windows")) {
            // includes: Windows 2000,  Windows 95, Windows 98, Windows NT, Windows Vista, Windows XP
            path = "C://Users//...";            
        }
        elseif (System.getProperty("os.name").startsWith("Mac")) {
            path = "///Users//...";
        }
        return path;        
    }
    // declare paths for file source and destination 
    String destinationPath = path;
    String sourcePath = path;
我有一个java swing数据库应用程序需要在Windows和Linux上运行,我的数据库连接细节存储在XML文件中,我加载它们,
这个应用程序可以在Linux上正确加载这个属性,但它不能在Windows上工作,请告诉我如何在多个平台上正确加载文件.
这是代码,
PropertyHandler propertyWriter = new PropertyHandler();
List keys = new ArrayList();
keys.add("ip");
keys.add("database");
Map localProps = propertyWriter.read(keys, "conf" + File.separatorChar + "properties.xml", true);//if false load from the local properties
//get properties from the xml in the internal package
List seKeys = new ArrayList();
seKeys.add("driver");
seKeys.add("username");
seKeys.add("password");
Map seProps = propertyWriter.read(seKeys, "conf" + File.separatorChar + "properties.xml", true);
String dsn = "jdbc:mysql://" + (String) localProps.get("ip") + ":3306/" + (String) localProps.get("database");
jDBCConnectionPool = new JDBCConnectionPool((String) seProps.get("driver"), dsn, (String) seProps.get("username"), (String) …我有一个aplicattion需要创建一个文件夹来解压缩一些文件,但我想传递一个文件路径,我的应用程序可以运行,创建和保存我的文件在任何操作系统,但我不知道应该是什么输出路径要做到这一点.
例如:
File folder = new File(outputFolder);
if (!folder.exists()) {
    folder.mkdir();
}
我应该在outputFolder中使用什么路径在任何操作系统(Windows,Mac或Linux)的Documents/UnzipTest中创建我的文件夹?
在我的程序中,我正在读取单元测试的资源文件。我使用文件路径为:
\\\path\\\to\\\file
在我的机器(Windows)上运行良好。但在服务器(Unix)上,这失败了,我必须将其更改为:/path/to/file
但Java 应该是平台无关的。那么这样的行为是不是出人意料呢?