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 = ...
随机推荐
- react实现极简搜索框效果
hover.css内容 * { margin:; padding:; } li.hover { background: #ccc; color: darkgreen; } js内容 import Re ...
- Setup Objective UI with UMG
创建UI蓝图控件 拖入一个文本框 新建一个Actor,继承自FPSHUD 创建控件,并显示到界面上 新建一个Actor,继承FPSGameMode 将属性里的HUD更改为之前创建的 在世界设置中,将G ...
- java String、StringBuffer、StringBuild、StringTokenizer
StringBuffer线程安全.StringBuilder线程不安全 效率: StringBuilder(线程不安全) > StringBuffer(线程安全) > String Str ...
- GridView item设置点击背景
GridView item设置点击背景 android:listSelector="@android:color/transparent"
- 数字图像处理的Matlab实现(3)—灰度变换与空间滤波
第3章 灰度变换与空间滤波(1) 3.1 简介 空间域指的是图像平面本身,这类方法是以对图像像素直接处理为基础的.本章主要讨论两种空间域处理方法:亮度(灰度)变换与空间滤波.后一种方法有时涉及到邻域处 ...
- Python os.walk文件遍历用法【转】
python中os.walk是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 1.载入 要使用os.walk,首先要载入该函数 可以使用以下两种方法 import os ...
- SharePoint 2013 报错 异常来自 HRESULT:0X80131904
直接上传文件,报错:该Url xxxx无效.它可能指向不存在的文件或文件夹,或者是执行不再当前网站中的有效文件或文件夹. 直接新建文件夹,报错:异常来自 HRESULT:0X80131904 以系统账 ...
- 范数(norm) 几种范数的简单介绍
原文地址:https://blog.csdn.net/a493823882/article/details/80569888 我们知道距离的定义是一个宽泛的概念,只要满足非负.自反.三角不等式就可以称 ...
- Vue 实战项目开发流程
一 基础配备 ## 一.环境搭建 #### 1.安装node - 去[官网](https://nodejs.org/zh-cn/)下载node安装包 - 傻瓜式安装 - 万一安装后终端没有node环境 ...
- python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用
python操作mongodb数据库①mongodb的安装和简单使用 参考文档:中文版:http://www.mongoing.com/docs/crud.html英文版:https://docs.m ...