IO流实例-字节流

从硬盘存在的一个文件读取内容到程序,需要使用FileInputStream
    @Test
    public void testFileInputStream1() throws IOException {
	// 1.创建一个File对象
	File file = new File("hello.txt");
	// 2.创建一个FileInputStream类的对象
	FileInputStream fis = new FileInputStream(file);
	// 3.调用FileInputStream类的方法,完成对File文件的读取
	// read()方法:读取文件的一个字节,当执行到文件末尾时,返回-1
	/*
	 * int b = fis.read(); // 判断有没有读取到文件结尾 while (b != -1) {
	 * System.out.println((char) b); b = fis.read(); }
	 */
	int b;
	while ((b = fis.read()) != -1) {
	    System.out.println((char) b);
	}
	// 4.关闭相应的流
	fis.close();
    }
    // 使用try-catch的方式处理异常可以保证流的关闭一定执行
    @Test
    public void testFileInputStream2() {
	// 2.创建一个FileInputStream类的对象
	FileInputStream fis = null;
	try {
	    // 1.创建一个File对象
	    File file = new File("hello.txt");
	    fis = new FileInputStream(file);
	    // 3.调用FileInputStream类的方法,完成对File文件的读取
	    // read()方法:读取文件的一个字节,当执行到文件末尾时,返回-1
	    int b;
	    while ((b = fis.read()) != -1) {
		System.out.println((char) b);
	    }
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    // 4.关闭相应的流
	    try {
		fis.close();
	    } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}

    }
    @Test
    public void testFileInputStream3() {
	FileInputStream fis = null;
	try {
	    File file = new File("hello.txt");
	    fis = new FileInputStream(file);
	    // 读取到数据写入到此数组中abcdefgh
	    byte[] b = new byte[5];
	    // 每次读入到byte中的字节长度
	    int len;
	    while ((len = fis.read(b)) != -1) {
		/*
		 * for (int i = 0; i < len; i++) {
		 * System.out.println((char)b[i]); }
		 */
		String str = new String(b, 0, len);
		System.out.println(str);
	    }
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    try {
		fis.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
    }
    // FileOutputStream
    @Test
    public void testFileOUTputStream() {
	// 2.创建一个FileOutputStream对象,将file对象作为形参传递到构造器中
	FileOutputStream fos = null;
	try {
	    // 1.创建一个File对象,指定要写入的文件位置
	    // 如果指定的文件存在,则将原有文件覆盖,如果不存在,则创建一个新的文件
	    File file = new File("hello2.txt");
	    fos = new FileOutputStream(file);
	    // 3.写入数据
	    String str = "苏喂苏喂苏喂苏喂";
	    // 需要将字符串转化成byte类型的数组write(byte[] b)
	    fos.write(str.getBytes());
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    // 4.关闭输出流
	    try {
		fos.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}

    }
    // 从硬盘读取一个文件,并写入到另外一个位置(复制)
    @Test
    public void testFileOutputStream1() {
	FileInputStream fis = null;
	FileOutputStream fos = null;

	try {
	    // 1.提供读入、写出的文件
	    File file1 = new File("hello.txt");
	    File file2 = new File("hello3.txt");
	    // 2.提供相应的输入流和输出流对象
	    fis = new FileInputStream(file1);
	    fos = new FileOutputStream(file2);
	    // 3.实现文件的复制(先读数据再写数据)
	    byte[] b = new byte[24];
	    int len;
	    while ((len = fis.read(b)) != -1) {
		// 写数据 从头开始写 写的长度为len
		fos.write(b, 0, len);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    // 4.关闭相应的输入流、输出流
	    // 最早打开的最晚关闭
	    if (fos!=null) {
		try {
		    fos.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	    if (fis!=null) {
		try {
		    fis.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
    }
    @Test
    public void Method(){
	String src = "one.jpg";
	String dest = "two.jpg";
	copyFile(src, dest);
    }
    
    public void copyFile(String source,String dest) {
	FileInputStream fis = null;
	FileOutputStream fos = null;

	try {
	    // 1.提供读入、写出的文件
	    File file1 = new File(source);
	    File file2 = new File(dest);
	    // 2.提供相应的输入流和输出流对象
	    fis = new FileInputStream(file1);
	    fos = new FileOutputStream(file2);
	    // 3.实现文件的复制(先读数据再写数据)
	    byte[] b = new byte[24];
	    int len;
	    while ((len = fis.read(b)) != -1) {
		// 写数据 从头开始写 写的长度为len
		fos.write(b, 0, len);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    // 4.关闭相应的输入流、输出流
	    // 最早打开的最晚关闭
	    if (fos!=null) {
		try {
		    fos.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	    if (fis!=null) {
		try {
		    fis.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值