Java IO流读取文件(一)

、使用字符流,读取和存储纯文本文件。

       存储文件,也就是像一个文件里写内容,既然是写,那就需要使用输出流。而且我们写的是纯文本文件,所以这里使用字符流来操作,Java api提供给我们FileWriter这么一个类,我们来试试:(读取文件同理使用FileReader类)

       

[java]  view plain copy
  1. package org.example.io;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8.   
  9. public class TestFileWriter {  
  10.   
  11.     public static void main(String[] args) throws Exception {  
  12.         writeToFile();  
  13.         readFromFile();  
  14.     }  
  15.   
  16.     /** 
  17.      * DOC 从文件里读取数据. 
  18.      *  
  19.      * @throws FileNotFoundException 
  20.      * @throws IOException 
  21.      */  
  22.     private static void readFromFile() throws FileNotFoundException, IOException {  
  23.         File file = new File("E:\\helloworld.txt");// 指定要读取的文件  
  24.         FileReader reader = new FileReader(file);// 获取该文件的输入流  
  25.         char[] bb = new char[1024];// 用来保存每次读取到的字符  
  26.         String str = "";// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好  
  27.         int n;// 每次读取到的字符长度  
  28.         while ((n = reader.read(bb)) != -1) {  
  29.             str += new String(bb, 0, n);  
  30.         }  
  31.         reader.close();// 关闭输入流,释放连接  
  32.         System.out.println(str);  
  33.     }  
  34.   
  35.     /** 
  36.      * DOC 往文件里写入数据. 
  37.      *  
  38.      * @throws IOException 
  39.      */  
  40.     private static void writeToFile() throws IOException {  
  41.         String writerContent = "hello world,你好世界";// 要写入的文本  
  42.         File file = new File("E:\\helloworld.txt");// 要写入的文本文件  
  43.         if (!file.exists()) {// 如果文件不存在,则创建该文件  
  44.             file.createNewFile();  
  45.         }  
  46.         FileWriter writer = new FileWriter(file);// 获取该文件的输出流  
  47.         writer.write(writerContent);// 写内容  
  48.         writer.flush();// 清空缓冲区,立即将输出流里的内容写到文件里  
  49.         writer.close();// 关闭输出流,施放资源  
  50.     }  
  51.   
  52. }  

测试结果:

hello world,你好世界


二、使用字节流,读取和存储图片

    首先使用输入流读取图片信息,然后通过输出流写入图片信息:

[java]  view plain copy
  1. package org.example.io;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6.   
  7. public class TestIOStream {  
  8.   
  9.     /** 
  10.      *  
  11.      * DOC 将F盘下的test.jpg文件,读取后,再存到E盘下面. 
  12.      *  
  13.      * @param args 
  14.      * @throws Exception 
  15.      */  
  16.     public static void main(String[] args) throws Exception {  
  17.         FileInputStream in = new FileInputStream(new File("F:\\test.jpg"));// 指定要读取的图片  
  18.         File file = new File("E:\\test.jpg");  
  19.         if (!file.exists()) {// 如果文件不存在,则创建该文件  
  20.             file.createNewFile();  
  21.         }  
  22.         FileOutputStream out = new FileOutputStream(new File("E:\\test.jpg"));// 指定要写入的图片  
  23.         int n = 0;// 每次读取的字节长度  
  24.         byte[] bb = new byte[1024];// 存储每次读取的内容  
  25.         while ((n = in.read(bb)) != -1) {  
  26.             out.write(bb, 0, n);// 将读取的内容,写入到输出流当中  
  27.         }  
  28.         out.close();// 关闭输入输出流  
  29.         in.close();  
  30.     }  
  31.   
  32. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值