zoukankan      html  css  js  c++  java
  • java的IO流初探

    DEMO代码:

    /*
     * 文件IO流的简单演示
     */
    package com.IO;
    import java.io.*;
    
    public class Demo_IO_1
    {
        
        /**
         * @param args
         */
        public static void main(String[] args)
        {
            // TODO Auto-generated method stub
            /*
            File file = new File("/javatest.txt");
            if(!file.exists())
            {
                try
                {
                    file.createNewFile();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                File file2 = new File("F:\ff");
                try
                {
                    if(file2.isDirectory())
                    {
                        
                    }
                    else 
                    {
                        file2.mkdirs();
                        System.out.println("文件夹创建");
                    }
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                }
            }
            */
            //读文件
            File file = new File("F:\javatest.txt");
            //因为file没有读写能力,所以要定义一个inputstream。
            FileInputStream fis = null;
            try
            {
                fis =  new FileInputStream(file);
                //定义一个字节数组,相当于缓存
                byte []b = new byte[1024];
                int n = 0; //实际读取到的字符数
                //循环读取,一直读到文件尾
                while((n=fis.read(b)) != -1)
                {
                    //把字节转成string
                    String s = new String(b,0,n);
                    System.out.println(s);
                }
                
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }
            finally    //无论是否异常必须执行
            {
                //关闭文件流
                try
                {
                    fis.close();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }        
        //写文件
            File file2 = new File("F:\writer.txt");
            FileOutputStream fos = null;
            try
            {
                fos = new FileOutputStream(file2);
                String string = "我是必胜
    我是最棒"; //返回换行
    
                fos.write(string.getBytes());
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }
            finally
            {
                try
                {
                    fos.close();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }    
    }

    代码实现的功能比较简单,就是字节流的读取和写入。

                        字节流(二进制)                字符流(文本文件)
    输入          InputStream                     Reader
    输出          OutputStream                   Writer
  • 相关阅读:
    城市的划入划出效果
    文本溢出省略解决笔记css
    长串英文数字强制折行解决办法css
    Poj 2352 Star
    树状数组(Binary Indexed Trees,二分索引树)
    二叉树的层次遍历
    Uva 107 The Cat in the Hat
    Uva 10336 Rank the Languages
    Uva 536 Tree Recovery
    Uva10701 Pre, in and post
  • 原文地址:https://www.cnblogs.com/starwolf/p/3237182.html
Copyright © 2011-2022 走看看