首页 > 其他 > 详细

输入流FileInputStrean获取文件中的内容

时间:2021-08-21 08:17:40      阅读:21      评论:0      收藏:0      [点我收藏+]
import java.io.*;

public class E3 {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop", "e3.txt");
        byte[] a = "好的阿".getBytes();
        //向文件写入内容
        try {
            FileOutputStream out = new FileOutputStream(file, false);
            out.write(a);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读取文件内容
        try {
            FileInputStream in = new FileInputStream(file);
            byte n[]=new byte[9];//声明一个长度为9的字节数组
            int fr=in.read(n);//读取文件
            String s=new String(n,0,n.length);//把字符数组转化为字符串
            System.out.println(s);
            System.out.println(fr);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输入流FileInputStrean获取文件中的内容

原文:https://www.cnblogs.com/nwl517/p/15168225.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!