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();
        }


    }

}
原文地址:https://www.cnblogs.com/sunxiaoyan/p/9195660.html