zoukankan      html  css  js  c++  java
  • Java基础之IO流,转换流应用InputStreamReader,OutputStreamWriter

    import java.io.*;

    class SystemInDemo
    {
        /*
            以键盘录入与屏幕输出为例
        
    */
        public static void main(String[] args) throws IOException
        {        
            //改变标准输入与输出设备实例文件复制操作
            System.setIn(new FileInputStream(new File("BufferedCopyFileDemo.java")));
            System.setOut(new PrintStream(new File("hah.txt")));
        
            //字节流->字符流
            
    //字符流->字节流
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            String line = null;
            while((line=br.readLine())!=null)
            {
                if("over".equals(line))
                    break;
                
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            
            br.close();
            
            /*
            //读取字节流
            InputStream inputStream = System.in;
            StringBuilder sb = new StringBuilder();
            int ch = 0;
            while((ch=inputStream.read())!=-1)
            {
                if('\r'==ch)
                    continue;
                if('\n'==ch)
                {
                    if(sb.toString().equals("over"))
                        break;
                    
                    System.out.println(sb.toString());
                    sb.delete(0,sb.length());
                }
                else
                    sb.append((char)ch);
            }
                    
            inputStream.close();
            
    */
        }
    }
  • 相关阅读:
    正则表达式
    javascript if(条件)------------条件中可以使用的值
    maven配置
    获取中文日期
    Oracle无安装客户端安装方法
    Eclipse org.eclipse.compare plug-in
    Differences or similarities between Java and C++
    Java Programming Guidelines
    Native Method
    Object in Java same as pointer
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2887081.html
Copyright © 2011-2022 走看看