Java InputStream如何读取文件呢?

转自:

Java InputStream如何读取文件呢?

下文笔者将讲述InputStream读取文件的方法分享,如下所示:

FileInputStream通过文件byte数组暂存文件中内容
将其转换为String数据
再根据“回车换行” 进行分割

public static String[] readToString(String filePath) {
    File file = new File(filePath);
    Long filelength = file.length(); // 获取文件长度
    byte[] filecontent = new byte[filelength.intValue()];
    try {
        FileInputStream in =new FileInputStream(file); in .read(filecontent); in .close();
    } catch(FileNotFoundException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }

    String[] fileContentArr = new String(filecontent).split("\r\n");

    return fileContentArr; // 返回文件内容,默认编码
}

使用InputStream从文件里读取数据,在已知文件大小的情况下,建立合适的存储字节数组

public class TestClass {
    public static void main(String args[]) throws Exception {
        File f = new File("E:" + File.separator + "test" + File.separator + "StreamDemo" + File.separator + "java265.txt");
        InputStream in =new FileInputStream(f);
        byte b[] = new byte[(int) f.length()]; //创建合适文件大小的数组
        in .read(b); //读取文件里的内容到b[]数组
        in .close();
        System.out.println(new String(b));
    }
}

使用InputStream从文件里读取数据
当不知道文件大小时,可循环读取文件

public static void main(String args[]) throws Exception {
    File f = new File("E:" + File.separator + "test" + File.separator + "StreamDemo" + File.separator + "java265.txt");
    InputStream in =new FileInputStream(f);
    byte b[] = new byte[1024];
    int len = 0;
    int temp = 0; //全部读取的内容都使用temp接收
    while ((temp = in.read()) != -1) { //当没有读取完时,继续读取
        b[len] = (byte) temp;
        len++;
    } in .close();
    System.out.println(new String(b, 0, len));
}
### 回答1: Java中可以使用InputStream读取文件。可以使用FileInputStream类来读取文件。例如: ``` import java.io.*; public class Main { public static void main(String[] args) { try { File file = new File("example.txt"); InputStream inputStream = new FileInputStream(file); int data = inputStream.read(); while (data != -1) { System.out.print((char) data); data = inputStream.read(); } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 上面的代码将会读取名为"example.txt"的文件并在控制台输出其内容。 ### 回答2: Java中可以使用InputStream读取文件InputStream是一个抽象类,常用的子类有FileInputStream和ByteArrayInputStream等。 以FileInputStream为例,在读取文件时需要先打开文件,并把该文件的路径或文件对象传入到FileInputStream的构造函数中。同时,也可以通过设置缓冲区大小来提高读取效率。 读取文件的步骤如下: 1.创建FileInputStream对象,指定要读取文件路径或文件对象。 2.创建字节数组缓冲区,设置缓冲区大小。 3.定义一个int类型的变量,用于记录当前读取的字节数。 4.通过while循环不断地从流中读取数据,直到读取文件末尾。 5.关闭流。 以下是读取文件的示例代码: ``` FileInputStream fis = new FileInputStream("D:\\test.txt"); byte[] buffer = new byte[1024]; int length = 0; while ((length = fis.read(buffer)) != -1) { String str = new String(buffer, 0, length); System.out.println(str); } fis.close(); ``` 在代码中,先创建一个FileInputStream对象,指定了要读取文件路径,然后创建一个大小为1024的字节数组缓冲区,记录当前读取的字节数的变量为length,通过while循环不断地从流中读取数据,并使用String的构造函数将读取的数据转换为字符串输出,最后关闭流。 使用InputStream读取文件时需要注意以下几点: 1.避免重复打开流,及时关闭流。 2.读取文件时需要考虑编码问题,尤其是文本文件。 3.考虑使用缓冲区提高读取效率。 4.读取文件时需要分块读取,防止内存溢出。 ### 回答3: Java中的InputStream是一个抽象类,它是所有字节输入流的超类。InputStream中的read()方法可以用来读取字节数据,但是它并不能直接读取文本数据,因为文本字符可能会被编码为不同的字节序列。 要读取文本文件,我们可以使用InputStreamReader类,它将字节流转换成字符流,从而能够读取文本文件InputStreamReader将InputStream作为输入源,然后通过指定字符集编码将字节流转换为字符流,使它可以读取文本数据。 下面是一个读取文本文件的示例代码: ```java import java.io.*; public class ReadTextFile { public static void main(String[] args) throws IOException { // 创建输入流 InputStream is = new FileInputStream("test.txt"); // 创建转换流 InputStreamReader isr = new InputStreamReader(is, "UTF-8"); // 创建字符缓冲区 char[] buffer = new char[1024]; // 读取文件内容 int len; StringBuilder sb = new StringBuilder(); while ((len = isr.read(buffer)) != -1) { sb.append(new String(buffer, 0, len)); } // 输出文件内容 System.out.println(sb.toString()); // 关闭输入流 isr.close(); is.close(); } } ``` 这个程序将读取一个名为test.txt的文本文件,并将其内容输出到控制台。在这个程序中,我们首先创建了一个InputStream对象,然后使用它创建一个InputStreamReader对象。在创建InputStreamReader对象时,我们指定了文件的编码格式为UTF-8。接着,我们使用字符缓冲区读取文件内容,并将其输出到控制台。最后,我们关闭了InputStreamReader和InputStream
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值