String ,inputStrem相互转化

1: String ->InputStream:

InputStrem is =new ByteArrayInputStream(str.getBytes());

或者

ByteArrayInputStream stream=newByteArrayInputStream(str.getBytes());

2 :inputStrem ->String :

    InputStream input;

    StringBuffer out = new StringBuffer();

    byte [] b = new byte [4096]

    for(int n ;(n = input.read(b))!=-1;){

    out.append(new String(b,0,n))

}

3、Reader ->String :

BufferedReader in = new BufferedReader(new InputStreamReader(is));

StringBuffer buffer = new StringBuffer();

String line = " ";

while ((line = in.readLine()) != null){

    buffer.append(line);

}

return buffer.toString();

4、String ->reader:

    Reader reader = null;

BufferedReader r = new BufferedReader(reader);

StringBuilder b = new StringBuilder();

String line;

while((line=r.readLine())!=null) {

    b.append(line);

    b.append(“\r\n”);

}

b.toString();

你可能感兴趣的:(String ,inputStrem相互转化)