Java 单字节、多字节读取文本文档中的内容
参考2:The try-with-resources Statement
文本文档位于工程下,使用鼠标右键点击工程,选择new -> File,即可创建。

文本文档的格式:GBK

例1:单字节读取
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class Main { public static void main(String[] args) {
System.out.println(System.getProperty("user.dir")); File file = new File("text.txt");
InputStream inputStream = null; try {
if ((file.exists()) && (file.isFile())) {
inputStream = new FileInputStream(file);
int data = -1;
do {
data = inputStream.read();
if (data != -1) {
System.out.print((char) data);
} else {
System.out.print(data);
}
} while (data != -1);
System.out.println();
} else if (!file.exists()) {
System.out.println("The " + file.getName() + " does not exist.");
} else if (!file.isFile()) {
System.out.println("The " + file.getName() + " is not a file.");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
// Closes this input stream and releases any system resources associated with the stream.
inputStream.close();
System.out.println("Close the input stream.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
改写例1:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class Main { public static void main(String[] args) {
System.out.println(System.getProperty("user.dir")); File file = new File("text.txt"); try (InputStream inputStream = new FileInputStream(file)) {
if ((file.exists()) && (file.isFile())) {
int data = -1;
do {
data = inputStream.read();
if (data != -1) {
System.out.print((char) data);
} else {
System.out.print(data);
}
} while (data != -1);
System.out.println();
} else if (!file.exists()) {
System.out.println("The " + file.getName() + " does not exist.");
} else if (!file.isFile()) {
System.out.println("The " + file.getName() + " is not a file.");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
多字节读取
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class IOTest02 { public static void main(String[] args) {
File src = new File("src.txt");
InputStream is = null; try {
is = new FileInputStream(src);
byte[] buffer = new byte[1024 * 1]; // 1k bytes
int length = -1;
while ((length = is.read(buffer)) != -1) {
String str = new String(buffer, 0, length); // decode
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
System.out.println("\n\nInputStream Closed.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Java 单字节、多字节读取文本文档中的内容的更多相关文章
- python 读取文本文档中的数据
import os dir = input('Please input the file dir:')#提示输入文件路径 while not os.path.exists(dir):#判断文件是否存在 ...
- 编写Java程序,读取文本文档的内容,去除文本中包含的“广告”字样,把更改后的内容保存到一个新的文本文档中
查看本章节 查看作业目录 需求说明: 读取文本文档的内容,去除文本中包含的"广告"字样,把更改后的内容保存到一个新的文本文档中 实现思路: 在main() 方法中,使用 new F ...
- c#读取文本文档实践4-读入到list泛型集合计算后写入新文档
商品 数量 单价英语 66 100语文 66 80数学 66 100化学 66 40物理 66 60 上面截图是要处理的文本文档内容,目的是计算出总价并加在最后一列. 这一篇与上一篇比较类似,目的相同 ...
- c#读取文本文档实践1-File.ReadAllLines()
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- AJAX实现简单的读取文本文档内容到网页--AJAX
效果图: Demo.html: <!DOCTYPE html><html lang="en"><head> <meta charset=& ...
- Java 写一段字符到指定的文本文档中,如果该文本文档不存在,则创建该文本文档
写一段字符到指定的文本文档中,如果该文本文档不存在,则创建该文本文档 import java.io.File; import java.io.FileNotFoundException; import ...
- c#读取文本文档实践3-写入到文本本文档
首先通过File.ReadAllLines()方法读入文本文档中内容并返回字符串数组contents,这样每行数据就成为了这个字符串数组contents的一个元素,再利用split()方法将每一个元素 ...
- c#读取文本文档实践2-计算商品价格
商品 数量 单价英语 66 100语文 66 80数学 66 100化学 66 40物理 66 60 上面是文本文档中读入的数据. using System; using System.Collect ...
- C# 读取文本文档(转)
1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出. byte[] byData = ...
随机推荐
- Sprng4之JDBC--很原始的使用方法
\[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]<一头扎进Spring4>第九讲 源码\Spring40 ...
- spring4 注入参数
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- HDOJ 3308 LCIS (线段树)
题目: Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. ...
- 【python网络爬虫】之requests相关模块
python网络爬虫的学习第一步 [python网络爬虫]之0 爬虫与反扒 [python网络爬虫]之一 简单介绍 [python网络爬虫]之二 python uillib库 [python网络爬虫] ...
- Pyperclip could not find a copy/paste mechanism for your system.
sudo apt-get install xsel sudo apt-get install xclip pip install gtk to install the gtk Python modul ...
- 内核中dump_stack()的实现,并在用户态模拟dump_stack()【转】
转自:https://blog.csdn.net/jasonchen_gbd/article/details/44066815?utm_source=blogxgwz8 版权声明:本文为博主原创文章, ...
- keepalived的vip无法ping通【原创】
今天收到redis的keepalived vip无法ping通的告警,查看服务器和服务时发现vip在服务器上,服务也正常.只能在本机ping通,跨网段无法ping通.切换keepalived vip至 ...
- VC获取操作系统位数
方法1,msdn 有相应的例子,代码贴出来给你看看 MSDN有相应Example! #include <windows.h> typedef BOOL (WINAPI *LPFN_ISWO ...
- 在PHP中使用AES加密算法加密数据及解密数据
这个算法可以将数据加密后,储存起来,到需要用的时候,用之前加密的秘钥将之还原. 除了这个之外,还有AES这个算法能够将数据很好的加密起来,在传输过程中不容易被破解. 在PHP中,我们必须先安装好mcr ...
- Alpha冲刺(9/10)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺倒计时之9 团队部分 后敬甲(组长) 过去两天完成了哪些任务 答辩准备中 和大佬们跟进进度 接下来的计划 准备答辩 ...