本文主要是介绍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
- 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();
- }
- }
- }
这篇关于fileinputstreame和fileoutputstream的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!