weixin_43490664 2019-03-11 17:36 采纳率: 100%
浏览 6330
已采纳

在springboot项目资源目录新建txt文件,来写入数据库查询的数据,本人小白,求详细点代码,谢谢!

将数据库的数据查询,在springboot项目资源目录新建txt文件,写入数据库查询的数据,本人小白,求详细点代码,谢谢!!!!!!!!

  • 写回答

2条回答 默认 最新

  • TinerSky 2019-03-11 17:40
    关注

    数据库查询代码也需要写,还是已经将数据库的内容查询到内存中?

    回复:
    难点一:获取项目的资源路径;
    难点二:文件流的操作;

    package com.qianqiangongzi.utils;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.util.ResourceUtils;
    
    /**
     * 将内存中的数据写入到SpringBoot根目录下面
     * 
     * @author 谦谦公子爱编程
     *
     */
    public class WriteDBToResourceUtils {
    
        public static void main(String[] args) throws IOException {
            List<Student> students = new ArrayList<Student>();
            students.add(new Student("张三", 23, "男"));
            students.add(new Student("夏红", 18, "女"));
            students.add(new Student("李四", 25, "男"));
            //假如 Student学生数据来自数据库,并且已经存放于内存中。
            writeToResource(students);
        }
    
        /**
         * 写到资源文件
         * 
         * @param students
         * @throws IOException
         */
        private static void writeToResource(List<Student> students) throws IOException {
            String basePath = getResourceBasePath();
            String studentResourcePath = new File(basePath, "student/student.txt").getAbsolutePath();
            // 保证目录一定存在
            ensureDirectory(studentResourcePath);
            System.out.println("studentResourcePath = " + studentResourcePath);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(studentResourcePath)));
            for (Student student : students) {
                StringBuffer buffer = new StringBuffer();
                buffer.append(student.getName());
                buffer.append("\t");
                buffer.append(student.getAge());
                buffer.append("\t");
                buffer.append(student.getSex());
                buffer.append("\r\n");
    
                writer.write(buffer.toString());
            }
    
            writer.flush();
            writer.close();
        }
    
        /**
         * 保证拷贝的文件的目录一定要存在
         * 
         * @param filePath
         *            文件路径
         */
        public static void ensureDirectory(String filePath) {
            if (StringUtils.isBlank(filePath)) {
                return;
            }
            filePath = replaceSeparator(filePath);
            if (filePath.indexOf("/") != -1) {
                filePath = filePath.substring(0, filePath.lastIndexOf("/"));
                File file = new File(filePath);
                if (!file.exists()) {
                    file.mkdirs();
                }
            }
        }
    
        /**
         * 将符号“\\”和“\”替换成“/”,有时候便于统一的处理路径的分隔符,避免同一个路径出现两个或三种不同的分隔符
         * 
         * @param str
         * @return
         */
        public static String replaceSeparator(String str) {
            return str.replace("\\", "/").replace("\\\\", "/");
        }
    
        /**
         * 获取项目根路径
         * 
         * @return
         */
        private static String getResourceBasePath() {
            // 获取跟目录
            File path = null;
            try {
                path = new File(ResourceUtils.getURL("classpath:").getPath());
            } catch (FileNotFoundException e) {
                // nothing to do
            }
            if (path == null || !path.exists()) {
                path = new File("");
            }
    
            String pathStr = path.getAbsolutePath();
            // 如果是在eclipse中运行,则和target同级目录,如果是jar部署到服务器,则默认和jar包同级
            pathStr = pathStr.replace("\\target\\classes", "");
    
            return pathStr;
        }
    }
    
    class Student {
        private String name; // 姓名
        private int age; // 年龄
        private String sex;// 性别
    
        public Student() {
        }
    
        public Student(String name, int age, String sex) {
            super();
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
    }
    
    

    图片说明

    图片说明

    编码不易,望采纳!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复