常用的I/O流:
1.字节流:
/**
* 输出字节流--写操作
*
* @param filePath
*/
public static void fileOutputStreamTest(String filePath) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
String str = "测试FileOutputStream";
fos.write(str.getBytes(StandardCharsets.UTF_8));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 输入字节流--读操作
*
* @param filePath
*/
public static void fileInputStreamTest(String filePath) {
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
byte[] bytes = new byte[1024];
int len = 0;
StringBuffer buffer = new StringBuffer();
while ((len = fis.read(bytes)) != -1) {
buffer.append(new String(bytes));
}
System.out.println(buffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 使用字节流--复制文件
*
* @param srcFile
* @param destFile
*/
public static void fileCopy(String srcFile, String destFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 缓冲字节输出流--读操作
*
* @param filePath
*/
public static void bufferedInputStreamTest(String filePath) {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(filePath);
bis = new BufferedInputStream(fis);
byte[] bytes = new byte[1024];
int len = 0;
StringBuffer buffer = new StringBuffer();
while ((len = bis.read(bytes)) != -1) {
buffer.append(new String(bytes));
}
System.out.println(buffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 缓冲字节输出流--写操作
*
* @param filePath
*/
public static void bufferedOutputStreamTest(String filePath) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(filePath);
bos = new BufferedOutputStream(fos);
String str = "测试BufferedOutputStream";
bos.write(str.getBytes(StandardCharsets.UTF_8));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.字符流:
/**
* 字符输入流--读操作
*
* @param filePath
*/
public static void fileReaderTest(String filePath) {
FileReader fr = null;
try {
fr = new FileReader(filePath);
char[] chars = new char[1024];
int len = 0;
StringBuffer buffer = new StringBuffer();
while ((len = fr.read(chars)) != -1) {
buffer.append(new String(chars));
}
System.out.println(buffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 字符输出流--写操作
*
* @param filePath
*/
public static void fileWriterTest(String filePath) {
FileWriter fw = null;
try {
fw = new FileWriter(filePath);
String str = "测试FileWriter";
fw.write(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 缓冲字符输入流--读操作
*
* @param filePath
*/
public static void bufferedReaderTest(String filePath) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(filePath);
br = new BufferedReader(fr);
StringBuffer buffer = new StringBuffer();
String res = null;
while ((res = br.readLine()) != null) {
buffer.append(res);
}
System.out.println(buffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 缓存字符输出流--写操作
*
* @param filePath
*/
public static void bufferedWriterTest(String filePath) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(filePath);
bw = new BufferedWriter(fw);
String str = "测试FileWriter写入内容";
bw.write(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.字节流转换为字符流:
/**
* 字节输入流--转换--字符输出流 ---读操作
*
* @param filePath
*/
public static void inputStreamReaderTest(String filePath) {
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fis = new FileInputStream(filePath);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String res = null;
StringBuffer buffer = new StringBuffer();
while ((res = br.readLine()) != null) {
buffer.append(res);
}
System.out.println(buffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
isr.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 字节输出流--转换--字符输出流--写操作
*
* @param filePath
*/
public static void outputStreamWriterTest(String filePath) {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
fos = new FileOutputStream(filePath);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
String str = "测试OutputStreamWriter";
bw.write(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意事项:
字节流:处理音频文件、图片、歌曲比较好,不会导致文件损坏,如使用字符流处理则会出现文件损坏。
字符流:处理word文档,文本类文件。