package com.what21.io11;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class String2Input {
/**
* @param text
*/
public static void readByByte(String text){
InputStream in = null;
try {
// 将字符串转换成输入流
in = new ByteArrayInputStream(text.getBytes());
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.print((char)tempbyte);
}
} catch (IOException e) {
e.printStackTrace();
return;
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param text
*/
public static void readByChar(String text){
InputStream in = null;
InputStreamReader isr = null;
try {
// 将字符串转换成输入流
in = new ByteArrayInputStream(text.getBytes());
isr = new InputStreamReader(in,"GBK");
int tempbyte;
while ((tempbyte = isr.read()) != -1) {
System.out.print((char)tempbyte);
}
} catch (IOException e) {
e.printStackTrace();
return;
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
String text = "http://www.what21.com";
readByByte(text);
System.out.println();
readByChar(text);
}
}