我想知道Java中是否有这样的方法.以此片段为例:
// this will output a/b
System.out.println(path_join("a","b"));
// a/b
System.out.println(path_join("a","/b");
Run Code Online (Sandbox Code Playgroud) 我有一个像下面的文件夹结构:
- main
-- java
-- resources
-- scalaresources
--- commandFiles
Run Code Online (Sandbox Code Playgroud)
在那个文件夹中,我有我必须阅读的文件.这是代码:
def readData(runtype: String, snmphost: String, comstring: String, specificType: String): Unit = {
val realOrInvFile = "/commandFiles/snmpcmds." +runtype.trim // these files are under commandFiles folder, which I have to read.
try {
if (specificType.equalsIgnoreCase("Cisco")) {
val specificDeviceFile: String = "/commandFiles/snmpcmds."+runtype.trim+ ".cisco"
val realOrInvCmdsList = scala.io.Source.fromFile(realOrInvFile).getLines().toList.filterNot(line => line.startsWith("#")).map{
//some code
}
val specificCmdsList = scala.io.Source.fromFile(specificDeviceFile).getLines().toList.filterNot(line => line.startsWith("#")).map{
//some code
}
}
} catch {
case e: Exception => e.printStackTrace
} …Run Code Online (Sandbox Code Playgroud) 我已将我的应用程序部署到jar文件.当我需要将数据从一个资源文件复制到jar文件之外时,我执行以下代码:
URL resourceUrl = getClass().getResource("/resource/data.sav");
File src = new File(resourceUrl.toURI()); //ERROR HERE
File dst = new File(CurrentPath()+"data.sav"); //CurrentPath: path of jar file don't include jar file name
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dst);
// some excute code here
Run Code Online (Sandbox Code Playgroud)
我遇到的错误是:URI is not hierarchical.在IDE中运行时我不满足此错误.
如果我更改上面的代码作为StackOverFlow上其他帖子的一些帮助:
InputStream in = Model.class.getClassLoader().getResourceAsStream("/resource/data.sav");
File dst = new File(CurrentPath() + "data.sav");
FileOutputStream out = new FileOutputStream(dst);
//....
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) { …Run Code Online (Sandbox Code Playgroud) 我有一个.jar文件,我正在整理.我想创建一个非常简单的.properties文件,其中包含用户名和其他内容等可配置的东西,这样他们就可以手工编辑而不是必须包含GUI编辑器.
我想做的是能够按以下顺序搜索:
args[0])我知道如何访问#1和#3(我认为),但如何在运行时#2和#4确定?
我只想在我的程序中读取一个文件.该文件位于工作目录上方的一个目录"../f.fsh".因此,当我在IDE中运行它时,以下代码正确运行
String name="../f.fsh";
InputStream is = getClass().getResourceAsStream(name);
InputStreamReader isreader=new InputStreamReader(is);//CRASHES HERE WITH NULL POINTER EXCEPTION
BufferedReader br = new BufferedReader(isreader);
Run Code Online (Sandbox Code Playgroud)
但是当我创建一个在其中压缩了f.fsh并运行它的JAR文件时,它会在创建InputStreamReader时崩溃,因为InputStream为null.
我已经阅读了一些关于输入流和JAR文件的问题的答案,我从中得到的是我应该使用相对路径,但我已经这样做了.根据我的理解,getResourceAsStream()可以找到相对于项目根目录的文件,这就是我想要的.为什么它在JAR中不起作用?出了什么问题,我该如何解决?
它与classpath有关吗?我认为这只是为了包含正在运行的jar外部的文件.
在尝试使用斜杠时,我也尝试了但仍然失败了:
InputStream is = getClass().getResourceAsStream("\\"+name);
Run Code Online (Sandbox Code Playgroud)
我看了一下:如何获取Java JAR文件中的资源路径,并发现JAR的内容可能不一定可以作为文件访问.所以我尝试将文件相对于jar(jar中的一个目录)复制,但仍然失败.在任何情况下,我都希望将我的文件留在jar中,并能够在那里阅读它们.我不知道出了什么问题.
我有一个 Gradle 插件,任务如下:
@TaskAction
def nyTask() {
def myFlags = project.pluginConfig.myFlags
if myFlags == null {
myFlags = "-arg1 absolutePathToFile/file1.txt -arg2 absolutePathToFile/file2.txt"
// *** this is the part I don't know ***
}
project.exec {
executable aCommand
args myFlags.split()
}
}
Run Code Online (Sandbox Code Playgroud)
myFlags在插件的扩展中定义。使用该插件时,我可以将其配置为:
myPlugin {
myFlags = "-arg1 absolutePathToFile/file1.txt -arg2 absolutePathToFile/file2.txt"
}
Run Code Online (Sandbox Code Playgroud)
我的插件src/main/resources/文件夹中有一些默认文件,即
src/main/resources/file1.txt
src/main/resources/file2.txt
Run Code Online (Sandbox Code Playgroud)
如果myFlags == null那么我想使用资源文件夹中的文件。
问题是我不知道如何获取这些文件的正确路径。
如何获取插件资源文件夹中file1.txt和file2.txt的绝对路径?
经过几个小时的斗争后,我开始感到非常沮丧。
我有一个 JKS 服务器文件,我想从 jar 中加载它。只要文件在 Jar 之外,使用 FileInputStream 就可以了,但是一旦我尝试将其更改为 getResourceAsStream,它就不会接受它,并说(没有这样的文件或目录)。
这就是项目的分发方式
ProjectFolder
-src
--package.name
---JKS File
---Class calling the resource retrieval and loading into the keystore
Run Code Online (Sandbox Code Playgroud)
我想我已经尝试了几乎所有我能想到的 this.getClass().getClassLoader().getResourceAsStream("jksFile.jks") 组合。
我提前为没有更具体地说明代码而道歉,但我实际上已经忘记了我尝试过什么和没有尝试过什么。
帮助!
提前致谢
我正在努力解决一个对我来说很奇怪的问题.我在StackOverflow上看到了至少五个类似的主题,但它们都没有提供答案.但对于问题本身:我想读取应用程序启动时的文件.当我在IDE(IntelliJ Idea)中运行应用程序时,一切正常.虽然当我使用Gradle Java构建它时会抛出FileNotFoundException:
java.io.FileNotFoundException: file:/home/user/IdeaProjects/time-keeper/build/libs/time-keeper-0.7-beta.jar!/data.csv (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
文件的路径是正确的,文件存在,jar具有适当的权限.声明:
File dataFile = new File(ClassLoader.getSystemResource("data.csv").getFile());
Handle<TimeTask> dataHandle = new FileHandle(dataFile);
Run Code Online (Sandbox Code Playgroud) 我的应用程序加载文件PROJECTNAME/resource夹中的txt文件。
这是我加载文件的方式:
URL url = getClass().getClassLoader().getResource("batTemplate.txt");
java.nio.file.Path resPath;
String bat = "";
try {
resPath = java.nio.file.Paths.get(url.toURI());
bat = new String(java.nio.file.Files.readAllBytes(resPath), "UTF8");
} catch (URISyntaxException | IOException e1) {
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
注意:这在我从Eclipse运行时有效,而在我导出到Jar文件(将所需的库提取到生成的JAR中)时不起作用。我知道该文件正在提取中,因为它在JAR文件中。图像也起作用。
错误MSG:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
Illegal character in path at index 38: file:/C:/DataTransformation/Reports Program/ReportsSetup.jar
at com.sun.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:87)
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:166)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Unknown Source)
at ReportSetup$13.mouseReleased(ReportSetup.java:794)
Run Code Online (Sandbox Code Playgroud)
我在这里也看到了类似的问题,但是它们引用的是JAR文件之外的文件/ URL。
我使用以下代码从类路径读取文件:
Files.readAllBytes(new ClassPathResource("project.txt").getFile().toPath())
Run Code Online (Sandbox Code Playgroud)
project.txt当我处于战争状态时,这工作得很好src/main/resources。现在我重构了代码并将某些代码移至 JAR 中。这个新 JAR 现在包含src/main/resources/project.txt上面的代码。现在我在读取文件时遇到以下异常:
java.io.FileNotFoundException: class path resource [project.txt]
cannot be resolved to absolute file path because it does
not reside in the file system:
jar:file:/usr/local/tomcat/webapps/ROOT/WEB-INF/lib/viewer-1.0.0-SNAPSHOT.jar!/project.txt
Run Code Online (Sandbox Code Playgroud)
我仍在 Tomcat 容器中执行 WAR。
我怎样才能解决这个问题?
我有以下代码:
fun main(args: Array<String>) {
val urlForCSR: URL = ClassLoader.getSystemClassLoader().getResource("merchant.id")
// also tried ClassLoader.getSystemResource("merchant.id")
...
Run Code Online (Sandbox Code Playgroud)
从intelliJ运行时,以下工作正常并找到资源.但是当使用捆绑的jar运行它时会给出一个NullPointerException.
/src/main/resources/merchant.id/src/main/java/Route.kt以下是Maven配置代码段:
...
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>RouteKt</mainClass> <!-- Class generated (for above main func - named Route.kt) -->
</manifest>
</archive>
</configuration>
</plugin>
<!-- Includes the runtime dependencies -->
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)
有没有其他的战争来获取上述资源的URL,这可以使用一个jar或其他方式制作一个胖罐.
Jar内容:
jar tf target/Route-1.0-SNAPSHOT.jar
META-INF/
META-INF/MANIFEST.MF
merchant.id
RouteKt$main$1.class …Run Code Online (Sandbox Code Playgroud) 我已经部署了一个 spring-boot 应用程序 JAR 文件。现在,我想从android上传图像并将其存储在资源目录的myfolder中。但无法获取资源目录的路径。
错误是: java.io.FileNotFoundException:src/main/resources/static/myfolder/myimage.png(没有这样的文件或目录)
这是将文件存储在资源文件夹中的代码
private final String RESOURCE_PATH = "src/main/resources";
String filepath = "/myfolder/";
public String saveFile(byte[] bytes, String filepath, String filename) throws MalformedURLException, IOException {
File file = new File(RESOURCE_PATH + filepath + filename);
OutputStream out = new FileOutputStream(file);
try {
out.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
return file.getName();
Run Code Online (Sandbox Code Playgroud)
}
更新:
这是我尝试过的
private final String RESOURCE_PATH = "config/";
controller class:
String filepath = "myfolder/";
String filename = "newfile.png"
public …Run Code Online (Sandbox Code Playgroud) 每次我运行导出的.jar文件时,包含一个JFrame图像作为其图标,该文件不会运行,除非我提取文件.在编译器中它正在运行.我不想制作一个将资源包和jar文件保存在目录中的启动器.
java ×11
jar ×4
eclipse ×2
file-io ×2
spring ×2
android ×1
build.gradle ×1
classloader ×1
classpath ×1
deployment ×1
export ×1
file ×1
gradle ×1
groovy ×1
image ×1
inputstream ×1
kotlin ×1
loading ×1
onejar ×1
path ×1
properties ×1
resources ×1
rest ×1
scala ×1
spring-boot ×1
swing ×1