java IO基础

java的IO流的分类


###1.按流的操作数据类型分为:字节流与字符流 ####字符流的对象中融入了编码表
###2.按流的流向分为:输入流与输出流

###3.IO流常用基类:
####字节流的抽象基类:
InputStream,OutputStream
####字符流的抽象基类: Reader,Writer ###总体来说IO其实就分两种:一个是操作用的流,另一个是操作用的流

4.IO的处理方式:

写的处理方法:

    class FileWriterDemo
    {
        public static void main(String[] args)
        {
            FileWriter fw =null;
            try
            {
                fw =  new FileWriter(xxxx);
            
                fw.write(xxxxxx);
            }
            catch(IOException e)
            {
                  System.out.println(e.toString());   
            }
            finally
            {
                try
                {
                    if(fw!= null)//此处是防止当new FileWriter失败的时候,造成fw = null,如果fw= null在进行close的话,则会产生空指针异常
                    {
                        fw.close();
                    }
                }
                catch(Exception e)
                {
                    Sysgem.out.println(e.toString());
                }
            }
        }
    }

读的处理方法:

    class FileReaderDemo
    {
        public static void main(String[] args)
        {
            FileReader fr = null;
            try{
                fr = new FileReader(xxxxx);

                int ch = fr.read(xxxxx);

                System.out.println(ch);
            }
            catch(Exception e)
            {
                System.out.println(e.toString());
            }
            finally
            {
                try{
                    if(fr!= null)
                    {
                         fr.close();
                    }
                }catch(Exception e)
                {
                    System.out.println(e.toString());
                }
            }
        }
    }

5.异常的种类:


####FileNotFoundException异常
原文地址:https://www.cnblogs.com/gxcstyle/p/6785362.html