相关疑难解决方法(0)

File.separator与路径中的斜杠之间的区别

在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));
    }
}
Run Code Online (Sandbox Code Playgroud)

要重新解释这个问题,如果/适用于Unix和Windows,为什么要使用File.separator

java

189
推荐指数
7
解决办法
28万
查看次数

如何以os独立方式设置Gradle`options.bootClasspath`?

因为我的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

java groovy gradle

20
推荐指数
1
解决办法
9012
查看次数

Groovy 将路径拆分为名称和父级

我正在尝试将路径分成父级和名称。

当尝试时

String path = "/root/file"
File file = new File(path)

println("Name: " + file.name)
println("Parent: " + file.parent)
Run Code Online (Sandbox Code Playgroud)

我们得到

Name: file
Parent: /root
Run Code Online (Sandbox Code Playgroud)

通过 Windows 路径C:\\root\\file.exe我们得到

Name: C:\root\file.exe
Parent: null
Run Code Online (Sandbox Code Playgroud)

这是预期的行为吗?如果是的话我如何获得 Windows 路径的相同结果?(如果可能请不要使用正则表达式)

groovy path

6
推荐指数
1
解决办法
5388
查看次数

如何根据操作系统更改文件路径

我有一个类,它读取特定位置的可用列表,

以下是我的代码,

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 = …
Run Code Online (Sandbox Code Playgroud)

java linux windows file

5
推荐指数
2
解决办法
2425
查看次数

用于声明文件路径位置的Java跨平台操作系统检测

我目前包含以下无法编译的代码.该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;
Run Code Online (Sandbox Code Playgroud)

java

4
推荐指数
1
解决办法
594
查看次数

Linux和Windows上java中的文件分隔符

我有一个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) …
Run Code Online (Sandbox Code Playgroud)

java path filepath

3
推荐指数
1
解决办法
1万
查看次数

如何使用Windows,Mac和Linux的通用文件路径保存文件?

我有一个aplicattion需要创建一个文件夹来解压缩一些文件,但我想传递一个文件路径,我的应用程序可以运行,创建和保存我的文件在任何操作系统,但我不知道应该是什么输出路径要做到这一点.

例如:

File folder = new File(outputFolder);
if (!folder.exists()) {
    folder.mkdir();
}
Run Code Online (Sandbox Code Playgroud)

我应该在outputFolder中使用什么路径在任何操作系统(Windows,Mac或Linux)的Documents/UnzipTest中创建我的文件夹?

java

2
推荐指数
1
解决办法
7574
查看次数

java中Windows与Unix上的文件路径问题

在我的程序中,我正在读取单元测试的资源文件。我使用文件路径为:

\\\path\\\to\\\file
Run Code Online (Sandbox Code Playgroud)

在我的机器(Windows)上运行良好。但在服务器(Unix)上,这失败了,我必须将其更改为:/path/to/file

但Java 应该是平台无关的。那么这样的行为是不是出人意料呢?

java platform-independent

1
推荐指数
1
解决办法
6576
查看次数

标签 统计

java ×7

groovy ×2

path ×2

file ×1

filepath ×1

gradle ×1

linux ×1

platform-independent ×1

windows ×1