zoukankan      html  css  js  c++  java
  • IO 流(InputStream,OutputStream)

    1.

    InputStream,OutputStream都是抽象类,所以不能创建对象。

    1个中文占两个字节

    package com.ic.demo01;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class StreamDemo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                File file = new File("f://myfile.txt");
                InputStream inputStream = new FileInputStream(file);
                //读取第一个内容
                //int i = inputStream.read();
                //读取全部内容,取出来的都是ASCII的编码,读取的第一种,但是这个不可以读中文
                 int i=0;
                 String str="";
                /* while((i=inputStream.read())!=-1){
                     System.out.println(i);
                     str = str+(char)i;
                 }*/
                 
                 //读取的第二种方式,可以读中文
                // inputStream.available();可读大小,可变
                 byte[] b = new byte[(int)file.length()];
                 inputStream.read(b);
                 str=new String(b);
                 
                 OutputStream outputStream = new FileOutputStream(file);
                 //getBytes() 是Java编程语言中将一个字符串转化为一个字节数组byte[]的方法。String的getBytes()方法是得到一个系统默认的编码格式的字节数组。
                 outputStream.write(str.toUpperCase().getBytes());
                
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
        }
    
    }
  • 相关阅读:
    设计模式-装饰器模式
    自定义 RestTemplate 异常处理 (转)
    Jackson 高级应用
    Jackson 的 基本用法
    Jackson转换为Collection、Array
    Jackson中处理map中的null key 或者null value 及实体字段中的null value
    sed
    MySQL server has gone away 异常
    nl命令
    线程池
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9195660.html
Copyright © 2011-2022 走看看