Java IO流的引入

     关于java IO流的知识,可以说是java SE中非常重要的一部分知识,本专栏就将对java中IO流的基础知识结合部分项目经验对其做一个较为系统的介绍。

1,引入话题

        现在有一个需求1:读取一个文件中的数据并以字符串的形式展示在控制台 这个需求在开发中是极其常见的,那么我们就来看看有哪些实现方式。准备工作:

  • 在Eclipse中创建一个Java项目
  • 在项目根目录中创建一个test.txt文件

方式1:

 //方式1:通过InputStream和FileInputStream实现
        InputStream in = new FileInputStream(file);
        byte[] by1=new byte[in.available()];//在文本较小的时候
        in.read(by1);
        System.out.println("FileInputStream文本内容:"+new String(by1).toString());
        in.close();

方式2:

// 方式2:通过BufferedInputStream实现和FileInputStream实现
			InputStream in1 = new FileInputStream(file);
			BufferedInputStream bin = new BufferedInputStream(in1);
			byte[] by2 = new byte[bin.available()];
			bin.read(by2);
			System.out.println("BufferedInputStream文本内容:" + new String(by2).toString());
			in1.close();
			bin.close();// 可有可无

方式3:

// 方式3:通过InputStreamReader实现和FileInputStream实现
			InputStream in2 = new FileInputStream(file);
			Reader reader1 = new InputStreamReader(in2);
			char[] ch1 = new char[in2.available() / 2];
			reader1.read(ch1);
			System.out.println("InputStreamReader文本内容:" + new String(ch1).toString());
			in2.close();
			reader1.close();// 可有可无

方式4:

// 方式4:通过BufferedReader实现和FileInputStream实现
			InputStream in3 = new FileInputStream(file);
			Reader reader2 = new BufferedReader(new InputStreamReader(in3));
			char[] ch2 = new char[in3.available() / 2];
			reader2.read(ch2);
			System.out.println("BufferedReader文本内容:" + new String(ch2).toString());
			in3.close();
			reader2.close();// 可有可无

方式5:

// 方式5:通过FileReader实现和
			FileReader reader3 = new FileReader(file);
			char[] ch3 = new char[1024];
			StringBuilder builder = new StringBuilder();
			while (reader3.read(ch3) != -1) {
				builder.append(new String(ch3).toString());
			}
			reader3.close();
			System.out.println("FileReader文本内容:" + builder.toString());

 

运行结果:

上面见得的列举了五种方法都实现了我们的需求,但是需要注意一点,我这里的byte数组和char数组的长度是根据文件大小来定义的,这对于较大的文件来说不合适,所以对于available()获取长度的定义应该都采用方式5的形式下面是原因,所以我上面是做了一个很错误的示范

备注:

返回从该输入流中可以读取(或跳过)的字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 下一个调用可能是同一个线程或另一个线程。 这个多个字节的单个读取或跳过将不会被阻塞,但可以读取或跳过较少的字节。注意,尽管一些实现InputStream将在流中返回的字节总数,许多人不会。 使用此方法的返回值分配用于保存此流中的所有数据的缓冲区是绝对不正确的。子类此方法的实现可以选择抛出IOException如果输入流已通过调用关闭close()方法。该available类方法InputStream总是返回0 。这个方法应该被子类覆盖。结果当输入流到达输入流的末尾时,可以从该输入流中读取(或跳过)的字节数,而不阻塞的字节数 0 。异常IOException - 如果发生I / O错误。

        既然有文件的读取,自然也有写入内容到文件,所以我们有了需求2:写入一个字符串到指定文件,下面我们就来看看有哪几种实现方式。

方式1:

	// 方式1:通过OutputStream和FileOutputStream实现
			File outFile1 = new File("outTest1.txt");
			if (!outFile1.exists()) {
				outFile1.createNewFile();
			}
			OutputStream os = new FileOutputStream(outFile1);
			os.write(testTxt.getBytes());
			os.close();

方式2:

// 方式2:通过OutputStream和FileOutputStream实现
			File outFile2 = new File("outTest2.txt");
			if (!outFile2.exists()) {
				outFile2.createNewFile();
			}
			OutputStream os2 = new FileOutputStream(outFile2);
			BufferedOutputStream bos2 = new BufferedOutputStream(os2);
			// os2.write(testTxt.getBytes());
			bos2.write(testTxt.getBytes());
			os2.close();
			// bos2.close();//可有可无

方式3:

// 方式3:通过FileWriter和File实现
			File outFile3 = new File("outTest3.txt");
			if (!outFile3.exists()) {
				outFile3.createNewFile();
			}
			FileWriter writer3 = new FileWriter(outFile3);
			writer3.write(testTxt);
			writer3.close();

方式4:

// 方式4:通过BufferedWriter和OutputStreamWriter
			File outFile4 = new File("outTest4.txt");
			if (!outFile4.exists()) {
				outFile4.createNewFile();
			}
			BufferedWriter writer4 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile4)));
			writer4.write(testTxt);
			writer4.close();

上面的几种方式中,方式2是不可行的;这个可以自己试试,至于为什么不行,这里不做解释,毕竟本篇只是引入java IO流的知识,还有一点需要注意,这里的每一个功能实现都需要捕获或者抛出异常。

 

2,简要总结

       文件的读写可以说是开发中必不可少的部分,当然这里需要考虑的问题不仅仅是实现,还包括同步,读写熟读,文件类型等等都需要考虑在具体的项目中。在上面的文件读写实现中,我们充分使用了java 平台类库中提供的IO流接口,包括字节流,字符流,转换流,缓冲流等,这其中部分概念是相互重合的,这个在后续的部分中会详细介绍。

 

3,简单练习

复制文件

package hfut.edu.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Date:2018年10月30日 下午7:57:20 Author:why
 */

public class IOSimpleTest {
	public static void main(String[] args) {

		/**
		 * 把test.txt文件中的内容复制到why.txt中
		 */
		try {
			File fromFile = new File("test.txt");
			File toFile = new File("why.txt");
			if(!toFile.exists()) {
				toFile.createNewFile();
			}
			InputStream in = new FileInputStream(fromFile);
			OutputStream os = new FileOutputStream(toFile);
			byte[] by = new byte[1024];
			while (in.read(by) != -1) {
				os.write(by);
			}
			in.close();
			os.close();

		} catch (IOException e) {
			System.out.println("Exception happen");
		} 

	}

}

下一篇我们就会开始介绍Java IO流的详细知识了。

注:欢迎扫码关注

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值