简体   繁体   English

文件路径导致错误

[英]File pathing leading to errors

I'm working on a new program that opens different files. 我正在研究一个打开不同文件的新程序。 The thing is when I change the computer and the files need to be opened they don't because the computer can't find those files. 问题是,当我更换计算机并且文件需要打开时,它们并没有打开,因为计算机找不到这些文件。 Is there a way to make the files reachable even on another computer? 有没有办法使文件甚至在另一台计算机上也可以访问? I mean the files are in a folder inside the project but the path changes when you open it on a new computer. 我的意思是文件位于项目内的文件夹中,但是当您在新计算机上打开文件时,路径会更改。

 File myFile = new File("C:\\Users\\Usuario\\Documents\\NetBeansProjects\\Info12\\src\\info12\\Dokumente\\Aufgaben\\Bubblesort.docx");

In a Maven Project, all yours files are in src/main/resources so: 在Maven项目中,您所有的文件都在src/main/resources因此:

src/main/resources/info12/Dokumente/Aufgaben/Bubblesort.docx src / main / resources / info12 / Dokumente / Aufgaben / Bubblesort.docx

Your java code is: 您的Java代码是:

File myFile = new File("src\\main\\resources\\info12\\Dokumente\\Aufgaben\\Bubblesort.docx")

and more smart: 更聪明:

File myFile = new File("src" + File.separator + "main" + File.separator + "resources" + File.separator + "info12" + File.separator + "Dokumente" + File.separator + "Aufgaben" + File.separator + "Bubblesort.docx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM