package io; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer; public class test { public static void main(String[] args) { // testFileOutputStream(); System.out.println("**********这是一个分隔符**********"); // testFileInputStream(); System.out.println("**********这是一个分隔符**********"); // testBufferedOutputStream(); System.out.println("**********这是一个分隔符**********"); // testBufferedInputStream(); System.out.println("**********这是一个分隔符**********"); // testFileWriter(); System.out.println("**********这是一个分隔符**********"); // testFileWriterAppend(); System.out.println("**********这是一个分隔符**********"); // testFileReader(); System.out.println("**********这是一个分隔符**********"); // testBufferedWriter(); System.out.println("**********这是一个分隔符**********"); // testBufferedWriterNewLineAppend(); System.out.println("**********这是一个分隔符**********"); // testBufferedWriterAppend(); System.out.println("**********这是一个分隔符**********"); // testBufferedReader(); System.out.println("**********这是一个分隔符**********"); // testBufferedReaderReadLine(); } /**
* 缓冲输入字符流
*/
private static void testBufferedReaderReadLine() { Reader r = null;
BufferedReader br = null; try { File f = new File("D:\\IO\\testBufferedReader.txt"); r = new FileReader(f);
br = new BufferedReader(r); String temp; while ((temp = br.readLine()) != null) { System.out.println(temp);// 每次读取一行,为字符串,字符串为null时,文件内容读取完毕 } } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { br.close();
r.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输入字符流
*/
private static void testBufferedReader() { Reader r = null;
BufferedReader br = null; try { File f = new File("D:\\IO\\testBufferedReader.txt"); r = new FileReader(f);
br = new BufferedReader(r); int temp;
String str = ""; while ((temp = br.read()) != -1) { str += (char) temp;// 每次读一个字符的ASCII码,转换为字符,添加为字符串,读取ASCII码为-1时,文件内容读取完毕 } System.out.println(str); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { br.close();
r.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输出字符流
*/
private static void testBufferedWriterAppend() { Writer w = null;
BufferedWriter bw = null; try { File f = new File("D:\\IO\\testBufferedWriter.txt"); w = new FileWriter(f, true);// 将新的字符串添加到文件内容末尾
bw = new BufferedWriter(w); bw.write("新增加一行\r\n"); bw.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bw.close();
w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输出字符流
*/
private static void testBufferedWriterNewLineAppend() { Writer w = null;
BufferedWriter bw = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testBufferedWriter.txt"); w = new FileWriter(f);
bw = new BufferedWriter(w); bw.write(str); bw.newLine();// 适应计算机系统的换行
bw.append("新增加一行\r\n"); bw.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bw.close();
w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输出字符流
*/
private static void testBufferedWriter() { Writer w = null;
BufferedWriter bw = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testBufferedWriter.txt"); w = new FileWriter(f);
bw = new BufferedWriter(w); bw.write(str); bw.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bw.close();
w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输入字符流
*/
private static void testFileReader() { Reader r = null; try { File f = new File("D:\\IO\\testFileReader.txt"); r = new FileReader(f); int temp;
String str = ""; while ((temp = r.read()) != -1) { str += (char) temp;// 每次读一个字符的ASCII码,转换为字符,添加为字符串,读取ASCII码为-1时,文件内容读取完毕 } System.out.println(str); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { r.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输出字符流
*/
private static void testFileWriterAppend() { Writer w = null; try { File f = new File("D:\\IO\\testFileWriter.txt"); w = new FileWriter(f, true);// 将新的字符串添加到文件内容末尾 w.write("新增加一行\r\n"); w.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输出字符流
*/
private static void testFileWriter() { Writer w = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testFileWriter.txt"); w = new FileWriter(f); w.write(str); w.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输入字节流
*/
private static void testBufferedInputStream() { InputStream is = null;
BufferedInputStream bis = null; try { File f = new File("D:\\IO\\testBufferedInputStream.txt"); is = new FileInputStream(f);
bis = new BufferedInputStream(is); byte[] b = new byte[(int) f.length()];// 获取文件的字节长度 bis.read(b); System.out.println(new String(b));// 转换成字符串输出 } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bis.close();
is.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输出字节流
*/
private static void testBufferedOutputStream() { OutputStream os = null;
BufferedOutputStream bos = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testBufferedOutputStream.txt"); os = new FileOutputStream(f);
bos = new BufferedOutputStream(os); bos.write(str.getBytes());// 获取字节数组 bos.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bos.close();
os.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输入字节流
*/
private static void testFileInputStream() { InputStream is = null; try { File f = new File("D:\\IO\\testFileInputStream.txt"); is = new FileInputStream(f); byte[] b = new byte[(int) f.length()];// 获取文件的字节长度 is.read(b); System.out.println(new String(b));// 转换成字符串输出 } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { is.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输出字节流
*/
private static void testFileOutputStream() { OutputStream os = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testFileOutputStream.txt"); os = new FileOutputStream(f); os.write(str.getBytes());// 获取字节数组 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { os.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } }