IO流的具体案例

来源:互联网 发布:数据透视表显示合计 编辑:程序博客网 时间:2024/06/05 18:47
public class IOUtils {
/**
 * 以字符流,按行读取的方式,读取一个文件并输出
 * 
 * BufferedReader
 * 
 * FileReader
 * 
 * FileInputStream
 */

public static void readFileByBufferedReaderLine(File file){
//目的:读取本地文件中的数据,打印到控制台显示
/*
* 1、从本地文件中读取数据到内存中:需要用到输入流,但是此案例,要求以字符为单位读取数据内容
* 所以用到:字符输入流
* 2、将读取到内存中的数据,通过控制台打印出来,涉及到输出流。
*/

//声明一个BufferedReader类型变量
BufferedReader reader = null;
String tempString = null;

try {
//实例化该变量,需要传入参数Reader类型的对象
reader = new BufferedReader(new FileReader(file));

//使用Reader来读取文件内容,按行读取,一次读取一行
while((tempString = reader.readLine()) != null){
System.out.println(tempString);
}

reader.close();
//FileNotFoundException 捕获的是new FileReader(file)这里面的异常
} catch (FileNotFoundException e) {
e.printStackTrace();
//IOException 捕获的是 reader.readLine()这个方法里的异常
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 字符流以字符为单位读取文件
* InputStreamReader

* FileInputStream
*/
public static void readFileByStreamReader(File file){
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(file));
int tempChar;
while((tempChar = reader.read()) != -1){
System.out.print((char)tempChar);
}

reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 字节流,以字节为单位,读取文件

* FileInputStream
*/

public static void readFileByFileInputStream(File file){
InputStream inputStream = null;
byte[] tempByte = new byte[1024];

try {
int byteRead;
inputStream = new FileInputStream(file);
while((byteRead = inputStream.read(tempByte)) != -1){
String tempString = new String(tempByte, 0, tempByte.length);
System.out.println(tempString);
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 以文件字节流写入文件

* FileOutputStream
*/
public static void writeByFileOutputStream(File file, String content){
FileOutputStream fileOutputStream = null;

try {
//如下构造方法FileOutputStream如果第二个参数省略,则默认为false,即:不追加写入。直接覆盖原文件中的内容
fileOutputStream = new FileOutputStream(file, true);
byte[] contents = content.getBytes();
fileOutputStream.write(contents);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 以字符流的方式写入文件
*/
public static void writeByBufferedWriter(File file, String content){
BufferedWriter bufferedWriter = null;

try {
//如下构造方法FileWriter如果第二个参数省略,则默认为false,即:不追加写入。
// 直接覆盖原文件中的内容
bufferedWriter = new BufferedWriter(new FileWriter(file, true));
// char[] contents = content.toCharArray();
// bufferedWriter.write(contents);
bufferedWriter.write(content);
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 文件的复制
* 1、涉及到输入流
* 2、输出流
*/

public static void copyFile(File oldFile, File newFile){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;

byte[] readBytes = new byte[1024];
long length = 0;
int curLength;

try {
fileInputStream = new FileInputStream(oldFile);
fileOutputStream = new FileOutputStream(newFile);

while((curLength = fileInputStream.read(readBytes)) != -1){
fileOutputStream.write(readBytes, 0, curLength);
length += curLength;
}

System.out.println("文件大小:" + length);
fileOutputStream.flush();

fileInputStream.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
0 0