147_文件读取_写出_追加文件

一、文件的读取与写入

  这里一定要注意文件读取和写入都是以程序为中心!

二、文件读取与写入示例

package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * [说明]:文件的读取与写入
 * @author aeon
 *
 */
public class TestIo {
    /**
     *读取指定文件中的内容
     * @param file
     */
    public static void testInput(File file){
        //1、建立联系、选择流
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            //2、设置缓存数组及实际读取文件长度
            byte [] by=new byte[1024];
            int len=0;//实际读取的长度
            //3、不断的读取 
            //len:实际读取到的大小
            //!=-1:的意思是文件还有内容就循环一直读取
            //is.read(by)将读取到的字节放入到by的这个缓存数组中
            while(-1!=(len=inputStream.read(by))){
                //将by这个缓存数组中的所有数据(0,len)转换为String并输出
                String str=new String(by,0,len);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            System.out.println("读取的文件找不到!");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("文件读取失败!");
            e.printStackTrace();
        }finally{//关闭资源
            if(null!=inputStream){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    System.out.println("文件流关闭失败!");
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 向指定文件中写入内容
     * @param file
     */
    public static void testOutput(File file){
        OutputStream outputStream=null;
        try {
            //选择流,此处的第二个参数true代表着文件的追加、默认是文件的覆盖!
            outputStream=new FileOutputStream(file,true);
            //设置写出的内容
是换行
            String str="我爱中华人民共和国!
";
            //将str中的内容放到字节数组中
            byte by[]=str.getBytes();
            try {
                outputStream.write(by, 0, by.length);
                //强制将注入到管道中的数据刷出
                outputStream.flush();
            } catch (IOException e) {
                System.out.println("文件写出失败!");
                e.printStackTrace();
            }finally{
                //关闭资源
                try {
                    if (null != outputStream) {
                        outputStream.close();
                    } 
                } catch (Exception e2) {
                    System.out.println("关闭资源失败!");
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("文件找不到!");
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        //建立联系
        File file=new File("b:/test/testInput.txt");
        //读取指定文件中的内容
        testInput(file);
        file=new File("b:/test/testOut.txt");
        //向指定文件中写入内容
        testOutput(file);
    }
}

结果截图:

  

testInput.txt内容截图:

  

ttestOut.txt文件内容截图:

  

如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

原文地址:https://www.cnblogs.com/aeon/p/10047821.html