TLTEO
asked on
convert string to InputStream
How to convert a String "1234678" into InputStream
String strA="asgetryur";
InputStream is = new StringReader(strA)
does not work?
String strA="asgetryur";
InputStream is = new StringReader(strA)
does not work?
ASKER
But how to put it into a InputStream ??
Like
FileInputStream fis = new FileInputStream(myfile);
I need something like
InputStream is= new StringBufferReder ( .....
Like
FileInputStream fis = new FileInputStream(myfile);
I need something like
InputStream is= new StringBufferReder ( .....
ASKER
I need to input this string into a inputstream
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi,
see the code
import java.io.*;
public class inputtest {
public static void main(String[] args) {
String outfile = null;
try { convert(args[0], args[1], "GB2312", "ISO8859-6"); } // or "BIG5"
catch (Exception e) {
System.out.print(e.getMess age());
System.exit(1);
}
}
public static void convert(String infile, String outfile, String from, String to)
throws IOException, UnsupportedEncodingExcepti on
{
// set up byte streams
InputStream in;
if (infile != null) in = new FileInputStream(infile);
else in = System.in;
OutputStream out;
if (outfile != null) out = new FileOutputStream(outfile);
else out = System.out;
// Use default encoding if no encoding is specified.
if (from == null) from = System.getProperty("file.e ncoding");
if (to == null) to = System.getProperty("file.e ncoding");
// Set up character stream
Reader r = new BufferedReader(new InputStreamReader(in, from));
Writer w = new BufferedWriter(new OutputStreamWriter(out, to));
// Copy characters from input to output. The InputStreamReader
// converts from the input encoding to Unicode,, and the OutputStreamWriter
// converts from Unicode to the output encoding. Characters that cannot be
// represented in the output encoding are output as '?'
char[] buffer = new char[4096];
int len;
while((len = r.read(buffer)) != -1){
w.write(buffer, 0, len);
System.out.println(buffer) ;
}
r.close();
w.flush();
w.close();
}
}
see the code
import java.io.*;
public class inputtest {
public static void main(String[] args) {
String outfile = null;
try { convert(args[0], args[1], "GB2312", "ISO8859-6"); } // or "BIG5"
catch (Exception e) {
System.out.print(e.getMess
System.exit(1);
}
}
public static void convert(String infile, String outfile, String from, String to)
throws IOException, UnsupportedEncodingExcepti
{
// set up byte streams
InputStream in;
if (infile != null) in = new FileInputStream(infile);
else in = System.in;
OutputStream out;
if (outfile != null) out = new FileOutputStream(outfile);
else out = System.out;
// Use default encoding if no encoding is specified.
if (from == null) from = System.getProperty("file.e
if (to == null) to = System.getProperty("file.e
// Set up character stream
Reader r = new BufferedReader(new InputStreamReader(in, from));
Writer w = new BufferedWriter(new OutputStreamWriter(out, to));
// Copy characters from input to output. The InputStreamReader
// converts from the input encoding to Unicode,, and the OutputStreamWriter
// converts from Unicode to the output encoding. Characters that cannot be
// represented in the output encoding are output as '?'
char[] buffer = new char[4096];
int len;
while((len = r.read(buffer)) != -1){
w.write(buffer, 0, len);
System.out.println(buffer)
}
r.close();
w.flush();
w.close();
}
}
It's
java.io.Reader is = new StringReader(strA);