Java BufferedReader create from StringReader to read from a string
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.PrintWriter; import java.io.StringReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader in2 = new BufferedReader(new StringReader("line1\nline2\n")); PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter( "Main.out"))); int lineCount = 1; String s = ""; while ((s = in2.readLine()) != null) out1.println(lineCount++ + ": " + s); out1.close();/*from w w w.ja va 2s .c om*/ System.out.println("done"); } }