• java+++IO流操作


    序:IO流的操作主要分为两种读和写。一方面:我们可以通过不加缓冲类字符流BufferedReader/Writer和字节流BufferedInputStream/OutputStream来进行简单的读写,当然加了缓冲类其效率更高。另一方面:当我们需要进行流转换时,主要为了解决读取乱码问题,且字节流能够进行编码指定,因此需要字节流到字符流的转换。

    1.字符流的读写+不加BufferedReader/Writer

    public class CharIOTest {
     	
     	/** 
          * 流转换读取数据.
     	 * @throws FileNotFoundException 
     	 * @throws IOException 
          * @throws IOException 
          */ 
     	@Test
         public void readChangeTest() throws IOException  {  
             File file = new File("C:\Documents and Settings\Administrator\桌面\1.txt");// 指定要读取的文件
             FileInputStream fis = new FileInputStream(file);// 获取该文件的输入流 
             InputStreamReader isr=new InputStreamReader(fis, "gbk");
             char[] bb = new char[1024];// 用来保存每次读取到的字符  
             StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好  
             int n;// 每次读取到的字符长度  
             while ((n = isr.read(bb)) != -1) { 
                 sb.append(new String(bb, 0, n));  
             }  
             isr.close();// 关闭输入流,释放连接  
             System.out.println(sb);  
         }  
     	
     	/**
     	 * 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。
     	 * 好处:代码少写几行,但不通用。
     	 * @throws IOException
     	 */
     	@Test
         public void readTest() throws IOException  {  
             File file = new File("C:\Documents and Settings\Administrator\桌面\2.txt");// 指定要读取的文件
             
             FileReader fr=new FileReader(file);
     //        System.out.println(fr.getEncoding());
             char[] bb = new char[1024];// 用来保存每次读取到的字符  
             StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好  
             int n;// 每次读取到的字符长度  
             while ((n = fr.read(bb)) != -1) { 
                 sb.append(new String(bb, 0, n));  
             }  
             fr.close();// 关闭输入流,释放连接  
             System.out.println(sb);  
         }
     	
     	 /** 
          * 往文件里写入数据. 
          * @throws IOException 
          */
     	@Test
         public void writeTest() throws IOException {  
             String writerContent = "hello world,你好世界";// 要写入的文本  
             File file = new File("C:\Documents and Settings\Administrator\桌面\2.txt");// 要写入的文本文件  
             if (!file.exists()) {// 如果文件不存在,则创建该文件  
                 file.createNewFile();  
             }  
             
             FileWriter writer = new FileWriter(file);// 获取该文件的输出流
             System.out.println(writer.getEncoding());
             // 写内容  ,默认使用writer.getEncoding()进行写入,不会出现乱码,因为得到的文件的编码便是写入的编码
             writer.write(writerContent);
             writer.flush();// 清空缓冲区,立即将输出流里的内容写到文件里  
             writer.close();// 关闭输出流,施放资源  
         } 
     }

    2.字节流的读写+不加BufferedInputStream/OutputStream

    public class ByteIOTest {
     	/** 
          *  
          * 将D盘下的D:\xxx.ico文件,读取后,再存到E盘下面. 
          *  
          * @param args 
          * @throws Exception 
          */  
     	@Test
         public void imageIOTest(String[] args) throws Exception {  
             FileInputStream in = new FileInputStream(new File("D:\xxx.ico"));// 指定要读取的图片  
             File file = new File("E:\test.jpg");  
             if (!file.exists()) {// 如果文件不存在,则创建该文件  
                 file.createNewFile();  
             }  
             FileOutputStream out = new FileOutputStream(new File("E:\yuanwang.ico"));// 指定要写入的图片  
             int n = 0;// 每次读取的字节长度  
             byte[] bb = new byte[1024];// 存储每次读取的内容  
             while ((n = in.read(bb)) != -1) {  
                 out.write(bb, 0, n);// 将读取的内容,写入到输出流当中  
             }  
             out.close();// 关闭输入输出流  
             in.close();  
         }  
     }

    3.字符流的读写+加BufferedReader/Writer

    public class BufferCharIOTest {
     	/** 
          * 考虑到编码问题-转换流读取,即字节流->字符流
         	  windows系统下默认编码为gbk,即所读取文本为gbk编码。
                                因此如果myeclipse的默认是utf-8编码的话则读取中文时会出现乱码
                                情况,utf8 中文会默认3个字节,而gbk 中文默认2个字节。
                              一个字符在不同编码的情况下,其所对应的字节数是不相同,字符有中文字符,英文字符,数字字符等等 
          * @throws FileNotFoundException 
          * @throws IOException 
          */
     	@Test
         public void readChangeTest() throws FileNotFoundException, IOException {  
             File file = new File("C:\Documents and Settings\Administrator\桌面\1.txt");// 指定要读取的文件  
             // 获得该文件的缓冲输入流  
             //BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
             
             BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
             InputStreamReader isr=new InputStreamReader(bis, "gbk");
             
             char[] bb = new char[1024];// 用来保存每次读取到的字符  
             StringBuffer sb = new StringBuffer();// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好  
             int n;// 每次读取到的字符长度  
             while ((n = isr.read(bb)) != -1) { 
                 sb.append(new String(bb, 0, n));  
             }  
             isr.close();// 关闭输入流,释放连接  
             System.out.println(sb);  
         }  
     	
     	
     	/**
     	 * 没有进行字符编码的指定,因此只适合程序生成的文本文件的读取。
     	 * 好处:代码少写几行,但不通用。
     	 * @throws IOException
     	 */
     	@Test
         public void readTest() throws FileNotFoundException, IOException {  
     		File file = new File("C:\Documents and Settings\Administrator\桌面\1.txt");// 指定要读取的文件  
             // 获得该文件的缓冲输入流  
             BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
             String line = "";// 用来保存每次读取一行的内容  
             while ((line = bufferedReader.readLine()) != null) {  
                 System.out.println(line);  
             }  
             bufferedReader.close();// 关闭输入流
         }  
       
         /** 
          * 在写入过程中,不需要考虑编码问题,
          * 因为生成的文件文本的编码会与写入编码一致。因此不会出现乱码情况
          *  
          * @throws IOException 
          */
     	@Test
         public void writeTest() throws IOException {  
             File file = new File("C:\Documents and Settings\Administrator\桌面\2.txt");// 指定要写入的文件  
             if (!file.exists()) {// 如果文件不存在则创建  
                 file.createNewFile();  
             }  
             // 获取该文件的缓冲输出流  
             BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));  
             // 写入信息  
             bufferedWriter.write("你好世界");  
             bufferedWriter.newLine();// 表示换行  
             bufferedWriter.write("hello world");  
             bufferedWriter.flush();// 清空缓冲区  
             bufferedWriter.close();// 关闭输出流  
         }  
     }

    4.字节流的读写+加BufferedInputStream/OutputStream

    public class BufferByteIOTest {
     	public void imageIOTest(String[] args) throws Exception {  
             // 指定要读取文件的缓冲输入字节流  
             BufferedInputStream in = new BufferedInputStream(new FileInputStream("F:\xx.jpg"));  
             File file = new File("E:\xx.jpg");  
             if (file != null) {  
                 file.createNewFile();  
             }  
             // 指定要写入文件的缓冲输出字节流  
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));  
             byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组  
             int n;// 每次读取到的字节数组的长度  
             while ((n = in.read(bb)) != -1) {  
                 out.write(bb, 0, n);// 写入到输出流  
             }  
             out.close();// 关闭流  
             in.close();  
         }  
     }


  • 相关阅读:
    20200305 VMware虚拟机安装及centOS
    20200303 pandas
    20200302 数据分析之numpy以及Jupyter
    Bash(Terminal)高频命令
    E117: Unkown function: vundle#installer#new
    字符串输入之%s
    结合getchar()理解缓冲区
    在HEXO主题中添加站内搜索
    字符串的全排列
    无法启动程序,系统找不到指定的文件
  • 原文地址:https://www.cnblogs.com/xiaohui123-com/p/6434413.html
Copyright © 2020-2023  润新知