- package com.io;
- import java.io.*;
- public class TestInputStream
- { public static void main(String[] args)
- {
- InputStream is=null;
- try
- {
- // 建立了跟文件 english.txt 的连接
- is=new FileInputStream(new File("res/english.txt"));
- // 读取数据
- byte[] data=new byte[1024];
- // 将文件中的数据 全部读取到 data 字节数组里面
- is.read(data);
- // 输出从文件读取的数据
- System.out.println(new String(data));
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- // 关闭文件
- try
- {
- is.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
english.txt 内容
- Provides classes that are fundamental to the design of the Java programming language.
来源: http://www.bubuko.com/infodetail-2659961.html