Java 字节流 读取和写入

来源:互联网 发布:可以编辑照片的软件 编辑:程序博客网 时间:2024/05/18 04:01

import java.io.*;

class Test

{

     public static void main(String [] args)

    {

//读

         FileInputStream s=null;

          try

         {

                s=new FileInputStream("路径");

                byte [] b=new byte[1024];

                while(true)

                {

                      int temp=s.read(b,0,b.length);

                      if(temp==-1)

                      {

                             break;

                      }

                }

               //把字节数组转化成字符串

               String ss=new String(b);

               System.out.println(ss);

         }

    }


}





import java.io.*;

class Test

{

     public static void main(String [] args)

    {

//写

         FileInputStream s=null;

         FileoutputStream w=null;

          try

         {

                s=new FileInputStream("路径");

                w=new FileoutputStream("路径");

                byte [] b=new byte[1024];

                while(true)

                {

                      int temp=s.read(b,0,b.length);

                      if(temp==-1)

                      {

                             break;

                      }

                     w.write(b,0,temp);

                }

         }

       catch(Exception e)

       {

               e.printStackTrace();

       }

        finally

      {

           try

          {

                  s.close();

                  w.close();

          }

         catch(Exception e)

        {

               e.printStackTrace();

        }

     }

    }

}




原创粉丝点击