Solved

convert string to InputStream

Posted on 2001-09-12
5
1,901 Views
Last Modified: 2012-06-21
How to convert a String "1234678" into InputStream

String strA="asgetryur";

InputStream is = new StringReader(strA)

does not work?



0
Comment
Question by:TLTEO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6476005
Sorry!

It's
java.io.Reader is = new StringReader(strA);
0
 

Author Comment

by:TLTEO
ID: 6476010
But how to put it into a InputStream ??


Like

FileInputStream fis = new FileInputStream(myfile);

I need something like

InputStream is= new StringBufferReder ( .....
0
 

Author Comment

by:TLTEO
ID: 6476015
I need to input this string into a inputstream
0
 
LVL 6

Accepted Solution

by:
kotan earned 5 total points
ID: 6476027
input stream is for bytes and reader/wirter is for char

If you insist to have inputStream, you can convert the string to byte array.

InputStream is = new ByteArrayInputSteam(strA.getBytes());

0
 
LVL 2

Expert Comment

by:vasan_sr
ID: 6478640
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.getMessage());
      System.exit(1);
    }
  }

  public static void convert(String infile, String outfile, String from, String to)
       throws IOException, UnsupportedEncodingException
  {
    // 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.encoding");
    if (to == null) to = System.getProperty("file.encoding");

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

}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 71
sql import cannot be resolved jsp 3 117
glassfish admin console 1 51
junit as external jar or library 7 58
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question