Java读取文件内容

“卷地风来忽吹散,望湖楼下水如天”
简单点,直接上代码:

//获取文件输入流
File file = new File("D:\\abc.txt");
InputStream inputStream = new FileInputStream(file);
//读取输入流的内容
byte[] filecontent = new byte[1024];
inputStream.read(filecontent);
inputStream.close();
//输出内容
System.out.println(new String(filecontent));

这样读出来的内容就像这样:
这里写图片描述
但是,如果发现输出来有乱码的话,只需要在new String(content,”UTF-8”),加上编码即可: System.out.println(new String(filecontent,"UTF-8"));

猜你喜欢

转载自blog.csdn.net/A_Runner/article/details/82220521
今日推荐