欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单字符流输入输出

程序员文章站 2022-06-10 23:14:50
...
package com.simpleFactory.ui;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class ListAndArray
{

    public static void main( String[] args )
    {
        String pathInput = "D:/io.txt";
        String pathWriter = "D:/io1.txt";
        BufferedReader bReader = ListAndArray.openFileAndTOBuffered( pathInput );
        ListAndArray.writeText( bReader, pathWriter );
        ListAndArray.printText( bReader );
    }

    private static BufferedReader openFileAndTOBuffered( String path )
    {
        File file = new File( path );
        BufferedReader bf = null;
        try
        {
            bf = new BufferedReader( new InputStreamReader(
                    new FileInputStream( file ) ) );
        } catch ( FileNotFoundException e )
        {
            e.printStackTrace();
        }
        return bf;
    }

    private static void writeText( BufferedReader bReader, String path )
    {
        BufferedReader bufReader = bReader;
        BufferedWriter bufWriter = null;
        try
        {
            //true为在后面追加
            bufWriter = new BufferedWriter( new FileWriter( path, true ) );
            String strLine = "";
            while ( (strLine = bufReader.readLine()) != null )
            {
                //换行
                bufWriter.newLine();
                bufWriter.write( strLine );
            }
        } catch ( IOException e1 )
        {
            e1.printStackTrace();
        }finally{
            try
            {
                bufWriter.close();
                bufReader.close();
            } catch ( IOException e )
            {
                e.printStackTrace();
            }
        }

    }

    private static void printText( BufferedReader bReader )
    {
        System.out.println("执行完毕");
    }
}