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 = ...
随机推荐
- SpringBoot文件上传大小设置(yml中配置)
#文件大小 MB必须大写 # maxFileSize 是单个文件大小 # maxRequestSize是设置总上传的数据大小 spring: servlet: multipart: enabled: ...
- ActiveMQ静态网络链接(broker-to-broker)
ActiveMQ的网络连接分为静态连接和动态连接.本章研究静态连接. 1.ActiveMQ的networkConnector是什么 在某些情况下,需要多个ActiveMQ的Broker做集群,那么就涉 ...
- HBSX2019 3月训练
Day 1 3月有31天废话 今天先颓过了就只剩30天了 初步计划 每天一道字符串/数据结构题 图论学习 根据<若干图论模型探讨>(lyd)复习 二分图与网络流学习 <算法竞赛进阶指 ...
- css模拟时钟
css模拟时钟 思路: 画时钟数字(x,y)坐标 x = x0 + r*cos(deg) y = y0 + r*sin(deg) 知识点: 创建元素: createElement 添加元素: appe ...
- 旷视科技 -- Face++ 世界最大的人脸识别技术平台
旷视科技 -- Face++ 世界最大的人脸识别技术平台: https://www.megvii.com/
- c++不定参数函数
不定参数当年做为C/C++语言一个特长被很多人推崇,但是实际上这种技术并没有应用很多.除了格式化输出之外,我实在没看到多少应用.主要原因是这种技术比较麻烦,副作用也比较多,而一般情况下重载函数也足以替 ...
- 【转】Leveldb源码分析——1
先来看看Leveldb的基本框架,几大关键组件,如图1-1所示. Leveldb是一种基于operation log的文件系统,是Log-Structured-Merge Tree的典型实现.LSM源 ...
- copy之深浅拷贝
深浅拷贝深拷贝 全部复制浅拷贝 只复制第一层 __author__ = 'Perfect' # -*- coding: utf-8 -*- import copy # copy.copy() #浅拷贝 ...
- 数字证书及CA的扫盲介绍
★ 先说一个通俗的例子 考虑到证书体系的相关知识比较枯燥.晦涩.俺先拿一个通俗的例子来说事儿. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 ...
- ansible笔记(12):handlers的用法
ansible笔记():handlers的用法 这篇文章会介绍playbook中handlers的用法. 在开始介绍之前,我们先来描述一个工作场景: 当我们修改了某些程序的配置文件以后,有可能需要重启 ...