java_IO流之FileInputStream和FileOutputStream
来源:互联网 发布:流水灯c语言程序 编辑:程序博客网 时间:2024/05/28 16:20
看,API说的多简单啊:
FileInputStream
用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用FileReader
。
FileOutputStream
用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用FileWriter
。
FileInputStream与FileOutputStream都是字节流,所以它们都是用来读byte与写byte的。
上例子:
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileStreamTest {public static void main(String[] args) {// writeTest();// readTest_1();// readTest_2();// readTest_3();// copyFile();}// 用字节流文件的读写。private static void copyFile() {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("F:\\library.rar");fos = new FileOutputStream("F:\\library_copy.rar");byte[] buf = new byte[1024];int len = 0;while ((len = fis.read(buf)) != -1) {fos.write(buf, 0, len);}System.out.println("复制完成");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}try {if (fos != null) {fos.close();}} catch (IOException e) {e.printStackTrace();}}}private static void readTest_3() {FileInputStream fis = null;try {fis = new FileInputStream("D:\\log.txt");// available()方法是用于创建一个大小刚刚适合的缓冲区,不过如果资源文件过大的话,很容易造成内在溢出的问题。byte[] buf = new byte[fis.available()];fis.read(buf);System.out.println(new String(buf));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}}}private static void readTest_2() {FileInputStream fis = null;try {fis = new FileInputStream("D:\\log.txt");byte[] buf = new byte[1024];int len = 0;while ((len = fis.read(buf)) != -1) {System.out.println(new String(buf, 0, len));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}}}private static void readTest_1() {FileInputStream fis = null;try {fis = new FileInputStream("D:\\log.txt");int b = 0;while ((b = fis.read()) != -1) {System.out.println((char) b);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}}}private static void writeTest() {FileOutputStream fos = null;try {fos = new FileOutputStream("D:\\log.txt");// 字节流操作的是字节数组。字符流操作的是字符数组。fos.write("abcdefg".getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fos != null) {fos.close();}} catch (IOException e) {e.printStackTrace();}}}}
- java_IO流之FileInputStream和FileOutputStream
- java_IO流之FileInputStream和FileOutputStream
- I/O流之--FileOutputStream 和FileInputStream
- IO流之FileInputStream和FileOutputStream
- 【JAVA的 IO流之FileInputStream和FileOutputStream】
- java核心技术之IO流(二)FileInputStream和FileOutputStream
- Java_io体系之File、FileInputStream、FileOutputStream简介、走进源码及示例——05
- Java文件操作之FileInputStream和FileOutputStream
- Java_IO 文件输入流(FileInputStream)与文件输出流(FileOutputStream)
- IO流之FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream
- Java的IO流--FileInputStream和FileOutputStream
- FileInputStream和FileOutputStream
- IO:FileInputStream和FileOutputStream
- FileInputStream和FileOutputStream类
- FileInputStream 和FileOutputStream
- FileInputStream 和 FileOutputStream
- FileOutputStream和FileInputStream
- FileInputStream和FileOutputStream解惑
- JBPM4.4的包安装及配置
- jQuery扩展函数的用法
- 黑马程序员 IO流整理
- 一个设计良好的爬虫架构必须满足的要求
- android布局文件 merge 标签的使用
- java_IO流之FileInputStream和FileOutputStream
- howdoi源码阅读
- 【九度OJ】 1006
- ThinkPad T430s 摄像头黑屏解决
- jQuery 入门教程(4): Events
- Android上传文件至服务器
- 一淘架构
- Zend studio中开发常用快捷键
- 设计模式——代理模式