使用字节流读取文件与生成文件

编程入门 行业动态 更新时间:2024-10-10 15:29:17

使用字节流读取<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件与生成文件"/>

使用字节流读取文件与生成文件

数据源:

[{"id": 14,"bookName": "Java EE企业级应用开发教程","author": "黑马程序员","description": "本书详细讲解了JavaEE中Spring、SpringMvc和Mybatis三大框架(以下简称“SSM”)的基本知识和应用。","press": "人民邮电出版社","createDate": "2021-05-15T13:51:17.000+00:00","inventory": 500,"typeId": 6,"bookCode": "a3f6199d9c55461c89f8b2e4d56b095e","picturePath": "images/425eff82-a567-4d2e-bd12-9367f782b111_5b7bb2a3N3b8bcc37.jpg","price": 49.8,"salesNumber": 0,"saleState": 0},{"id": 15,"bookName": "运动训练基础理论","author": "佚名","description": "运动基础理论","press": "人民邮电出版社","createDate": "2021-05-15T13:55:57.000+00:00","inventory": 450,"typeId": 1,"bookCode": "5cfc7ce4ec3d443fad5604427e1b990d","picturePath": "images/22255587-7e69-45fc-acf2-20e64e7f323d_yundongjichu.jpg","price": 39.9,"salesNumber": 45,"saleState": 0},{"id": 16,"bookName": "费曼学习法","author": "佚名","description": "费曼学习法","press": "人民邮电出版社","createDate": "2021-05-15T15:05:44.000+00:00","inventory": 1200,"typeId": 2,"bookCode": "d9d0cb3818874a56adc2e879ceaefe2b","picturePath": "images/11deac41-316b-4e6d-87da-a5a051d973c7_41v3KZf+bhL._AC_SX184_.jpg","price": 12.0,"salesNumber": 12,"saleState": 1},{"id": 17,"bookName": "test02","author": "佚名","description": "test02","press": "人民邮电出版社","createDate": "2021-05-15T15:06:11.000+00:00","inventory": 450,"typeId": 6,"bookCode": "afc5978235734f9192f3b52e6794870a","picturePath": "images/6b77fb3f-3d4f-4dad-ab77-c4c14d0a350b_29233886-1_l_2002.jpg","price": 69.0,"salesNumber": 23,"saleState": 1},{"id": 19,"bookName": "考研公共日语203","author": "褚进","description": "没有简介就是最好的简介","press": "北京理工大学出版社","createDate": "2021-05-15T19:30:07.000+00:00","inventory": 120,"typeId": 1,"bookCode": "10a12dd1cece4bed894d85866152a246","picturePath": "images/00101736-9141-4045-92e8-c206680b80e7_clear218.jpg","price": 39.5,"salesNumber": 450,"saleState": 1},{"id": 20,"bookName": "跑步圣经","author": "佚名","description": "跑步圣经,全方位提高跑步实力。","press": "中国工信出版社","createDate": "2021-05-22T08:48:13.000+00:00","inventory": 120,"typeId": 2,"bookCode": "71079c0187fc4a4cbb5f4cd4d8e2eeed","picturePath": "images/f2d6aa56-9d40-4d3d-a777-f9ca07f0f751_d0d0930851a4a728.jpg","price": 45.0,"salesNumber": 0,"saleState": 1},{"id": 21,"bookName": "运动揭破学","author": "佚名","description": "运动类基础课程","press": "北京理工大学出版社","createDate": "2021-05-22T09:00:08.000+00:00","inventory": 12,"typeId": 7,"bookCode": "4bef98dc0f164c809bdab79ae4a10972","picturePath": "images/13a82c8d-3be1-4de0-b7a9-2ccd41d64387_5450bc0aNce91b51d.jpg","price": 42.9,"salesNumber": 0,"saleState": 1},{"id": 22,"bookName": "狼人杀","author": "佚名","description": "从入门到封神,从入门到入土","press": "人民邮电出版社","createDate": "2021-05-22T09:02:08.000+00:00","inventory": 450,"typeId": 1,"bookCode": "9b1706584b3f42a290a41976cf920180","picturePath": "images/5002caa9-4e76-466e-b090-cee709653ac4_79366d2be5c914e4.jpg","price": 29.0,"salesNumber": 0,"saleState": 1},{"id": 23,"bookName": "人体运动学","author": "佚名","description": "高等院校指定教材","press": "人民邮电出版社","createDate": "2021-05-22T09:07:47.000+00:00","inventory": 1500,"typeId": 1,"bookCode": "8d04a50c53bd4581a93876e34d3bbe71","picturePath": "images/1f25a364-b896-446b-b3b0-2759b16ab836_973beaf3c1856e7b.jpg","price": 47.0,"salesNumber": 0,"saleState": 1},{"id": 24,"bookName": "动画运动规律","author": "佚名","description": "高等院校指定教材","press": "中国工信出版社","createDate": "2021-05-22T09:10:03.000+00:00","inventory": 1200,"typeId": 1,"bookCode": "e0beedc21c394d01927592e2f0909b81","picturePath": "images/56a5d2f3-1b6a-4f24-84e3-92f54beab958_rBEhWFKgTxkIAAAAAAKcpRDTmdsAAGYvgKCSyEAApy9637.jpg","price": 12.0,"salesNumber": 0,"saleState": 1},{"id": 25,"bookName": "最后测试0529 0711","author": "佚名","description": "最后的测试","press": "北京理工大学出版社","createDate": "2021-05-28T23:05:15.000+00:00","inventory": 12,"typeId": 8,"bookCode": "94fa95fca71e4a3088ff1543c9ab8444","picturePath": "images/089fda76-5480-49a6-a196-6b7ee47a0456_413RwxijiyL._AC_SX184_.jpg","price": 56.0,"salesNumber": 0,"saleState": 1}
]

