Resources.class.getResourceAsStream 获取配置的方法 getResourceAsStream和getResource的用法及Demo实例

getResourceAsStream和getResource的用法及Demo实例

https://www.cnblogs.com/shanheyongmu/p/5898764.html

Resources.class.getResourceAsStream 获取配置的方法

https://blog.csdn.net/dwl764457208/article/details/78593005


关于.class.getClassLoader().getResourceAsStream
https://blog.csdn.net/wsad578169903/article/details/68160377
类加载器与 Class.getResourceAsStream 问题解决
https://blog.csdn.net/w1196726224/article/details/54428493
getResourceAsStream用法详解
https://www.cnblogs.com/ydxblog/p/5632271.html
java通过InputStream读取和写入文件操作实例代码>/InputStreamFile/src/com/test/InputStreamFile.java

五种方式让你在java中读取properties文件内容不再是难题
https://www.cnblogs.com/hafiz/p/5876243.html
框架的概念及用反射技术开发框架的原理<br/>getResourceAsStream用法详解
http://www.cnblogs.com/linjiqin/archive/2011/02/14/1954647.html

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();
                }

            }
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/chengjun/p/9508804.html