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 用于直接读写基本类型数据
public class wori { public static void main(String args[]) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); //分配内存给一个字节数组,以此为节点 DataOutputStream dos = new DataOutputStream(baos); // try { dos.writeDouble(Math.random()); // ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); //toByteArray()返回一个字节数组作为节点 DataInputStream dis = new DataInputStream(bais); System.out.println(dis.readDouble()); // dos.close(); dis.close(); } catch(IOException e) { System.exit(-1); } } }
3
PrintStream
public class wori { public static void main(String args[]) { PrintStream ps = null; try { FileOutputStream fos = new FileOutputStream("d:/1.txt"); ps = new PrintStream(fos); } catch(IOException e) { System.exit(0); } System.setOut(ps); // int i = 0; while(i < 100) { System.out.println(i); i++; } ps.close(); } }
时间: 2024-12-21 16:49:18