大家好,又见面了,我是你们的朋友全栈君。
废话不多说, 直接上关键代码
package com.zhongjing.file;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException;
public class FileInputStreamDemo {
/** * FileInputStream 字节输入流 –> 读取数据 * @param args */ public static void main(String[] args) { FileInputStream fis = null; File file = new File(“D:/test.txt”); try { fis = new FileInputStream(file); byte[] buf = new byte[1024]; //数据中转站 临时缓冲区 int length = 0; //循环读取文件内容,输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。 //当文件读取到结尾时返回 -1,循环结束。 while((length = fis.read(buf)) != -1){ System.out.println(new String(buf, 0, length)); } } catch (Exception e) { e.printStackTrace(); }finally{ try { fis.close();//强制关闭输入流 } catch (IOException e) { e.printStackTrace(); } } } } 运行结果如下:
关于怎样使用FileOutStream写入内容请查看下面这篇文章 :
https://blog.csdn.net/qq_35661171/article/details/86539554
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/135165.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有