package qs.javase.iotest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.RandomAccessFile;
public class Test {
public static void main(String[] args) {
try {
BufferedInputStream in = new BufferedInputStream(new
FileInputStream(new File("/home/qs/test/test.java")));
byte[] buf = new byte[1024 * 2];
in.read(buf, 0, buf.length);
System.out.println(new String(buf));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
//
public static void showAvailableBytes(InputStream is) {
try {
System.out.println("当前字节输入流中的字节数为:" + is.available());
} catch (IOException e) {
e.printStackTrace();
}
}
// 以字节为单位读取文件, 常用于读取二进制文本, 如图片, 声音, 影像等文件。
public static void readFileByBytes(String fileName) throws IOException {
File f = new File(fileName);
InputStream is = null;
try {
System.out.println("以字节为单位读取文件内容, 一次读一个字节: ");
is = new FileInputStream(f);
int tempByte;
while ((tempByte = is.read()) != -1) {
System.out.println(tempByte);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字节为单位读取文件内容, 一次读取多个字节: ");
byte[] tempBytes = new byte[200];
int byteRead = 0;
is = new FileInputStream(fileName);
Test.showAvailableBytes(is);
while ((byteRead = is.read(tempBytes)) != -1) {
System.out.write(tempBytes, 0, byteRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 以字符为单位读取文件, 常用语读取文本, 数字等类型的文件
public static void readFileByChars(String fileName) throws IOException {
File f = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符问单位读取文件内容, 一次读一个字符");
reader = new InputStreamReader(new FileInputStream(f));
int tempChar;
while ((tempChar = reader.read()) != -1) {
if (((char)tempChar) != '\r') {
System.out.println((char)tempChar);
}
}
reader.close();
} catch(Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符为单位读取文件内容, 一次读多个字符:");
char[] tempChars = new char[32];
int charRead = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while ((charRead = reader.read(tempChars)) != -1) {
// 屏蔽 \r 不显示
if ((charRead == tempChars.length)
&& (tempChars[tempChars.length - 1] != '\r')) {
System.out.println(tempChars);
} else {
for (int i = 0; i < charRead; i++) {
if (tempChars[i] == '\r')
continue;
else {
System.out.print(tempChars[i]);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
// 以行为单位读取文件, 常用语读取面向行的格式化文件
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader br = null;
try {
System.out.println("以行为单位读取文件内容, 一次读取一整行: ");
br = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行, 直到读入null为文件结束
while ((tempString = br.readLine()) != null) {
System.out.println("line " + line + ": " + tempString);
line++;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
}
}
}
}
// 随机读取文件
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile rfile = null;
try {
System.out.println("随机读取一段文件内容:");
rfile = new RandomAccessFile(fileName, "r");
long fileLength = rfile.length();
int beginIndex = fileLength > 4 ? 4 : 0;
rfile.seek(beginIndex);
byte[] buf = new byte[10];
int byteRead = 0;
while ((byteRead = rfile.read(buf)) != -1) {
System.out.write(buf, 0, byteRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (rfile != null) {
try {
rfile.close();
} catch (IOException e) {
}
}
}
}
}
Java IO
最新推荐文章于 2024-03-05 14:57:45 发布