复习了java IO操作,总结以便备忘。
package threeDay;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TestFile2 {
/**
* 文件操作
* @param args
*/
public static void main(String[] args){
String fileName = "d:\\test.txt";
String contents = "1234567890\n";
System.out.println("二进制形式读取文件----开始");
readByBytes(fileName);
System.out.println("二进制形式读取文件----结束");
System.out.println("以字符形式读入文件,一次对入一个字符----开始");
readByCharter(fileName);
System.out.println("以字符形式读入文件,一次对入一个字符----结束");
System.out.println("以字符形式读入文件,一次对入一行字符----开始");
readLineByCharter(fileName);
System.out.println("以字符形式读入文件,一次对入一行字符----结束");
System.out.println("将字符内容追加到文件末尾----开始");
writeContentsByAppend(fileName,contents);
System.out.println("将字符内容追加到文件末尾----结尾");
System.out.println("将字符内容覆盖原文件----开始");
writeContentsByClear(fileName,contents);
System.out.println("将字符内容覆盖原文件----结尾");
}
/**
* 以二进制形式读取文件
* @param fileName
*/
public static void readByBytes(String fileName) {
InputStream in = null;
try {
in = new FileInputStream(fileName);
int i;
while ((i = in.read()) != -1) {
System.out.print((char) i);
}
System.out.println("\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 以字符形式读入文件一次读入一个
* @param fileName
*/
public static void readByCharter(String fileName) {
InputStream in = null;
InputStreamReader reader = null;
try {
in = new FileInputStream(fileName);
reader = new InputStreamReader(in);
int i;
while ((i = reader.read()) != -1) {
System.out.print((char) i);
}
System.out.println("\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 以字符形式一次读入一行
* @param fileName
*/
public static void readLineByCharter(String fileName) {
InputStream in = null;
BufferedReader reader = null;
try {
in = new FileInputStream(fileName);
reader = new BufferedReader(new InputStreamReader(in));
String str = null;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 将内容追加到文件末尾
* @param fileName
* @param contents
*/
public static void writeContentsByAppend(String fileName,String contents){
FileWriter writer = null;
try {
writer = new FileWriter(fileName, true);
writer.write("\n"+contents);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 将内容覆盖到原文件
* @param fileName
* @param contents
*/
public static void writeContentsByClear(String fileName,String contents){
FileWriter writer = null;
try {
writer = new FileWriter(fileName, false);
writer.write("\n"+contents);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}