将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
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- class IODemo
- {
- public static void main(String[] args)
- {
- try
- {
- //使用FileInputStream和FileOutputStream进行文件复制
- FileInputStream fis=new FileInputStream("a.txt");
- FileOutputStream fos=new FileOutputStream("b.txt");
- int read;
- //read=fis.read();
- byte b[]=new byte[1024];
- //读取文件,存入字节数组b,返回读取到的字符数,存入read,默认每次将b数组装满
- read=fis.read(b);
- while(read!=-1)
- {
- fos.write(b,0,read);
- read=fis.read(b);
- //read=fis.read();
- }
- fis.close();
- fos.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }