Java:如何创建&;将文件写入maven目标目录

Java:如何创建&;将文件写入maven目标目录,java,file-io,path,Java,File Io,Path,目标: 正在尝试创建文本文件,并将其写入maven项目中的目标文件夹 方法: public synchronized void writeToFile(List<String> list, String file) { String path = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath(); try { Path out

目标: 正在尝试创建文本文件,并将其写入maven项目中的目标文件夹

方法:

public synchronized void writeToFile(List<String> list, String file) {
        String path = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

        try {
            Path out = Paths.get( path + file);
            Files.write(out, list, Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public synchronized void writeToFile(列表,字符串文件){
字符串路径=MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
试一试{
Path out=Path.get(Path+file);
write(out,list,Charset.defaultCharset());
}捕获(IOE异常){
e、 printStackTrace();
}
}
问题:

  • java.nio.file.InvalidPathException错误

  • 应用程序将部署在不同的系统上,因此路径可能会更改

  • 我不知道如何在不检索此错误的情况下返回此案例的目标路径

错误:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: /C:/Users/jquinn/IdeaProjects/Exercise1/Word%20Scraper%20-%20Java/target/classes/exclusions.txt
    at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
    at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
    at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
    at java.nio.file.Paths.get(Paths.java:84)
    at FileManager.writeToFile(FileManager.java:58)
    at Driver.main(Driver.java:21)
线程“main”java.nio.file.InvalidPathException中的异常:索引2处的非法字符:/C:/Users/jquinn/IdeaProjects/Exercise1/Word%20Scraper%20-%20Java/target/classes/excludes.txt 位于sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) 位于sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) 位于sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) 位于sun.nio.fs.WindowsPath.parse(WindowsPath.java:94) 位于sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255) 位于java.nio.file.Paths.get(path.java:84) 在FileManager.writeToFile(FileManager.java:58) 位于Driver.main(Driver.java:21) 只要用这个

字符串pathTemp=System.getProperty(“user.dir”)//它将给出程序运行的根目录

串 字符串pathTemp=pathTemp+“/target/”+文件名//这里是参数中的文件名

Path Path=Path.get(pathTemp)

write(路径、列表、Charset.defaultCharset())

就用这个吧

字符串pathTemp=System.getProperty(“user.dir”)//它将给出程序运行的根目录

串 字符串pathTemp=pathTemp+“/target/”+文件名//这里是参数中的文件名

Path Path=Path.get(pathTemp)


write(路径、列表、Charset.defaultCharset())

第一个
/
导致字符串出现问题。你必须得到完整的字符串,没有这个

我与我的班级一起努力实现这一目标:

String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
path = path.substring(1, path.length()) + "file.txt";
System.out.println(path);
Path out = Paths.get(path);
System.out.println(out.isAbsolute());
输出:

F:/software/workspace/file.txt
true

第一个
/
导致字符串出现问题。你必须得到完整的字符串,没有这个

我与我的班级一起努力实现这一目标:

String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
path = path.substring(1, path.length()) + "file.txt";
System.out.println(path);
Path out = Paths.get(path);
System.out.println(out.isAbsolute());
输出:

F:/software/workspace/file.txt
true

@Josh Quinn你能将其标记为验证答案吗?@Josh Quinn你能将其标记为验证答案吗?