对于java文件读取一直比较迷糊,整理了下,日后可以直接翻看。
package baixiaosheng;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class JavaReadAndWriteFile {
private static String filePath = "E:/learn/test.txt";
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件
*
* @param fileName
*/
public static void readFileByBytes(String filePath) {
InputStream inputStream = null;
/*
* try { System.out.println("以字节为单位读取文件内容,一次读取一个字节:"); inputStream = new
* FileInputStream(new File(fileName)); int tempbyte; while ((tempbyte =
* inputStream.read()) != -1) { System.out.write(tempbyte); }
* inputStream.close(); } catch (IOException e) { e.printStackTrace(); }
*/
try {
System.out.println("以字节为单位读取文件内容,一次读取多个字节:");
inputStream = new FileInputStream(new File(filePath));
byte[] tempbytes = new byte[100];
int byteNumRead = 0;
while ((byteNumRead = inputStream.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteNumRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println();
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*
* @param fileName
*/
public static void readFileByChars(String filePath) {
Reader reader = null;
/*try {
System.out.println("以字符为单位读取文件内容,一次读一个字符:");
reader = new InputStreamReader(new FileInputStream(new File(
filePath)));
int tempChar = 0;
while ((tempChar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行; 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if((char)tempChar != '\r'){
System.out.print((char)tempChar);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}*/
try {
System.out.println("以字符为单位读取文件内容,一次读多个字符:");
reader = new InputStreamReader(new FileInputStream(new File(
filePath)));
char[] tempchars = new char[100];
int charNumRead = 0;
// 读入多个字符到字符数组中,charNumRead为一次读取字符数
while ((charNumRead = reader.read(tempchars)) != -1) {
if(charNumRead==tempchars.length && (tempchars[tempchars.length - 1] != '\r')){
System.out.println(tempchars);
}else {
for(char c:tempchars){
if(c == '\r'){
continue;
}else {
System.out.print(c);
}
}
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
* @param filePath
*/
public static void readFileByLines(String filePath) {
BufferedReader bufferedReader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
bufferedReader = new BufferedReader(new FileReader(new File(
filePath)));
String tempString;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = bufferedReader.readLine()) != null) {
System.out.println("第" + line + "行内容:" + tempString);
line++;
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e1) {}
}
}
}
/**
* 写内容到文件
* @param filePath
* @param content
*/
public static void writeContenToFile(String filePath,String content){
FileWriter fileWriter = null;
try {
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
fileWriter = new FileWriter(filePath,true);
fileWriter.append(content);
System.out.println("文件写入成功...请打开文件查看.");
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fileWriter != null){
try {
fileWriter.close();
} catch (IOException e) {}
}
}
}
public static void main(String[] args) {
JavaReadAndWriteFile.readFileByBytes(filePath);
JavaReadAndWriteFile.readFileByChars(filePath);
JavaReadAndWriteFile.readFileByLines(filePath);
JavaReadAndWriteFile.writeContenToFile(filePath, " zyb");
}
}
看到的另外一个大神对java io流的整理挺好的,文章标题:深入理解JAVA中的IO