定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5)
- public static void main(String[] args) {
- File file=new File("d:"+File.separator+"test4.txt");
- if(!file.exists())throw new RuntimeException("压根没文件");
- FileInputStream in;
- ByteArrayOutputStream arr=new ByteArrayOutputStream();
- try {
- in=new FileInputStream(file);
- byte[] data=new byte[5];
- int len=0;
- try {
- while((len=in.read(data))!=-1){
- arr.write(data, 0, len);
-
- }
- System.out.println(arr.toString("UTF-8"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
复制代码 |
|