1. Read the file content and manipulate the file
package com.test.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
@Slf4j
public class WriteFile {
/**
* 向文件追加内容
*
* @param content 写入的内容
* @param fileName 文件
*/
public static void writeFile(String content, String fileName) {
// 在文件夹目录下新建文件
File file = new File(fileName);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
if (!file.exists()) {
boolean hasFile = file.createNewFile();
if (hasFile) {
log.info("file not exists, create new file");
}
fos = new FileOutputStream(file);
} else {
fos = new FileOutputStream(file, true);
}
osw = new OutputStreamWriter(fos, "utf-8");
// 写入内容
osw.write(content);
// 换行
osw.write("\r\n");
log.info("成功向文件 [{}] 写入内容:[{}]", fileName, content);
} catch (Exception e) {
log.info("写入文件发生异常", e);
} finally {
// 关闭流
try {
if (osw != null) {
osw.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
log.info("关闭流异常", e);
}
}
}
// 将内容 "testFlag_20210112=1" 追加到文件 "D:\\testConfig.properties"
public static void main(String[] args) {
String content = "testFlag_20210112=1";
String filePath = "D:\\testConfig.properties";
writeFile(content, filePath);
}
}
Read and write each line of the file
import java.io.*;
public class TransStreamTest {
/**
* 读写文件
*@author:cyz
*/
public static void main(String[] args) throws IOException {
//对文件进行读获取
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\41639\\Desktop\\java\\temp\\test1031.txt"));
String line =null;
//对文件进行读取每一行
while ((line=br.readLine())!=null) {
if ("over".contentEquals(line)) {
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
}
PS: When the readLine() method reads a line, it will only return the read result when it encounters a carriage return (\r) or a newline (\n). This is the meaning of "reading a line". Interested students View readLine() source code
2. Upload and download files in a streaming manner
//将文件以流的方式上传到数据库中,FileCopyUtils为spring框架中的访问静态资源的利器。
//嵌入代码,实体中定义接受流的类型为byte[]
public static void main(String[] args) {
String path="文件路劲";
File file=new File(path);
String name="文件名";
try {
InputStream fis=new FileInputStream(path+name);
byte[] bytes=FileCopyUtils.copyToByteArray(fis);
driver.setDriver_io(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//从数据库中下载流文件,首先获取路劲地址,获取文件名; 嵌套代码
byte[] driver_io=实体.getDriver_io();
String fileName="文件名";
String parentPath="下载的文件路劲";
binToFile(driver_io, fileName, parentPath);
}
public static File binToFile(byte[] driver_io, String fileName,String parentPath) {
try {
File fout=new File(parentPath,fileName);
fout.createNewFile();
FileCopyUtils.copy(driver_vo,fout);
FileOutputStream outs=new FileOutputStream(fout);
outs.write(driver_io);
outs.flush();
outs.close();
return fout;
} catch (Exception ex) {
throw new RuntimeException("transform bin into file 出错",ex);
}
/**
*数据库中获取的流直接响应到页面
*/
@RequestMapping(value="/fileread")
@ResponseBody
public void FileRead(int id,HttpServletResponse response) {
FileOutputStream fos=null;
UploadEntity entity =uploadService.findIO(id);
byte[] driver_io=entity.getUpl_All();
/* String path="D:/2.pdf"; */
try {
/*将数据库中获取流写入到本地路劲下
* fos=new FileOutputStream(path);
* fos.write(driver_io);
*/
response.getOutputStream().write(driver_io);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos!=null) {
try {
fos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 读取文件内容进行追加显示于页面
* @return
*/
public String fileReadLine() {
String path="D:/";
String name="123.txt";
StringBuffer sbf = new StringBuffer();
File file=new File(path+name);
try {
BufferedReader reader=new BufferedReader(new FileReader(file));
String templine;
while((templine=reader.readLine())!=null) {
sbf.append(templine+"<br>");
}
reader.close();
return sbf.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sbf.toString();
}
}
MySQL uses longblob type to store byte stream, and varbinary (max) is used to store byte stream in SQL server