首页>代码>java通过InputStream读取和写入文件操作实例代码>/InputStreamFile/src/com/test/InputStreamFile.java
package com.test;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;


public class InputStreamFile
{
    public static void main(String[] args)
    {
        InputStreamFile app = new InputStreamFile();
        app.fileToInputStream();
        app.inputStreamToFile();
    }


    public void fileToInputStream()
    {
        InputStream inputStream = null;
        BufferedReader br = null;
        try
        {
            String path = System.getProperty("user.dir") + "/bin/file.xml";
            inputStream = new FileInputStream(path);
            br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null)
            {
                sb.append(line);
            }
            System.out.println(sb.toString());
            System.out.println("\nDone!");

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (inputStream != null)
            {
                try
                {
                    inputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (br != null)
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }


    public void inputStreamToFile()
    {
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try
        {
            String userDir = System.getProperty("user.dir");

            String path = userDir + "/bin/file.xml";
            inputStream = new FileInputStream(path);

            String newPath = userDir + "/bin/file-new.xml";
            File file = new File(newPath);
            outputStream = new FileOutputStream(file);

            int bytesWritten = 0;
            int byteCount = 0;

            byte[] bytes = new byte[1024];

            while ((byteCount = inputStream.read(bytes)) != -1)
            {
                outputStream.write(bytes, bytesWritten, byteCount);
                bytesWritten += byteCount;
            }

            System.out.println("Done!");

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (inputStream != null)
            {
                try
                {
                    inputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (outputStream != null)
            {
                try
                {
                    outputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

            }
        }
    }

}
最近下载更多
1126055836  LV13 2021年8月20日
dxsfzy123  LV1 2020年5月26日
三生石sh1  LV13 2019年3月26日
Nen Code  LV1 2018年3月8日
yezifeng  LV1 2018年1月17日
PLOKMIJN  LV6 2017年10月10日
kimliyanlin  LV1 2017年9月18日
ymz123456  LV3 2017年9月14日
almahehe  LV1 2017年5月12日
紫蝶520  LV1 2017年3月15日
最近浏览更多
caomin  LV4 2023年10月25日
bzp1995 2023年5月2日
暂无贡献等级
1719863922  LV11 2022年2月11日
sayalala  LV1 2022年1月12日
AURORORKKK 2021年12月23日
暂无贡献等级
Levi丶 2021年12月21日
暂无贡献等级
2480256868  LV1 2021年11月19日
zxm0317  LV10 2021年11月3日
cccmap 2021年9月22日
暂无贡献等级
hello_jugg  LV7 2021年9月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友