【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了TextReadFile.java相关资料,希望对各位网友有所帮助!
package com.zf.s10;//创建一个包
import java.io.BufferedReader;//引入类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class TextReadFile {//操作运用多种方式读取文件的类
public static void readFileByBytes(String fileName){//以字节为单位读取文件
File file = new File(fileName);//创建文件
InputStream in = null;
try {
System.out.println("①.以字节为单位读取文件内容,一次读一个字节:");
in = new FileInputStream(file);//将文件放入文件输入流中
int tempbyte;
while ((tempbyte = in.read()) != -1){ //一次读一个字节,循环将内容读出来
System.out.write(tempbyte);
}
in.close();//关闭文件输入流
} catch (IOException e) {//捕获异常
e.printStackTrace();
return;
}
try {
System.out.println("②.以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];//声明长度为100的字节数组
int byteread = 0;
in = new FileInputStream(fileName);//将文件放入文件输入流中
TextReadFile.showAvailableBytes(in);////显示输入流中还剩的字节数
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1){//一次读多个字节,循环将内容读出来
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {//捕获异常
e1.printStackTrace();
} finally {//内容总执行
if (in != null) {
try {
in.close();//确保文件输入流关闭
} catch (IOException e1) {
}
}
}
}
public static void readFileByChars(String fileName){//以字符为单位读取文件
File file = new File(fileName);//创建文件
Reader read = null;
try {
System.out.println("①.以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
read = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = read.read()) != -1) {
// 对于windows下,rn这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。
if (((char) tempchar) != 'r') {//只要是不换行就读取
System.out.print((char) tempchar);
}
}
read.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("②.以字符为单位读取文件内容,一次读多个字节:");
char[] tempchars = new char[30];
int charread = 0;
read = new InputStreamReader(new FileInputStream(fileName));//创建文件读入流
while ((charread = read.read(tempchars)) != -1) {// 一次读多个字符
// 同样屏蔽掉r不显示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != 'r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == 'r') {
continue;//停止执行当前的迭代,然后退回循环开始处
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {//捕获异常
e1.printStackTrace();
} finally {//内容总执行
if (read != null) {
try {
read.close();//确保关闭
} catch (IOException e1) {
}
}
}
}
public static void readFileByLines(String fileName){//以行为单位读取文件
File file = new File(fileName);
BufferedReader reader = null;//创建缓存读取
try {
System.out.println("①.以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));//将文件放在缓存读取中
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null){
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {//捕获异常
e.printStackTrace();
} finally {//内容总执行
if (reader != null) {
try {
reader.close();//关闭缓存读取
} catch (IOException e1) {
}
}
}
}
private static void showAvailableBytes(InputStream in){//显示输入流中还剩的字节数
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {//捕获异常
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "F:/poem.txt";
System.out.println("1.按字节为单位读取文件:");
readFileByBytes(fileName);
System.out.println("2.按字符为单位读取文件:");
readFileByChars(fileName);
System.out.println("3.按行为单位读取文件:");
readFileByLines(fileName);
}
}
上一篇:
TextRandomAccess.java
下一篇:
天亮之后