Java的标准输入设备是键盘:System.in
Java的标准输出设备是控制台:System.out
System的两个方法:
static void setIn(InputStream in) 重新分配“标准”输入流。
static void setOut(PrintStream out) 重新分配“标准”输出流。
关于何时使用何种流的一个工具图,首先确定操作源(要复制的文件)和目的(复制到哪):
一。InputStreamReader和OutputStreamWriter转换标准输入输出的字节流与字符流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
public static void main(String[] args) throws IOException { // 系统标准输入设备:键盘。字节流 InputStream in = System.in; /* // 打印敲入的字符 int byter = 0; // 每次读一个字符(英文) while ( (byter = in.read()) != -1 ) { // 回车符、换行符略过 if (byter == '\r' || byter == '\n') continue; else System.out.print((char)byter); } System.out.println("ctrl+z 结束"); */ /* // 打印敲入的字符串 in = System.in; // 可变长度字符串 StringBuilder sb = new StringBuilder(); // 循环读取输入 while ( true ) { int ch = in.read(); if (ch == '\r') // 如果是回车符,重新循环遍历字符 continue; if (ch == '\n') { // 如果是换行符,进行下面转换输出 String string = sb.toString(); if(string.equals("exit")){ System.out.println("Exit 结束"); break; } System.out.println(sb.toString()); sb.delete(0, sb.length()); } else { // 不是回车也不是换行,存储 sb.append((char)ch); } } */ String line = null; /* 1. 使用缓冲字符输入流直接读取键盘输入的字节流 * InputStreamReader 是字节流通向字符流的桥梁 */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 从标准输入的字节,转为的字符 /* 1. 使用缓冲字符输出流直接向控制台打印字符 * OutputStreamWriter 是字符流通向字节流的桥梁 */ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));// 从字符,转为标准输出可用的字节 // 2.标准输入设备输入字节转为字符后,按行读取 while ( (line = br.readLine()) != null ) { if (line.equals("exit")) break; // 3.缓冲字符输出流,经OutputStreamWriter过渡,输出字符到标准输出设备 bw.write(line); bw.newLine(); bw.flush(); } // 4.关闭资源 bw.close(); br.close(); } |
二。改变标准输入输出复制文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public static void main(String[] args) throws IOException { System.setIn(new FileInputStream("test.txt")); // 1.设置新的“标准”输入字节流。 System.setOut(new PrintStream("test2.txt")); // 1.设置新的“标准”输出字节流。 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 2.读取输入字节流到缓冲区字符流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));// 2.将缓冲区字符流写到输出字节流 String line = null; // 3.读取输入字节流到缓冲区字符流 while((line = br.readLine()) != null){ if (line.equals("over")){ break; } // 4.将缓冲区字符流写到输出字节流 bw.write(line); bw.newLine(); bw.flush(); } // 5.关闭资源 bw.close(); br.close(); } |
三。日志输出到文件示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
public static void main(String[] args) { try { System.out.println(1/0); } catch (Exception e) { try { Properties properties = System.getProperties(); // 获取当前系统平台信息 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formatDate = sdf.format(date); // 获取当前时刻 // 方式1: PrintWriter pw = new PrintWriter("log.log"); // 1.日志输出目标 properties.list(pw); // 2.输出当前系统信息 pw.println("================================================================"); pw.println(formatDate); // 3.输出当前时刻 e.printStackTrace(pw); // 4.输出异常信息到指定字符输出流 pw.flush(); // 刷新 // 5.写入文件 /* // 方式2: PrintStream ps = new PrintStream("log2.log"); // 1.日志输出目标 properties.list(ps); // 2.输出当前系统信息 ps.println("================================================================"); ps.println(formatDate); // 3.输出当前时刻 e.printStackTrace(ps); // 4.输出异常信息到指定字节输出流。直接写入文件 */ } catch (Exception e1) { throw new RuntimeException("xxxxxxx"); } } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/1103.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设