输入输出流(I/O)

一.流概述

    流是一组有序的数据序列,根据操作的类型,可分为输入流和输出流两种。I/O流提供了―条通道程序,可以使用这条通道把源中的字节序列送到目的地,虽然I/O流经常与磁盘文件存取有关,但是程序的源和目的地也可以是键盘鼠标、内存或显示器窗口等。

Java由数据流处理输入输出模式,程序从指向源的输入流中读取源中的数据。源可以是文件、网络、压缩包或者其他数据源。

输出流的指向是数据要到达的目的地,程序通过向输出流中写入数据把信息传递到目的地。输出流的目标可以是文件、网络、压缩包、控制台和其他数据输出目标。

二.输入输出流I/O

    1.输入流

      InputStream 类是字节输入流的抽象类,是所有字节输入流的父类。

该类中所有方法遇到错误时都会引发IOException异常。下面是对该类中的一些方法的简要说明:

 2.输入流

      OutputStream类是字节输入流的抽象类,此抽象类表示输出字节流的所有类的超类。

OutputStream类中的所有方法均返回void,在遇到错误时会引发IOException异常。

 Writer类是字符输出流的抽象类,所有字符输出类的实现都是它的子类。

 3.特点

      以数据为基准,根据数据的流动方向来确定是输入还是输出流。

三.文件构造

    File类是io包中唯一代表磁盘文件本身的对象。File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中的方法实现创建、删除重命名文件等。File类的对象主要用来获取文件本身的一些信息,如文件所在的目录、文件的长度、文件读写权限等。数据流可以将数据写入到文件中而文件也是数据流最常用的数据媒体。 

    1.构造方法

             new  File(文件全路径)

    或      new File(文件路径,文件名)

    2.方法

      (1)exists()  判断是否存在;(2)createNewFile()  创建新文件,如果文件已存在,不会覆盖原文件;(3)getName  获取文件名;(4)delete() 删除;(5)renameTo(File对象) 修改路径及文件名。 

File的构造:

import java.io.*;

public class TestFile {

    public static void main(String[] args) {
     
        
        //构建File实例,指向了硬盘上的一个文件
        File  file =new File("d:\test.txt");
        
        file =new File("d:\", "test1.txt");
        
        //判断文件是否存在
        if(file.exists())
        {
            System.out.println(file.getName()+"文件已存在");
            
            System.out.println(file.getAbsolutePath());
            
            
            //改名
            file.renameTo(new File("g:\text2.txt"));
            
            
            try {
                if(file.createNewFile())
                {
                    System.out.println("创建文件成功");
                }
                else
                {
                    System.out.println("创建文件失败");
                }
            } catch (IOException e) {
                
                System.out.println(e.getMessage());
                
                e.printStackTrace();
            }
            
//            if(file.delete())
//            {
//                System.out.println("删除成功");
//            }
//            else
//            {
//                System.out.println("删除失败");
//            }
        }
        else
        {
            System.out.println(file.getName()+"文件不存在");
            
            //创建
            try {
                if(file.createNewFile())
                {
                    System.out.println("创建文件成功");
                }
                else
                {
                    System.out.println("创建文件失败");
                }
                
            } catch (IOException e) {
                
                System.out.println(e.getMessage());
                
                e.printStackTrace();
            }
        }

    }

}
File

文件输入/输出流

程序运行期间,大部分数据都是在内存中进行操作,当程序结束或关闭时,这些数据将消失。如果需要将数据永久保存,可使用文件输入输出流与指定的文件建立连接,将需要的数据永久保存到文件中。

