zoukankan      html  css  js  c++  java
  • Java:IO流与IO设备

    打印流:PrintWriter和PrintStream

    特点:可以直接操作输入流和文件
    //例子1:使用PrintStream将格式化的日期打印到文件中
    import java.io.*;
    import java.util.*;
    import java.text.*;
    class ExceptionInfo
    {
        public static void main(String[] args)throws IOException
        {
            try
            {
                int[] arr = new int[2];
                System.out.println(arr[3]);            
            }
            catch(Exception e)
            {
                try
                {
                    Date d = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String s = sdf.format(d);
                    PrintStream ps = new PrintStream("f:\myfile\IOException.log");
                    ps.println(s);
                    System.setOut(ps);
                }
                catch(Exception e1)
                {
                    throw new RuntimeException("日志文件创建失败!");
                }
                e.printStackTrace(System.out);
            }
        }
    }

    //例子2:使用printStream将系统信息properties打印到文件中

    import java.util.*;
    import java.io.*;
    import java.text.*;
    class SystemInfo
    {
        public static void main(String[] args)throws IOException
        {
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            String s = sdf.format(d);
            Properties p = System.getProperties();
            PrintStream ps = new PrintStream("f:\myfile\SystemInfo.txt");
            System.setOut(ps);
            ps.println(s);
            p.list(ps);
            ps.close();
        }
    }

    //例子3:使用printStream打印流打印数据时对异常的处理

    import java.io.*;
    import java.util.*;
    import java.text.*;
    class ExceptionInfo
    {
        public static void main(String[] args)throws IOException
        {
            try
            {
                int[] arr = new int[2];
                System.out.println(arr[3]);            
            }
            catch(Exception e)
            {
                try
                {
                    Date d = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String s = sdf.format(d);
                    PrintStream ps = new PrintStream("f:\myfile\IOException.log");
                    ps.println(s);
                    System.setOut(ps);
                }
                catch(Exception e1)
                {
                    throw new RuntimeException("日志文件创建失败!");
                }
                e.printStackTrace(System.out);
            }
        }
    }

    读取键盘录入:System.out、System.in

    System.out:对应的是标准输出流设备,控制台(屏幕)

    System.in:对应的是标准输入设备,键盘

    需求:通过键盘录入数据,当录入一行数据后,将就改行数据进行打印,

    如果录入的数据是over,那么停止录入。

    //例子4:
    import java.io.*;
    class ReadInDemo 
    {
        public static void main(String[] args)throws IOException
        {
            InputStream in = System.in;  //从键盘输入数据
            StringBuilder sb = new StringBuilder();
            int ch = 0;
            while(true)
            {
                ch = in.read();
                if(ch=='
    ')
                    continue;
                if(ch=='
    ')
                {
                    String s = sb.toString();
                    if("over".equals(s))
                        break;
                    System.out.println(s.toUpperCase());
                    sb.delete(0,sb.length());
                }
                else
                 sb.append((char)ch);        
            }
            in.close();
        }
    }

     

  • 相关阅读:
    【BZOJ2959】—长跑(LCT维护双连通分量+并查集)
    【BZOJ5394】【Ynoi2016】—炸脖龙(树状数组+广义欧拉定理)
    【BZOJ2588】【Spoj10628】—Count on a tree(主席树)
    SCOI2019爆零记+总结反思
    【SCOI2019】—DAY1T1平台跳跃(打表+高精度)
    省选模板复习—【字符串】
    省选模板复习—【数据结构】
    【BZOJ3572】【HNOI2014】—世界树(虚树+倍增+dp)
    python paramiko 模块
    python sys模块
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4918091.html
Copyright © 2011-2022 走看看