JAVA多种方式实现从文件按行读数据和写入文件数据

本文介绍了Java中通过Reader/Writer、Scanner、InputStream/OutputStream以及FileChannel四种方式实现从文件按行读取和写入数据。详细讨论了每种方法的特点和使用方法,包括Reader和Writer的便捷性,Scanner的灵活性,InputStream/OutputStream处理行结束符的复杂性,以及FileChannel配合ByteBuffer的高效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作为一个基本知识,对读写文件相信我们在写代码的时候都遇到过,对于我而言我一般是直接BufferReader或者BufferWriter安排上就完事了,但是我们的实验从来不让我失望,他这一次要求我们用三种不同的方式进行读写文件,没错就是三种。在我好不容易完成了他的要求之后我赶紧将这三种方式变成我的博客嘿嘿嘿

1.Reader and Writer

作为Reader和Writer,他们和InputStream和OutputStream的最大区别是什么,是一次性操作的字节数目,前者一次操作两个后者一次操作一个。但是在这里我们的主要观测点是在他们的方法上,作为最常用的读写文件的类,Reader和Writer为我们提供了很方便的方法,再写方面Writer和BufferedWriter都为我们提供了写字符串的方法。直接write()里面放个字符串就可以了,凡是Reader则没有给我们提供直接按行读取字符串的方法,但是BufferedReader的readline()方法则可以直接读一行字符串,直接调用即可。太简单了这里就不放代码了。。

2.Scanner

Scanner经常被我们用于读取控制台的输入,但是我们只需要将他构造函数中的System.in替换成一个File文件,他就可以变成一个读取文件的Scanner了,Scanner的按行读取方法是nextLine()我们在使用它的时候可以在前面判断它时候hasNextLine()。也是十分方便

File file=new File(fileSource);
	try {
		in=new Scanner(file);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
	@Override
	public String readline() {
		if(in.hasNextLine()){
		return in.nextLine();
		}
		return null;
	}

3.InputStream和OutputStream

下面就是倒霉的FileOutputStream和FileInputStream了,这俩倒霉孩子虽说在有时候挺好使的,但是要是按行来是真的麻烦啊,由于这两个只能读/写byte数组,所以再写的时候我们将字符串调用getBytes()方法转换成byte数组来写

FileOutputStream fos;
	 public OUTStream(File file) {
		try {
			fos=new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("file not find");
			e.printStackTrace();
		}
	}

	@Override
	public void write(String string) {
		// TODO Auto-generated method stub
		byte b[]=string.getBytes();
		try {
			fos.write(b);

要说到FileInputStream就更麻烦了我们判断他是否换行的唯一依据就是\r\n,所以我们只能够一个字节一个字节的读取,并把每次读取的字节转换为char类型存在一个足够大的char数组中,当读到\r的时候证明这行读完了(当read()返回-1的时候证明文件读完了)这时候我们将char数组变为String返回

public class INStream implements INstrategy{
//use Stream as IN strategy
	private byte b[]=new byte[1];
	private char[] cs=new char[1024];
    private FileInputStream fis=null;
	public INStream(String fileSource){
		try {
			fis=new FileInputStream(fileSource);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("file not find");
			System.exit(0);
		}
	}
	@Override
	public String readline()  {
		try {
			int i=0;
			int j=0;
			cs=new char[1024];
			fis.read(b);
			while((b[0]!='\r')){//判断是否换行
				if(j==-1){
					return null;
				}
				cs[i]=(char)b[0];
				j=fis.read(b);//判断是否读完
				i++;
			}
			if(j!=-1){
				fis.read(b);
			}
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new String(cs);
	}

3.FileChannel

刚才说了这么多我们很容易发现,这写还差一种啊,不着急最后一种就是我以前根本就不知到的FileChannel方法,FileChannel翻译过来就是文件通道,按这个翻译看起来应该是能够读写文件的。FileChannel类的使用必须配合一个byteBuffer类使用我们再创建一个FileOutputStream对象之后使用getChannel()方法来创建一条通道,同时我们将要写入的字符串是用getBytes()存在byteBuffer中,翻转byteBuffer使它变为写状态,只要byteBuffe不为空就像FileChannel中写,最后清空bytebuffer完成写操作

public class OUTBuffer implements OUTStrategy{
	FileChannel outChannel;
	ByteBuffer byteBuffer1=ByteBuffer.allocate(1024);
	 public OUTBuffer(File file) {
		 try {
			FileOutputStream fos=new FileOutputStream(file);
			outChannel=fos.getChannel();
		} catch (FileNotFoundException e) {
			System.out.println("file not find");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}

	@Override
	public void write(String string) {
		// TODO Auto-generated method stub
		byteBuffer1.put(string.getBytes());
		byteBuffer1.flip();
		while (byteBuffer1.hasRemaining()) {
			try {
				outChannel.write(byteBuffer1);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		byteBuffer1.clear();
	}

到现在三种读写方式可算是凑全了,不容易不容易啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值