fileinputstreame和fileoutputstream

将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.csdn.net/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. }  

你可能感兴趣的:(fileinputstreame和fileoutputstream)