如何将一个Stream类型的流写入一个文件中

如何将一个Stream类型的流写入一个文件中,第1张

基于流(Stream)的解决

流是单向的有方向性的描述信息流的对象,InputStream是输入流的接口,对程序来说是入,是读,可以从文件读,缓存区读,网络节点读等等.

写入文件,对程序来说是出,是写,就是FileOutputStream,可以写入int也可以byte[]

所以解决方案就是从InputStream中读出内存到byte[]中然后,使用FileOutputStream写入文件中.比如:其中一种写法

InputStream is = new FileInputStream("a.txt")

FileOutputStream fos = new FileOutputStream("b.txt")

byte[] b = new byte[1024]

while((is.read(b)) != -1){

fos.write(b)

}

is.close()

fos.close()

Stream流读写文件: 

 

private void button3_Click(object sender, EventArgs e)

        {

            //将textBox3.Text写入nihao.txt文件

            Stream s = new FileStream("nihao.txt",FileMode.Create,FileAccess.Write)

//打开模式,访问方式

            StreamWriter sw = new StreamWriter(s, Encoding.Default)//创建Stream流,指定编码方式

            sw.Write(textBox3.Text)

            sw.Close()

        }

 

private void button2_Click(object sender, EventArgs e)

        {

            //将nihao.txt文件读出到textBox3.Text

            Stream s = new FileStream("nihao.txt", FileMode.Open)

            StreamReader sr = new StreamReader(s,Encoding.Default)

            textBox3.Text= sr.ReadToEnd().ToString()

            sr.Close()

            s.Close()

            //sr.BaseStream的Position或Seek()可移动文件流指针到的任意位置。

使用工具类Properties

工具类有Load()方法 用于加载文件

首先将文件写成流(输入)

File file=new  File(confPath)

            in = new FileInputStream(file)

加载流load(in)如果需要 *** 作则完成具体 *** 作

输出流并保存文件

out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK")

            cp.store(out2)

完成

具体实例代码

public String updateConfParam(ConfParam cpl) throws IOException {

        String error = null

        Properties cp=new Properties()

        InputStream in= null

        OutputStreamWriter out1=null

        OutputStreamWriter out2=null

        JSONObject jObj = new JSONObject()

        try {

            String confPath=validateSystem(cpl.getConfAddress()+"/"+cpl.getConfName())

            File file=new  File(confPath)

            in = new FileInputStream(file)   

            cp.load(in)

            out1=new OutputStreamWriter(new FileOutputStream(confPath+".bak"),"GBK")

            cp.store(out1)

            cpl.setParaOldValue(cp.getProperty(cpl.getParaKey()))

            cp.setProperty(cpl.getParaKey(), cpl.getParaValue())    

            out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK")

            cp.store(out2)

            jObj.put("paraOldValue", cpl.getParaOldValue())

            jObj.put("paraValue", cpl.getParaValue())

        } catch (FileNotFoundException e) {

            error=e.getMessage()

        } catch (IOException e1) {

            error = e1.getMessage()

        }

        finally{

            if(in !=null){

                in.close()

            }

            if(out1 !=null){

                out1.close()

            }

            if(out2 !=null){

                out2.close()

            }

            if(error != null){

                jObj.put("error", error)

            }

        }

        return jObj.toString()

    }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12081540.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存