关于字节流,字符流,缓冲流对文件的读入和写出的基本操作。
/**
* 字节流的读入,写出
* FileInputStream
* FileOutputStream
*
* @throws IOException
*/
@Test
public void test01() throws IOException {
String path = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo01.txt";
String path2 = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo02.txt";
File file = new File(path);
if (file.exists()) {
FileInputStream stream = new FileInputStream(file);
//stream.read()读取字节的二进制,返回十进制的整数
byte[] bytes = new byte[1024];
int by = 0;
FileOutputStream outputStream = new FileOutputStream(path2, true);//true为追加
while ((by = stream.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, by));
outputStream.write(bytes, 0, by);//写入文件
String sh = "哈哈哈---新写入的文件";
outputStream.write(sh.getBytes());
}
outputStream.close();
stream.close();//关闭资源
}
}
/**
* FileReader
* FileWriter
* 字符流的读入写出
* 和单纯的字节流的读入写出,大同小异
*/
@Test
public void test02() throws IOException {
String path = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo01.txt";
String path2 = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo02.txt";
Reader reader = new FileReader(path);
Writer writer = new FileWriter(path2, true);//自动添加
char[] chars = new char[1024];
int ch = 0;
while ((ch = reader.read(chars)) != -1) {
System.out.println(new String(chars, 0, ch));//读出文件。
writer.write(chars, 0, ch);//写出文件
}
writer.close();//关闭资源
reader.close();//关闭资源
}
/**
* BufferedInputStream
* BufferedOutputStream
* 字节流的读入和写出
*
* @throws IOException
*/
@Test
public void test03() throws IOException {
String path = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo01.txt";
String path2 = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo02.txt";
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(path));//字节缓冲读入流
BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(path2, true));//字节缓冲写出流
byte[] bytes = new byte[1024];
int b = 0;
while ((b = bi.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, b));
bo.write(bytes, 0, b);//字节缓冲流写出。------
bo.write(new String(bytes, 0, b).getBytes());//----
}
//刚才没写就导致了写出的内容为空。
bo.flush();//有可能缓冲区没满,强制写出
bo.close();
bi.close();
}
/**
* BufferedReader
* BufferedWriter
* 关于字符缓冲流的输入和输出。
*
* @throws IOException
*/
@Test
public void test04() throws IOException {
String path = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo01.txt";
String path2 = "C:\\Users\\lhl\\Desktop\\myProject\\ssmDemo\\io\\Demo02.txt";
/**
* 读取一行数据,不会有换行符,需要手动添加换行符
*/
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));//字符读入缓冲流
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path2, true));
String input = "";
while ((input = bufferedReader.readLine()) != null) {
System.out.print(input);
System.out.println("\n");//这样子读出有了换行。
bufferedWriter.write(input);
bufferedWriter.write("\n");//这样子读出就有了换行
}
bufferedWriter.flush();//刷新缓冲区
bufferedWriter.close();
bufferedReader.close();
}
主要应用:下载和预览
其中下载和预览的主要区别是:
下载有response的响应头设置:
response.setContentType("application/force-download");// 设置强制下载不打开
//设置相应头
response.addHeader("Content-Disposition", "attachment;fileName=" + file.getName());
要特别注意:这里的OutputStream是response获取的,直接new是不行的。
}
/**
* 展示文件,主要是先读取,再输出
* @param path
* @param response
*/
@RequestMapping(value = "/showFile")
@ResponseBody
public void showFile(
@RequestParam(name = "path",required = false) String path, HttpServletResponse response,
HttpServletRequest request) {
System.out.println(path);
//header中的这个*指所有域名,参数指的是域名。。可以指定允许特定的域名
response.setHeader("Access-Control-Allow-Origin", "*");
File file = new File(path);
System.out.println("file"+file.exists());
System.out.println("----------");
if (file.exists()) {
System.out.println("文件存在");
response.setContentType("application/pdf");
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
byte[] buffer = new byte[fis.available()];
bis = new BufferedInputStream(fis);
response.reset(); // 非常重要
OutputStream outputStream = response.getOutputStream();
int i=0;
while ( (i=bis.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
bis.close();
outputStream.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else {
System.out.println("文件不存在---------------------------");
}
}
/**
* 下载文件
* @param path
* @param response
*/
@RequestMapping(value = "/downLoadFile")
public void downLoadFile(@RequestParam(name = "path") String path, HttpServletResponse response){
File file = new File(path);
if (file.exists()) {
byte[] buffer = new byte[2048];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
response.setContentType("application/force-download");// 设置强制下载不打开
//设置相应头
response.addHeader("Content-Disposition", "attachment;fileName=" + file.getName());
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream outputStream = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
outputStream.write(buffer, 0, i);
i = bis.read(buffer);
}
bis.close();
outputStream.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
无偿免费分享源码以及技术和面试文档,更多优秀精致的源码技术栈分享请关注微信公众号:gh_817962068649