java之IO节点流示例,其他情况举一反三都是类似的

Posted 小蜗牛爱远行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java之IO节点流示例,其他情况举一反三都是类似的相关的知识,希望对你有一定的参考价值。

  • I/O流的一般操作流程:

  • 创建或者选择数据源

  • 选择I/O流

  • 创建操作对象

  • 创建读取or存储的缓存区

  • 具体操作,写入,读取,转换等等

  • 出现乱码原因:字节转字符,字节数不够,导致乱码;编码不一致导致乱码

  • 注意事项

    • FileInput/OutputStream最适合用于操作非文本文件(如图片、视频等)

    • FileReader/Writer最适合用于操作文本文件(如txt文件)

    • BufferStream可以加速节点流操作文件的速度

      * 抽象基类            节点流(文件流)     缓冲流
      字节流
      * InputStream      FileInputStream     BufferedInputStream
      * OutputStream     FileOutputStream    BufferedOutputStream
      字符流
      * Reader           FileReader          BufferedReader
      * Writer           FileWriter          BufferedWriter
      
    • 示例:文件<----->字节流:BufferedInputStream(new FileInputStream(src)) -----> ByteArrayOutputStream; ByteArrayInputStream ----> new BufferedOutputStream(new FileOutputStream(dest)

  • 示例:

    /*
    *文件转字节流
    */
    public static byte[] fileToByteArray(String filePath)
            //create source
            File file = new File(filePath);
            //select stream
            InputStream is = null;
            ByteArrayOutputStream os = null;
            
            try 
                //create object
                is = new FileInputStream(file);
                os = new ByteArrayOutputStream();
                //create catch place
                byte[] flush = new byte[1024*10];
                //do something
                int len = -1;
                while ((len=is.read(flush))!=-1)
                    String str = new String(flush, 0, len);
                    os.write(str.getBytes());
                
                os.flush();
                return os.toByteArray();
             catch (FileNotFoundException e) 
                e.printStackTrace();
             catch (IOException e) 
                e.printStackTrace();
             finally 
                //先开后关
                try 
                    if (null != os)
                        is.close();
                    
                    
                 catch (IOException e) 
                    e.printStackTrace();
                
                try 
                    if (null != is)
                        is.close();
                    
                 catch (IOException e) 
                    e.printStackTrace();
                
            
            return null;
        
    
    /*
    *字节流转文件
    */
    public static boolean byteArrayToFile(byte[] bytesArray, String filePath)
            //create source
            File file = new File(filePath);
            //select stream
            ByteArrayInputStream is = null;
            FileOutputStream os = null;
            try 
                //create object
                is = new ByteArrayInputStream(bytesArray);
                os = new FileOutputStream(file);
                //create catch place
                byte[] flush = new byte[1024*10];
                //do something
                int len = -1;
                while ((len=is.read(flush))!=-1)
                    os.write(flush, 0, len);
                
                os.flush();
                return true;
             catch (FileNotFoundException e) 
                e.printStackTrace();
             catch (IOException e) 
                e.printStackTrace();
             finally 
                //先开后关
                try 
                    if (null != os)
                        is.close();
                    
    
                 catch (IOException e) 
                    e.printStackTrace();
                
                try 
                    if (null != is)
                        is.close();
                    
                 catch (IOException e) 
                    e.printStackTrace();
                
            
            return false;
        
    
    public static boolean byteArrayToFile(byte[] bytesArray, String filePath)
        //create source
        File file = new File(filePath);
        //select stream(高版本JDK,无需close,try会帮你执行close, 操作字节流使用BufferedI/O,大大提高性能)
        try (ByteArrayInputStream is = new ByteArrayInputStream(bytesArray); OutputStream os = new BufferedOutputStream(new FileOutputStream(file)))
            //create catch place
            byte[] flush = new byte[1024*10];
            int len = -1;
            while ((len=is.read(flush))!=-1)
                os.write(flush, 0, len);
            
            os.flush();
            return true;
         catch (FileNotFoundException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        
        return false;
    
    
  • 字节流,字符流转换;页面源代码读取

import java.io.*;
import java.net.URL;

/**
 * @author yyds
 * 字符流转字节流
 *
 */
public class ConvertTest 
    public static void main(String[] args) 
        //操作system,,in和system.out
        try (
             // InputStreamReader isr = new InputStreamReader(System.in);
             //OutputStreamWriter osw = new OutputStreamWriter(System.out);
             BufferedReader isr = new BufferedReader(new InputStreamReader(System.in));
             BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(System.out));
             BufferedReader is = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(), "UTF-8"));
             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("baidu.html"), "UTF-8"));
             ) 
                int temp;
                String temp2;
                String temp1;
                while ((temp=is.read())!=-1)
                    System.out.print((char) temp);
                    bw.write(temp);
                
                while ((temp1=is.readLine())!=null)
                    bw.write(temp1);
                    bw.newLine();
                
            bw.flush();
            while (!"exit".equals(temp2 = isr.readLine()))
                osw.write(temp2);
                osw.newLine();
                osw.flush();
              catch (IOException e) 
            System.out.println(e.toString());
        
    

以上是关于java之IO节点流示例,其他情况举一反三都是类似的的主要内容,如果未能解决你的问题,请参考以下文章

Java-IO流学习

Java IO流:节点流(文件流)之 FileInputStream

重新java系列之IO流

java IO流 之 其他流

理解Java之IO流

Java之IO流总结