导入导出

一、加载文件

  1. FileInputStream & InputStream 区别:

    InputStream 不可以读取文件,它是一个 Abstract 的类,不能实例化,是所有输入流的基类;

    FileInputStream 是 InputStream 的一个实现类,用于读取诸如图像数据之类的原始字节流。

  2. FileInputStream & FileReader & BufferedReader 区别:

      

// FileInputStream



File file = new File(path); FileInputStream fis= new FileInputStream(file);

while((i=fis.read()) != -1){ System.out.println(i); }
// InputStreamReader
File file = new File(path);
FileInputStream fis= new FileInputStream(file);

InputStreamReader isr=new InputStreamReader(fis,"utf8");
while((i=isr.read()) != -1){ System.out.println((char)i); }
// BufferedReader
File file = new File(path);
FileInputStream fis= new FileInputStream(file);

InputStreamReader isr=new InputStreamReader(fis,"utf8");   
BufferedReader br=new BufferedReader(isr); String line; while((line=br.readLine()) != null){ System.out.println(line); }

二、下载

  ① 下载到指定目录

File file = new File(path);  
FileOutputStream out = new FileOutputStream(file);  

doc.write(out);  
out.close();  

  ② 直接下载

File file = new File(path);
response.setHeader("Content-Disposition", "attachment; filename="+new String(output.getBytes("gb2312"), "iso-8859-1"));

OutputStream out= response.getOutputStream();
doc.write(out);

三、输出方式(注:下面两种方式是互斥的,不能两种同时使用)

  1. 通过 PrintWriter

// 定义页面输出流
PrintWriter out = response.getWriter();
out.print("hello world!");
out.flush();// 刷新,把缓存区的东东发出去
out.close();// 关闭页面输出流

  2. 通过 OutputStream

response.setHeader("Content-Disposition", "attachment; filename="+new String(output.getBytes("gb2312"), "iso-8859-1"));
OutputStream outputStream = response.getOutputStream();
outputStream.write("hello world!".getBytes());
outputStream.flush();
outputStream.close();

此篇为学习笔记:

http://blog.csdn.net/zndxlxm/article/details/7405088

http://blog.csdn.net/moxie008/article/details/5663488

posted @ 2017-05-19 16:14  ☀Beans✡✌  阅读(189)  评论(0编辑  收藏  举报