java - 当我写入内容时,csv 文件为空

标签 java spring

我正在尝试写入一个 csv 文件。执行下面的代码后,csv 文件仍然是空的。 文件位于文件夹 .../webapp/resources/ 中。

这是我的 dao 类:

public class UserDaoImpl implements UserDao {

private Resource cvsFile;

public void setCvsFile(Resource cvsFile) {
    this.cvsFile = cvsFile;
}

@Override
public void createUser(User user) {

    String userPropertiesAsString = user.getId() + "," + user.getName()
            + "," + user.getSurname() +"\n";;

    System.out.println(cvsFile.getFilename());

    FileWriter outputStream = null;
    try {
        outputStream = new FileWriter(cvsFile.getFile());
        outputStream.append(userPropertiesAsString);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public List<User> getAll() {
    return null;
}
}

这是 beans.xml 的一部分。

<bean id="userDao" class="pl.project.dao.UserDaoImpl"
    p:cvsFile="/resources/users.cvs"/>

程序编译并且没有抛出任何异常,但是 CSV 文件是空的。

最佳答案

如果您在 IDE 中运行您的应用程序,用于运行应用程序的 /webapp/resources 将不同于您的 IDE 中的 /webapp/resources。尝试记录文件的完整路径并在那里检查。

关于java - 当我写入内容时,csv 文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501843/

相关文章:

java.util.Objects.isNull vs object == null

java - PSQLException : ResultSet not positioned properly, 也许你需要接下来调用

java - 2D 数组通过从 0 到数组大小的位置编号获取行和列

java - Grails - 为什么需要 withTransaction?

javascript - 使用 html+css+javascript 或 JSP 作为我的表示层

java - Spring - 带有 AbstractWizardFormController 的多个命令类

java - 浮点乘法错误并在 GUI 中显示

java - Spring简介页面的困境

java - 如何将 swagger-ui.html 设置为默认上下文路径

java - 在方法参数中初始化数组