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 = ...
随机推荐
- Tip:什么是JavaBean
可视化JavaBean 非可视化JavaBean(分:值JavaBean和工具JavaBean) JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参 ...
- 解释下面URL
解释下面URL各部分的含义 a.duke.csc.villanova.edu/jss/examles.html duke是计算机名,该计算机属于villanova.edu域的csc子域.edu是最高级 ...
- pythonの信号量
#!/usr/bin/env python import threading,time def run(n): # 申请锁 semaphore.acquire() time.sleep(1) prin ...
- 2018 Multi-University Training Contest 2 杭电多校第二场
开始逐渐习惯被多校虐orz 菜是原罪 1004 Game (hdoj 6312) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 虽然披着 ...
- LwIP Application Developers Manual9---LwIP and multithreading
1.前言 lwIP的内核并不是线程安全的.如果我们必须在多线程环境里使用lwIP,那么我们必须使用“upper”API层的函数(netconn或sockets).当使用raw API时,你需要自己保护 ...
- 【Boost】boost::tokenizer详解
分类: [C++]--[Boost]2012-12-28 21:42 2343人阅读 评论(0) 收藏 举报 目录(?)[+] tokenizer 库提供预定义好的四个分词对象, 其中char ...
- 如何确定windows启动类型是bios还是uefi
原文地址:http://www.kqidong.com/bios/3728.html 如何确定windows启动类型是bios还是uefi?随着装机越来越简单,大家对安装系统充满信心,但是了解到启动类 ...
- jquery简单使用入门
<!DOCTYPE html> <html> <head> <title>jquery</title> <meta charset=& ...
- Scientific Toolworks Understand
Scientific Toolworks Understand是一款定位于代码阅读的软件.界面用Qt开发的. 软件特性: 1.支持多语言:Ada, C, C++, C#, Java, FORTRAN, ...
- 前端-----JavaScript 初识基础
JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:操作网页上的 ...