java 输入流得到String

if(input != null)
{
	String strResult="";  
			
	byte[] b = new byte[1024];  
	int i = 0;  
	try {
		while ((i = input.read(b)) != -1) {  
			strResult+=new String(b);  
			b = new byte[1024];   
		}
		input.close();
	} catch (IOException e1) {
	// TODO Auto-generated catch block
		e1.printStackTrace();
	} 
} 
StringBuilder strResult2 = new StringBuilder();  
					BufferedReader bufferReader = new BufferedReader(new InputStreamReader(input));
					try {
						String s ="";
						while((s = bufferReader.readLine()) != null)
						{
							strResult2.append(s);
						}
						bufferReader.close();
						input.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 数据String 是指将数据中的内容读取到一个 String 对象中。常见的数据包括字节(InputStream 和 OutputStream)、字符(Reader 和 Writer)和缓冲等。使用 String 对象可以方便地对数据进行操作和处理。 在 Java 中,可以使用 BufferedReader 类来读取字符中的数据,并将其存储到一个字符串中。例如,以下代码演示了如何从标准输入中读取数据,并将其存储到一个字符串中: ``` import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String str = ""; String line; while ((line = reader.readLine()) != null) { str += line; } System.out.println(str); } } ``` 在上述代码中,我们使用 BufferedReader 类从标准输入中读取数据,并将每行数据存储到一个字符串中。由于 BufferedReader 的 readLine() 方法可以返回 null,表示已经读取到了的末尾,因此我们可以在 while 循环中不断读取数据,直到读取完毕。 除了 BufferedReader,还可以使用 Scanner 类来读取数据中的数据,并将其存储到一个字符串中。例如,以下代码演示了如何使用 Scanner 从标准输入中读取数据,并将其存储到一个字符串中: ``` import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.useDelimiter("\\A").next(); System.out.println(str); } } ``` 在上述代码中,我们使用 Scanner 类从标准输入中读取数据,并使用 useDelimiter() 方法将分隔符设置为整个输入。然后,使用 next() 方法将读取到的数据存储到一个字符串中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值