mapper接口:

package com.itheima.mapper;import com.itheima.entity.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface BookMapper {@Select({"select id,bookName,author,description,press,createDate,inventory,typeId,bookCode,picturePath,price,salesNumber,saleState"+" from book_tb"})List<Book> selectAllBook();
}

控制器:

package com.itheima.controller;import com.itheima.entity.Book;
import com.itheima.mapper.BookMapper;
import com.itheima.test.JdbcConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.sql.DataSource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;@RestController
public class HelloController {@Autowiredprivate DataSource dataSource;@Autowiredprivate Book book;@Autowiredprivate JdbcConfig jdbcConfig;@Autowiredprivate BookMapper bookMapper;@GetMapping("hello")public List<Book> hello(){//将从数据库中查询的数据做成一个文件/** System.out.println("dataSource = "+dataSource);System.out.println(jdbcConfig);System.out.println(bookMapper.selectAllBook());*/List<Book> bookList = bookMapper.selectAllBook();FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream("E:\\temp.txt");/** ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);for (Book book:bookList) {objectOutputStream.writeObject(book);}*/for (Book book:bookList) {fileOutputStream.write(String.valueOf(book.getId()).getBytes());System.out.println(String.valueOf(book.getId()).getBytes());fileOutputStream.write("\t".getBytes());fileOutputStream.write(book.getBookName().getBytes());fileOutputStream.write("\t".getBytes());fileOutputStream.write(book.getAuthor().getBytes());fileOutputStream.write("\r\n".getBytes());}} catch (IOException ioException) {ioException.printStackTrace();}finally {try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}//从作成的文件中读取数据到实体pojo类中FileInputStream fileInputStream = null;try {fileInputStream = new FileInputStream("E:\\temp.txt");int len = 0;
/**           while ((len=fileInputStream.read())!=-1){System.out.println(len);}*/byte[] bytes = new byte[1024];while ((len=fileInputStream.read(bytes))!=-1){//System.out.println(new String(bytes,0,len));String pojoTemp = new String(bytes, 0, len);String[] split = pojoTemp.split("/t");
/**                System.out.println("-----------以换行符分割----------");for (int i = 0; i < split.length; i++) {System.out.println(split[i]);}*/System.out.println(split[0]);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}return bookList;}
}

结果:

1.生成txt文件

配置文件:

server:port: 80servlet:session:timeout: 30tomcat:uri-encoding: utf-8
age: 19
name: zhaodabao
personinfo:name: zhaoxiaobaoage: 3jdbc:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/online_book?serverTimezone=UTCusername: lekpassword: qq+835897980ww#日志记录级别
logging:level:com.itheima: debugorg.springframework: info
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/online_book?serverTimezone=UTCusername: lekpassword: qq+835897980ww
mybatis:#实体文件别名type-aliases-package: com.itheima.entityconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl#映射文件路径#mapper-locations: classpath:mapper/*.xmlbook:id: 2bookName:  springBoot企业级开发教程author: heimadescription: 这是一段描述press: 人民邮电出版社createDate: 2021/07/12

 实体类:

package com.itheima.entity;import jdk.jfr.DataAmount;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.intellij.lang.annotations.Pattern;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;import java.util.Date;@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@Validated
@ConfigurationProperties(prefix = "book")
public class Book implements java.io.Serializable{private int id;private String bookName;private String author;private String description;private String press;private Date createDate;private int inventory;private int typeId;private String bookCode;private String picturePath;private double price;private int salesNumber;private int saleState;
}

更多推荐

使用字节流读取文件与生成文件

本文发布于:2024-02-17 17:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1694872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   字节

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!