IO流的数据来源分别为四种硬盘、内存、键盘、网络
硬盘
public class TestDemo {
public static
void main(String[] args) {
//磁盘IO
try {
FileInputStream
fileInputStream =new FileInputStream("F:/test.txt");
} catch (FileNotFoundException
e) {
e.printStackTrace();
}
}
}
内存
public class TestDemo02 {
public static void main(String[] args) {
//内存
String str ="hello world";
ByteArrayInputStream byteArrayInputStream =new ByteArrayInputStream(str.getBytes());
int i = 0;
while ((i = byteArrayInputStream.read()) != -1){
System.out.print((char)i);
}
}
}
键盘
public class TestDemo03 {
public static void main(String[] args) throws IOException {
//键盘Scanner
InputStream inputStream = System.in;
int i = 0;
while ((i = inputStream.read()) != -1){
System.out.print((char)i);
}
}
}
网络
public class TestDemo04 {
public static void main(String[] args) throws IOException {
//网络IO
Socket socket = new Socket();
socket.getInputStream();
socket.getOutputStream();
}
}
原文:https://www.cnblogs.com/liyaolog/p/12879135.html