Search code examples
javaoutputstreamfileoutputstream

How can I create string while writing file?


I have a webservice. When I call the function webservice create a file in my pc. I want to create a string from file contents while file is creating.

My function is :

OutStream report = new FleOutStream("report.txt");
request.setReportOutputStream(report);
invokeGetReport(service, request);

after this function report.txt was created. I write this class

public class OutStr extends FileOutputStream
{
....
    @Override
    public void write(byte[] b, int off, int len) throws IOException
    {
        System.out.println("b");
        super.write(b, off, len); 
    }
....
}

and change my object to

OutStr report = new OutStr("report.txt");

I see many b in console. But how can I get text ?


Solution

  • Declare this variable inside your class:

    private String myString = "";
    

    replace "System.out.println("b");" with this:

    myString = new String(b, off, len);
    

    then obtain the value with a getter

    public String getMyString()
    {
        return myString;
    }