字节转换成字符


  import java.io.*;
    public class Outchant {


//        字节转化成字符类型
 
    public static void main(String[] args) {

  //    异常处理
            try {
                  fun();
                  fun1();
            }    catch (Exception e) {
            
                e.printStackTrace();
        }
    }
    


//    字节转化成字符类型


       public static void  fun() throws Exception{


//         创建要在那个文件中续写文字

  
            FileOutputStream fos=new FileOutputStream("d:\IO\ac.txt",true);


//        写的格式为utf-8

           OutputStreamWriter fow=new OutputStreamWriter(fos,"utf-8");


//                    续写的文字


              fow.write("学的还行");
        
                fow.close();
    }

      public static void fun1() throws Exception{


//            读取文件中的文字


            FileInputStream fis=new FileInputStream("d:\IO\ac.txt");


//              速度为1M


            byte [] b=new byte[1024*10] ;


//              定义长度为0;


            int len=0;


//            如果长度为-1结束运行


            while((len=fis.read(b))!=-1){


//                  打印


                System.out.println(new String(b,0,len));
        }


              fis.close();


    }
}

原文地址:https://www.cnblogs.com/hph1728390/p/10596540.html