  1.字节输入输出流

FileInputStream类与FileOutputStream类都是用来操作磁盘文件的。如果用户的文件读取需求比较简单,则可以使用FileInputStream类。该类继承自InputStream类。 FileInputStream类与FileOutputStream类对应,提供了基本的文件写入能力。FileOutputStream类是OutputStream类的子类。

FileInputStream类常用的构造方法如下:

FileInputStream(String name)

FileInputStream(File file)

第一个构造方法使用给定的文件名name创建—个FileInputStream对象,第二个构造方法使用File对象创建FileInputStream对象。第一个构造方法比较简单,但第二个构造方法允许在把文件连接输入流之前对文件作进一步分析。
FileInputStream类与FileOutputStream类相同参数的构造方法,创建一个FileOutputStream对象时,可以指定不存在的文件名,但此文件不能是—个已被其他程序打开的文件。
注意:虽然Java语言在程序结束时自动关闭所有打开的流,但是当使用完流后,显式地关闭任何打开的流仍是一个好习惯。一个被打开的流有可能会用尽系统资源,这取决于平台和实现。如果没有将打开的流关闭,当另一个程序试图打开另一个流时,这些资源可能会得不到。

FileInputStream类与FileOutputStream类方法代码:

package com.hanqi;

import java.io.*;

public class TestFile1 {

    public static void main(String[] args) {
        
        File f1=new File("d:\test.txt");
        
        if(f1.exists())
        {
            System.out.println("文件已存在");
            
            //输入输出D
            //向硬盘上的文件输出文字
            
            
            try 
            {
                
                //构造方法 1.传入File实例 2.传入文件名
                FileOutputStream fo=new FileOutputStream(f1);
                
                String str="大家好";
                
                //以字节数组的形式写入
                byte[] b=str.getBytes();
                
                fo.write(b);//写入
                
                fo.close();//关闭输出流,释放文件
                
                
                
                //从硬盘文件读入文字
                
                //构造方法,指明从哪个文件读入
                FileInputStream fi=new FileInputStream(f1);
                
                //用来装载读入的数据,事先指定一个默认大小
                byte[] c=new byte[1024];
                
                //返回值表示读入数据的长度
                //传入引用类型
                int i=fi.read(c);
                
                //转换指定位数的字符串
                String str1=new String(c,0,i);
                
                System.out.println("读入的内容长度="+str1.length());
                
                System.out.println("读入的内容="+str1);
                
                
            } 
            catch (FileNotFoundException e) 
            {
                System.out.println("要输出的文件不存在");
                
                e.printStackTrace();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        else
        {
            try 
            {
                if(f1.createNewFile())
                {
                    System.out.println("文件创建成功");
                }
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }

    }

}
FileInputStream、FileOutputStream 1
package com.hanqi;

import java.io.*;

public class TestFile2 {

    public static void main(String[] args) {
        
        try 
        {
            //覆盖写入
            //true 追加写入
            FileOutputStream fos=new FileOutputStream("d:\test.txt",true);
            
            String str="
心情不错!";
            
            fos.write(str.getBytes());
            
            
            
            
            fos.close();
            
            FileInputStream fis=new FileInputStream("d:\test.txt");
            
            byte[] b=new byte[200];
            
            int i=fis.read(b);
            
            String str1=new String(b,0,i);
            
            System.out.println("读取内容:"+str1);
            
            fis.close();
        } 
        catch (Exception e) 
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }

    }

}
FileInputStream、FileOutputStream 2

2.字符输入输出流
使用FileOutputStream类向文件中写入数据与使用FileInputStream类从文件中将内容读出来,存在一点不足,即这两个类都只提供了对字节或字节数组的读取方法。由于汉字在文件中占用两个字节,如果使用字节流,读取不好可能会出现乱码现象。此时采用字符流FileReader或FileWriter类即可避免这种现象。
FileReader、FileWriter字符流对应了FileInputStream、FileOutputStream。FileReader流顺序地读取文件,只要不关闭流。每次调用read()方法就顺序地读取源中其余的内容,直到源的末尾或流被关闭。

package com.hanqi;

import java.io.*;

public class TestFile3 {

    public static void main(String[] args) {
        
        
        try
        {
            FileReader fr=new FileReader("d:\test.txt");
            
            char[] c=new char[200];
            
            int i=fr.read(c);
            
            String str=new String(c,0,i);
            
            System.out.println("读取:"+str);
            
            fr.close();
            
            //写入
            FileWriter fw=new FileWriter("d:\test.txt",true);
            
            fw.write("
新追加的内容");
            
            fw.close();
        } 
        catch (Exception e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }

    }

}
FileReader、FileWriter

原文地址:https://www.cnblogs.com/panyiquan/p/5281373.html