操作 Java I/O输入输出流的基本步骤
1 创建管道
2 读写操作
3 关闭管道
输入流:
public class InputStreamTest {
public static void main(String[] args) {
//File
//String filePath
try {
InputStream in = new FileInputStream("file/test.txt");
// int i = 0;
//
// while(true){
// if((i = in.read()) != -1){
// System.out.println(i);
// }else{
// break;
// }
// }
int i = 0;
byte[] by = new byte[1024];
while(true){
if((i = in.read(by)) != -1){
}else{
break;
}
}
String info = new String(by);
String [] a = info.split("@");
System.out.println(a[0] + " " + a[1].trim());
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出流:
public static void main(String[] args) {
try {
OutputStream out = new FileOutputStream("file/test.txt", true);
String str = "zhangsan@123";
out.write(str.getBytes());
out.flush();//多一步刷新操作
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
原文:http://www.cnblogs.com/CMCM/p/5289953.html