Java -- FileInputStream与FileOutputStream的简单使用

来源:互联网 发布:python并行图像处理 编辑:程序博客网 时间:2024/06/05 07:05

本地文件读写编程的基本过程为:

①  生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类);

②  调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容;

③  关闭文件(close())。


//读取某路径下的文件File file = new File("D:\\123.txt");try {  FileInputStream fis=new FileInputStream(file);//新建一个FileInputStream对象  try {      byte[] b=new byte[fis.available()];//新建一个字节数组      fis.read(b);//将文件中的内容读取到字节数组中      fis.close();      String str2=new String(b);//再将字节数组中的内容转化成字符串形式输出      System.out.println(str2);      } catch (IOException e) {        e.printStackTrace();      }         } catch (FileNotFoundException e) {  e.printStackTrace();}//文件的写入FileOutputStream file_out = new FileOutputStream(file);file_out.write(outputData);  //byte数据file_out.close(); 

继承于InputStream和OutputStream的类,用于本地文件读写(二进制格式读写并且是顺序读写,读和写要分别创建出不同的文件流对象)

http://www.cnblogs.com/kyxyes/archive/2013/02/16/2913424.html

http://www.cnblogs.com/jjtech/archive/2011/04/17/2019210.html

0 0