zoukankan      html  css  js  c++  java
  • java 输入输出流 关于InputStream 和 OutputSteam 实现文本一行一行读入和 文本一行一行输出

    对  byte 有了新的认知;byte表示一个字节 因此他可以表示成 一个 数字; 一个数字可以映射成一个字符;InputStream 中的 read() 函数  的意思是读入一个整数,读入一个整数的意思就是读入一个字节;而  read( byte b[] ) 是读入整个文档内容,可以这么认为 read是把 文本里面的所有数字读进去了;所以只要控制好  read() 函数就可以实现 一行一行读入了; 为什么write()函数写到文档里面不是数字 而是字符呢! 用 System.out.pirntln() 函数打印出来发现确实数字; 其实数字就是字符 字符就是数字,可以把那一堆字符看成数字;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileInputStream;
    import java.io.OutputStream;
    import java.io.InputStream;
    import java.util.Random;
    
    //import java.io.StringBufferInputStream;
    
    public class Number1 {
        public static void main( String arg[] )throws Exception {
            Random rad = new Random();
            File file = new File("D:"+File.separator+"tt.txt");
            file.createNewFile();
            OutputStream out = new FileOutputStream(file); 
            for( int i = 100; i >= 0; i-- )
            {
                int n = rad.nextInt(4)+1; StringBuffer str = new StringBuffer();
                for( int j = 0; j < n; j++ )str.append((char)(rad.nextInt(26) + 'a'));
                str.append("
    "); 
                out.write(str.toString().getBytes());
            }
            File file2 = new File("D:"+File.separator+"xx.txt");
            file2.createNewFile();
            OutputStream output = new FileOutputStream(file2);
            
            InputStream in = new FileInputStream(file);
            byte b[] = new byte[100]; int sum = 0;
            int len = (int)file.length();
            for( int i = 0; i < len; i++ ){
                byte temp = (byte)in.read();
                if( temp != (byte)'
    ' && temp != (byte)'
    ' )
                    b[sum++] = temp;
                else{
                      output.write( new String(b,0,sum).getBytes() ); sum = 0; 
                      if( temp == (byte)'
    ') output.write( (byte)'
    ' );
                      else output.write( (byte)'
    ');
                    }
            }
            in.close();
            out.close();
        }
    }
    View Code
  • 相关阅读:
    《ASP.NET Core项目开发实战入门》带你走进ASP.NET Core开发
    网络监控工具 iftop dstat
    可进行组合的枚举
    查看Qt自带的QSS
    通过文件设置QSS
    QString转QByteArray
    error: jump to case label
    error: static member function cannot have cv-qualifier
    C++ std::vector的大小和容量
    获取QMessageBox返回值
  • 原文地址:https://www.cnblogs.com/wulangzhou/p/3623068.html
Copyright © 2011-2022 走看看