黑马程序员-读取键盘_转换流_异常日志信息_打印java虚拟机信息

来源:互联网 发布:锐捷网络校园招聘 编辑:程序博客网 时间:2024/05/17 00:54
---------------------- android培训、java培训、期待与您交流! ---------------------- 

读取键盘

System.out:对应的是标准的输出设备,控制台
System.in:对应的是标准的输入设备,键盘
public static final InputStream in“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源。(阻塞方法)(控制台结束Ctrl+c) 
public static final PrintStream out“标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标。
import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class InputStreamReaderDemo {public static void main(String[] args) {InputStream in = System.in;InputStreamReader insr = new InputStreamReader(in);StringBuilder sb = new StringBuilder();try {int i =0;while(true){i = insr.read();if(i == '\r')continue;if(i == '\n'){String str = sb.toString(); sb.delete(0, sb.length());if(str.equals("over"))break;System.out.println(str.toUpperCase());}elsesb.append((char)i);}} catch (IOException e) {throw new RuntimeException("读取异常!");}}}


转换流

public class InputStreamReader
extends ReaderInputStreamReader 
字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
每次调用 InputStreamReader 中的一个 read() 方法都会导致从底层输入流读取一个或多个字节。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。  
为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:  
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
public class OutputStreamWriter
extends WriterOutputStreamWriter 
字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。
每次调用 write() 方法都会导致在给定字符(或字符集)上调用编码转换器。在写入底层输出流之前,得到的这些字节将在缓冲区中累积。可以指定此缓冲区的大小,不过,默认的缓冲区对多数用途来说已足够大。注意,传递给 write() 方法的字符没有缓冲。
为了获得最高效率,可考虑将 OutputStreamWriter 包装到 BufferedWriter 中,以避免频繁调用转换器。例如:
 Writer out= new BufferedWriter(new OutputStreamWriter(System.out));
import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class InputStreamReaderDemo {public static void main(String[] args) {InputStream in = System.in;InputStreamReader insr = new InputStreamReader(in);StringBuilder sb = new StringBuilder();try {int i =0;while(true){i = insr.read();if(i == '\r')continue;if(i == '\n'){String str = sb.toString(); sb.delete(0, sb.length());if(str.equals("over"))break;System.out.println(str.toUpperCase());}elsesb.append((char)i);}} catch (IOException e) {throw new RuntimeException("读取异常!");}}}


异常日志信息

import java.io.FileNotFoundException;import java.io.PrintStream;import java.text.SimpleDateFormat;import java.util.Date;public class ExceptionInfo {public static void main(String[] args) {try {int[] i = new int[2];System.out.println(i[3]);} catch (Exception e) {Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//MM年中的月数mm小时中的分钟数,HH24小时制hh12小时制try {System.setOut(new PrintStream("Exception.txt"));System.out.println(sdf.format(date));} catch (FileNotFoundException e1) {throw new RuntimeException("创建文件异常");}e.printStackTrace(System.out);//e.printStackTrace(); 默认标准输出就是控制台}}}


打印java虚拟机信息

import java.io.FileNotFoundException;import java.io.PrintStream;import java.util.Properties;public class SystemInfo {public static void main(String[] args) {Properties pro = System.getProperties();//Properties类中list()方法打印//pro.list(System.out);try {pro.list(new PrintStream("SystemInfo.txt"));} catch (FileNotFoundException e) {throw new RuntimeException("文件创建异常");}}}

---------------------- android培训、java培训、期待与您交流! ---------------------- 
0 0
原创粉丝点击