首页 > 代码库 > 2014/9/23
2014/9/23
1
System.in
1 public class wori //先用InputStreamReader套System.in,再用BufferedReader套是常用方法 2 { 3 public static void main(String args[]) 4 { 5 InputStreamReader a = new InputStreamReader(System.in); // System.in是InputStream父类的引用,用于键盘输入 6 BufferedReader b = new BufferedReader(a); 7 String s = null; 8 try 9 {10 while((s = b.readLine()) != null) //使用BufferedReader的方法readLine()11 {12 if(s.equalsIgnoreCase("exit")) //13 {14 break;15 }16 System.out.println(s.toUpperCase());17 }18 b.close(); //b close即可19 }20 catch(IOException e)21 {22 e.printStackTrace();23 }24 }25 }
2
DataInputStream, DateOutputStream, ByteArrayInputStream, ByteArrayOutputStream 用于直接读写基本类型数据
1 public class wori 2 { 3 public static void main(String args[]) 4 { 5 ByteArrayOutputStream baos = new ByteArrayOutputStream(); //分配内存给一个字节数组,以此为节点 6 DataOutputStream dos = new DataOutputStream(baos); // 7 try 8 { 9 dos.writeDouble(Math.random()); //10 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); //toByteArray()返回一个字节数组作为节点11 DataInputStream dis = new DataInputStream(bais);12 System.out.println(dis.readDouble()); //13 dos.close();14 dis.close();15 }16 catch(IOException e)17 {18 System.exit(-1);19 }20 21 }22 }
3
PrintStream
1 public class wori 2 { 3 public static void main(String args[]) 4 { 5 PrintStream ps = null; 6 try 7 { 8 FileOutputStream fos = new FileOutputStream("d:/1.txt"); 9 ps = new PrintStream(fos);10 }11 catch(IOException e)12 {13 System.exit(0);14 }15 System.setOut(ps); //16 int i = 0;17 while(i < 100)18 {19 System.out.println(i);20 i++;21 }22 ps.close();23 }24 }
4
1 public class wori 2 { 3 public static void main(String args[]) 4 { 5 try{ 6 FileOutputStream a = new FileOutputStream("d:/1.txt"); 7 FileInputStream b = new FileInputStream("d:/1.txt"); 8 ObjectOutputStream oos = new ObjectOutputStream(a); 9 ObjectInputStream ois = new ObjectInputStream(b);10 Animal n = new Animal();11 oos.writeObject(n); //12 oos.flush();13 oos.close();14 Animal n1 = (Animal)ois.readObject(); //15 ois.close();16 System.out.println(n1.a );17 System.out.println( n1.k);18 }19 catch(IOException e)20 {21 22 }23 catch(ClassNotFoundException o)24 {25 26 }27 28 }29 }
class Animal implements Serializable //序列化接口
{
int a = 0;
transient int k = 23; //透明的,表示不能序列化,无法写进流中
}
2014/9/23
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。