我正在尝试将序列化的目标文件写入我的类路径,该文件稍后将用于读取目的。我已经编写了以下代码,
FileOutputStream fos = new FileOutputStream("src/main/resources/ListOfIotcontentObjects");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(fileList);
因此,在使用mvn clean package
构建项目时,它会毫无例外地创建可执行文件.jar,但是当我尝试运行.jar时,它的意思是(The system cannot find the path specified)
我不明白这里出了什么问题以及如何使它工作?预先感谢。
编辑1 :要在资源文件夹中写入/创建文件,我尝试了以下代码,该代码抛出NullPointerException
。
ArrayList<DevicesPOJO> employees = new ArrayList<>();
employees.add(new DevicesPOJO(100, 1001, "Shaik", "Nasir", 12233456));
employees.add(new DevicesPOJO(101, 1002, "Shaik", "Nasir", 12233654));
employees.add(new DevicesPOJO(102, 1003, "Shaik", "Nasir", 12233465));
ClassLoader classLoader = TestApplication.class.getClass().getClassLoader();
File file = new File(classLoader.getResource("./ListOfIotcontentObjects").getFile());
System.out.println(file.getPath());
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(employees);
System.out.println("Success...");
有人可以帮我解决这个例外吗?