Search code examples
javastringtype-conversioninputstreamdatainputstream

How can I convert a String into a DataInputStream type in Java?


Is there any way to do this conversion in Java? I have tried the suggested tips online for converting a String into an InputStream type, but it is not the same, and there is nothing else online that I can find.


Solution

  • This might help

    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.InputStream;
    import java.nio.charset.Charset;
    
    public class MyApp {
    
        public static void main(String[] args) throws Exception {
    
            InputStream is = new ByteArrayInputStream("hi I am test".getBytes(Charset.forName("UTF-8")));
    
            DataInputStream dataIn = new DataInputStream(is);
            while (dataIn.available() > 0) {
                String k = dataIn.readLine();
                System.out.print(k + " ");
            }
        }
    
    }