FileInputStream与BufferedInputStream、FileInputStream与FileReader区别

FileInputStream与BufferedInputStream区别:
FileInputStream是字节流,BufferedInputStream是字节缓冲流,使用BufferedInputStream读资源比FileInputStream读取资源的效率高(BufferedInputStream的read方法会读取尽可能多的字节),且FileInputStream对象的read方法会出现阻塞;
 
FileInputStream与FileReader区别:
FileInputStream是字节流,FileReader是字符流,用字节流读取中文的时候,可能会出现乱码,而用字符流则不会出现乱码,而且用字符流读取的速度比字节流要快;

详细代码

FileStreamDemo.java
import java.io.*;
public class FileStreamDemo {
    public static void main(String[] args) {
        try {
            FileInputStream in = new FileInputStream("FileCopy.java");
            FileOutputStream out = new FileOutputStream("fileoutput.txt");
            byte[] bytearray = new byte[1024];
            do {
                in.read(bytearray, 0, 1024);
               out.write(bytearray);
            } while (in.available() > 0);
            in.close();
            out.close();
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
BufferedStreamDemo.java
import java.io.*;
public class FileStreamDemo {
    public static void main(String[] args) {
        try {
            FileInputStream in = new FileInputStream("FileCopy.java");
            FileOutputStream out = new FileOutputStream("bufferoutput.txt");
            BufferedInputStream bufferedIn = new BufferedInputStream(in);
            BufferedOutputStream bufferedOut = new BufferedOutputStream(out);
            byte[] data = new byte[1];
            while (bufferedIn.read(data) != -1) {
                bufferedOut.write(data);
            }
            bufferedOut.flush();
            bufferedIn.close();
            bufferedOut.close();
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 }
import java.io.*;
class  CopyText
{
	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("FileCopy.java");
		FileWriter fw = new FileWriter("filewriter.txt");
		char[] arr = new char[1024];
		int num = 0;
		while((num=fr.read(arr))!=-1)
		{
			fw.write(arr,0,num);
		}
		fw.close();
		fr.close();
	}
}


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: FileReaderFileInputStream都是用于读取文件的类。 FileReader是Java中的一个字符,它可以读取文本文件中的字符。它继承自InputStreamReader类,可以将字节转换为字符FileReader的使用非常简单,只需要创建一个FileReader对象,然后使用read()方法读取文件中的字符即可。 FileInputStream是Java中的一个字节,它可以读取任何类型的文件。它继承自InputStream类,可以读取文件中的字节。FileInputStream的使用也很简单,只需要创建一个FileInputStream对象,然后使用read()方法读取文件中的字节即可。 ### 回答2: FileReaderFileInputStream都是用来读取文件内容的类。 FileReaderReader类的子类,它是文本文件输入的表示。它将文本文件的内容按字符读取,并且提供了适用于文本读取的一些便捷方法。它主要用于读取文本文件的内容,例如TXT文件。使用FileReader时,我们可以按字符、按行或按指定编码来读取文件内容。 FileInputStreamInputStream类的子类,它是字节输入的表示。它将文件的内容按字节读取,并且提供了适用于字节读取的一些方法。它主要用于读取二进制文件的内容,例如图片、视频或音频文件。使用FileInputStream时,我们通常会将其包装在其他的类中(如BufferedInputStream或DataInputStream),以提供更高级的功能。 这两个类的区别在于,FileReader是按字符读取文本文件的类,而FileInputStream是按字节读取文件的类。如果我们需要读取文本文件的内容,可以使用FileReader来读取;如果需要读取二进制文件,可以使用FileInputStream来读取。另外,FileReader提供了更便捷的方法用于读取文本文件内容(如按行读取),而FileInputStream则需要我们自己来处理字节与字符的转换。 综上所述,FileReaderFileInputStream是用于读取文件内容的类,其主要区别在于读取的方式,一个按字符读取文本文件,另一个按字节读取文件内容。我们可以根据需求选择使用哪个类来读取文件。 ### 回答3: FileReaderFileInputStream都是用于读取文件的类,但是有一些不同之处。 1. FileReader是Java IO库中用于读取字符数据的类,而FileInputStream是用于读取字节数据的类。 2. FileReader继承自InputStreamReader类,它将字节转换为字符。而FileInputStream直接继承自InputStream类,用于读取字节数据。 3. FileReader以字符为单位进行读取,可以读取包含Unicode字符的文件。而FileInputStream以字节为单位进行读取,将文件中的字节按照二进制进行处理。 4. FileReader可以读取文本文件的内容,并且提供了方便的字符操作方法,如read()、readLine()等。而FileInputStream只能读取字节,需要自己处理字符编码和字符操作。 5. FileReader适用于读取文本文件,如.txt、.csv等格式的文件。而FileInputStream适用于读取任何类型的文件,包括二进制文件、图片、视频等。 总的来说,如果需要读取文本文件并进行字符操作,建议使用FileReader。如果需要读取任意类型的文件,或者需要处理字节数据,可以使用FileInputStream

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值