16Java语法回顾之System.in标准的输入输出流

来源:互联网 发布:令狐公子 知乎 编辑:程序博客网 时间:2024/06/18 16:59

Java语法回顾之System.in标准的输入输出流


读了那么多年的书让我明白一个道理。人要稳重,不要想到啥就做啥。做一行越久即使你不会,几年之后慢慢的你也会了,加上一点努力你或许你能成为别人眼中的专家。

System.in&out继承关系

/* * 标准的输入输出流: * System.in    --  InputStream *      System类: *          public static final InputStream in *           *          InputStream is = System.in; *  * System.out   --  PrintStream --  OutputStream *      System类: *          public static final PrintStream out *  *          PrintStream ps = System.out; *          OutputStream os = System.out; */

System.in&out继承关系代码实现

public class SystemInDemo {    public static void main(String[] args) {        InputStream is = System.in; // java.io.BufferedInputStream@60e128        System.out.println(is);        OutputStream os = System.out;// //java.io.PrintStream@18b3364        System.out.println(os);    }}

用System.in实现键盘录入到文本

public class MySystemIn {    /**     * 数据源:System     *      * 目的地:BufferedWriter     *      */    public static void main(String[] args) throws IOException{        //数据源        InputStream in = System.in;        //因为BufferedWriter不能直接接受InputStream作为参数类型,所以需要转换字符流        InputStreamReader isr = new InputStreamReader(in);        BufferedReader br = new BufferedReader(isr);        //目的地        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));        //读取并写入数据        String line = null;        while ((line = br.readLine())!=null) {            //录入文本的结束标记            if ("exit".equals(line)) {                break;            }            bw.write(line);            bw.newLine();            bw.flush();        }        //关闭流        bw.close();        br.close();    }}

PrintStream&PrintWriter打印流

/* * PrintStream:字节打印流 * PrintWriter:字符打印流 *  * 打印流特点: *      A:可以写入任意类型的数据。 *      B:可以自动刷新。必须先启动,并且是使用println,printf及format方法才有效。 *      C:可以直接对文件进行写入。 *          哪些流对象是可以直接对文件进行操作的? *          看构造方法,是否有同时接受FileString类型的参数构造。 *  *      注意:打印流只有写数据的,没有读取数据的。 *  * 构造方法: *      PrintWriter(String fileName)  */

PrintSWriter字符打印流代码测试

public class PrintWriterDemo {    public static void main(String[] args) throws IOException {        // 创建对象        PrintWriter pw = new PrintWriter("a.txt");        //写数据,可以写任意类型        pw.write("hello");//      pw.write(123.123);        pw.flush();        pw.close();    }}

PrintWriter实现自动刷新自动换行println代码测试

public class MySystemIn {    /*     * PrintWriter     *      自动刷新。并且能够自动换行。     */    public static void main(String[] args) throws IOException{        PrintWriter printWriter = new PrintWriter(new FileOutputStream(new File("pw.txt")), true);        printWriter.println(true);        printWriter.println("fafadsfdasf");        printWriter.println(1256);        printWriter.close();    }} 

序列号流

/* * 序列化:把对象按照流一样的方式传输或者存储。 * 反序列化:把网络中的流数据或者文件中的流数据还原成对象。 *  * 把对象存储到文本文件。 * ObjectInputStream:ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化 *      Object readObject() * ObjectOutputStream:ObjectOutputStreamJava 对象的基本数据类型和图形写入 OutputStream。 *      void writeObject(Object obj) */

序列号流代码测试

    /*     * 序列化:把对象按照流一样的方式传输或者存储。     */public class MyTest {    /*     * 序列化:把对象按照流一样的方式传输或者存储。     */    public static void main(String[] args) throws IOException{        //数据源        ObjectOutputStream ooStream = new ObjectOutputStream(new FileOutputStream("oo.txt"));        //初始化对象        Person person = new Person("Hasi",23);        //序列号对象到文本文件中        ooStream.writeObject(person);        ooStream.close();    }}————————————————————————————————————————————————————————public class MyTest {    /*     * 反序列化:把网络中的流数据或者文件中的流数据还原成对象。     */    public static void main(String[] args) throws IOException, ClassNotFoundException{        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oo.txt"));        Person readObject = (Person) ois.readObject();        ois.close();        System.out.println(readObject);    }}

Properties集合和IO流

/* * Properties:是一个表示属性集的集合。可以从流中加载数据或者把数据保存到流中。键和值都是字符串。 *                是唯一一个可以和IO流结合使用的集合类。 *  * Properties的父亲是Hashtable,所以,我们知道它是一个Map体现的。那么,我们就存储数据并遍历。 */

Properties修改方法

/* * Properties作为集合的特殊功能: * 1:修改功能        *      public Object setProperty(String key,String value) * 2:获取功能 *      public String getProperty(String key) *      public String getProperty(String key,String defaultValue) *      public Set<String> stringPropertyNames() */

Properties修改方法代码测试

public class PropertiesDemo2 {    public static void main(String[] args) {        // 创建集合对象        Properties prop = new Properties();        // 添加元素        // System.out.println(prop.setProperty("刘备", "双股剑"));        // System.out.println(prop.setProperty("关羽", "青龙偃月刀"));        // System.out.println(prop.setProperty("张飞", "丈八蛇矛"));        // System.out.println(prop.setProperty("刘备", "哭泣"));        prop.setProperty("刘备", "双股剑");        prop.setProperty("关羽", "青龙偃月刀");        prop.setProperty("张飞", "丈八蛇矛");        // 根据键获取值        // System.out.println(prop.getProperty("刘备"));        // System.out.println(prop.getProperty("关羽"));        // System.out.println(prop.getProperty("张飞"));        // System.out.println(prop.getProperty("赵云"));        // public String getProperty(String key,String defaultValue)        // System.out.println(prop.getProperty("刘备", "黄忠"));        // System.out.println(prop.getProperty("赵云", "黄忠"));        // public Set<String> stringPropertyNames()        Set<String> set = prop.stringPropertyNames();        for (String key : set) {            String value = prop.getProperty(key);            System.out.println(key + "---" + value);        }    }}

集合和IO的结合是什么意思

/* * System: *      public static Properties getProperties():系统属性 *  * 集合和IO的结合是什么意思? *      难道是指可以把集合中的数据存储到IO流中(文件中。) *      不是,如果是的话,那么,任意的集合数据我们都可以存储到文本文件中。 *      那么,到底什么是集合和IO流的结合呢? */

集合和IO的结合代码测试

public class PropertiesDemo3 {    public static void main(String[] args) {        Properties prop = System.getProperties();        // System.out.println(prop.size());        // 遍历        Set<String> set = prop.stringPropertyNames();        for (String key : set) {            String value = prop.getProperty(key);            System.out.println(key + "***" + value);        }    }}打印出当前的系统信息

Properties和IO结合使用

/* * 结合的方式就是,参数传递IO流对象。 * 怎么使用呢? * public void list(PrintStream out) * public void list(PrintWriter out) * 把集合中的数据按照键值对的形式存储到文本文件中。 */

Properties和IO结合使用的代码测试

public class MySystemIn {    /*     * 需求:把获取到的系统信息写入到文本文件中     */    public static void main(String[] args) throws IOException, ClassNotFoundException{        Properties properties = System.getProperties();//      properties.list(new PrintStream(new File("properties.txt")));        PrintStream printStream = new PrintStream(new File("properties.txt"));        properties.list(printStream);        printStream.close();    }}
0 0
原创粉丝点击