fileinputstreame和fileoutputstream

本文提供了一个如何将字符串转换为输入流的具体示例,并展示了使用Java实现文件复制的方法。

将string转化为流

public class Main {
    public static void main(String[] args) {
        String text = "Example on how to convert a String to an InputStream";

        try {
            InputStream is = new ByteArrayInputStream(text.getBytes());

            int byteRead;
            while ((byteRead = is.read()) != -1) {
                System.out.print((char)byteRead);
            }
            System.out.println();
            is.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




http://blog.youkuaiyun.com/caixiexin/article/details/6719407

  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. class IODemo  
  5. {  
  6.     public static void main(String[] args)   
  7.     {  
  8.         try  
  9.         {  
  10.         //使用FileInputStream和FileOutputStream进行文件复制  
  11.         FileInputStream fis=new FileInputStream("a.txt");  
  12.         FileOutputStream fos=new FileOutputStream("b.txt");  
  13.             int read;  
  14.             //read=fis.read();  
  15.             byte b[]=new byte[1024];  
  16.             //读取文件,存入字节数组b,返回读取到的字符数,存入read,默认每次将b数组装满  
  17.             read=fis.read(b);  
  18.             while(read!=-1)  
  19.             {  
  20.                 fos.write(b,0,read);  
  21.                 read=fis.read(b);  
  22.                 //read=fis.read();  
  23.             }  
  24.             fis.close();  
  25.             fos.close();  
  26.         }  
  27.         catch (IOException e)  
  28.         {  
  29.             e.printStackTrace();  
  30.         }  
  31.           
  32.     }  
  33. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值