Java如何把字符串转化为输入流
try{//将System.in转化为面向字符的流InputStreamReaderir=newInputStreamReader(System.in);in=newBu...
try{
//将System.in转化为面向字符的流
InputStreamReader ir = new InputStreamReader(System.in);
in = new BufferedReader(ir);//为输入流提供缓冲区
while ((s = in.readLine()) != "bye")
System.out.println("Read: " + s);
}
catch(IOException e)
{System.out.println("Error! ");}
}
我现在要把System.in换为一个从TextArea中获取的字符串,如何修改? 展开
//将System.in转化为面向字符的流
InputStreamReader ir = new InputStreamReader(System.in);
in = new BufferedReader(ir);//为输入流提供缓冲区
while ((s = in.readLine()) != "bye")
System.out.println("Read: " + s);
}
catch(IOException e)
{System.out.println("Error! ");}
}
我现在要把System.in换为一个从TextArea中获取的字符串,如何修改? 展开
4个回答
展开全部
代码如下:
/**
* 将一个字符串转化为输入流
*/
public static InputStream getStringStream(String sInputString){
if (sInputString != null && !sInputString.trim().equals("")){
try{
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
return tInputStringStream;
}catch (Exception ex){
ex.printStackTrace();
}
}
return null;
}
/**
* 将一个字符串转化为输入流
*/
public static InputStream getStringStream(String sInputString){
if (sInputString != null && !sInputString.trim().equals("")){
try{
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
return tInputStringStream;
}catch (Exception ex){
ex.printStackTrace();
}
}
return null;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//txt为TextArea内的字符串
try{
InputStream myIn=new ByteArrayInputStream(txt.getBytes());
//将System.in转化为面向字符的流
InputStreamReader ir = new InputStreamReader(myIn);
in = new BufferedReader(ir);//为输入流提供缓冲区
while ((s = in.readLine()) != "bye")
System.out.println("Read: " + s);
}
catch(IOException e)
{System.out.println("Error! ");}
}
try{
InputStream myIn=new ByteArrayInputStream(txt.getBytes());
//将System.in转化为面向字符的流
InputStreamReader ir = new InputStreamReader(myIn);
in = new BufferedReader(ir);//为输入流提供缓冲区
while ((s = in.readLine()) != "bye")
System.out.println("Read: " + s);
}
catch(IOException e)
{System.out.println("Error! ");}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class ReadByte {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(
"c:/bsmain_runtime.log"));
String s;
while (br.ready() && (s = br.readLine()) != "bye") {
System.out.println("Read: " + s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上是直接从文件
===================
一下是从String
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;
class ReadByte {
public static void main(String[] args) {
try {
String test = "1234\r\nsdfs";
PipedWriter pw = new PipedWriter();
PipedReader pr = new PipedReader(pw);
PrintWriter pw2 = new PrintWriter(new BufferedWriter(pw), true);
BufferedReader br = new BufferedReader(pr);
pw2.println(test);
String s;
while (br.ready() && (s = br.readLine()) != "bye") {
System.out.println("Read: " + s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class ReadByte {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(
"c:/bsmain_runtime.log"));
String s;
while (br.ready() && (s = br.readLine()) != "bye") {
System.out.println("Read: " + s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上是直接从文件
===================
一下是从String
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;
class ReadByte {
public static void main(String[] args) {
try {
String test = "1234\r\nsdfs";
PipedWriter pw = new PipedWriter();
PipedReader pr = new PipedReader(pw);
PrintWriter pw2 = new PrintWriter(new BufferedWriter(pw), true);
BufferedReader br = new BufferedReader(pr);
pw2.println(test);
String s;
while (br.ready() && (s = br.readLine()) != "bye") {
System.out.println("Read: " + s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询