【java开发系列】—— java输入输出流

时间:2023-03-08 22:30:28

前言

  任何语言输入输出流都是很重要的部分,比如从一个文件读入内容,进行分析,或者输出到另一个文件等等,都需要文件流的操作。这里简单介绍下reader,wirter,inputstream,outputstream的使用方法。其实Apache commons里面有个方法IOUtils可是实现方便快捷的流拷贝,感兴趣的可以参考官方文档。

  JAVA的输入输出流有两种,一种是字节流(InPutStream,OutPutStream),一种是字符流(Reader,Writer)。

  字节流是普遍适用的,比如我们读取一个视频,音乐,或者文本都可以用这种方式。

  字符流只能读取类似文本这种文件。那么它们之间是什么关系呢?看下面这张图吧!【java开发系列】—— java输入输出流

  大致可以看到它们之间的关系,我们可以使用InPutStreamReader来实现字节流到字符流的转换。比如

Reader reader = new InputStreamReader(new FileInputStream(fileName));

  也可以使用OutPutStreamWriter来实现字节流到字符流的转换,如

Writer writer = new OutputStreamWriter(new FileOutputStream(filePathName));

  下面简单介绍下,文件读取和文件写入的样例!

  按行读取文件!

 /**
* 以行为单位读取文件,常用于读面向行的格式化文件
*
* @paramfileName:文件名
*/
public static List<String> readFileByLines(String fileName) {
List<String> list = new ArrayList<String>();
if (fileName != null && !"".equals(fileName)) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
/* 一次读入一行,直到读入null为文件结束 */
while ((tempString = reader.readLine()) != null) {
System.out.println(tempString);
list.add(tempString);
}
} catch (IOException e) {
System.out.println("读取文本文件异常" + e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
System.out.println("读取文本文件异常" + e1);
}
}
}
}
return list;
}

  向文件中写入内容,直接覆盖掉原来的内容。

 /**
* 把内容写到文件
*
* @paramfilePathName文件名
* @paramList<String>文件内容
*/
public static boolean writerFile(String filePathName, String content) {
boolean flag = false;
OutputStreamWriter osw = null;
try {
if (filePathName != null && !"".equals(filePathName)) {
osw = new OutputStreamWriter(new FileOutputStream(filePathName));
}
} catch (FileNotFoundException e1) {
flag = false;
e1.printStackTrace();
}
if (osw != null) {
BufferedWriter bw = new BufferedWriter(osw);
try {
if (content != null && !"".equals(content)) {
bw.write(content);
flag = true;
}
} catch (IOException e) {
flag = false;
e.printStackTrace();
} finally {
try {
bw.close();
osw.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
}
}
}
return flag;
}

  向文件中追加内容,追加到末尾。

 /**
* 把内容写到文件或追加到文件中
*
* @paramfilePathName文件名
* @paramList<String>文件内容
*/
public static boolean writerFileIsAppend(String filePathName, String content) {
boolean flag = false;
OutputStreamWriter osw = null;
try {
if (filePathName != null && !"".equals(filePathName)) {
osw = new OutputStreamWriter(new FileOutputStream(filePathName,
true));
}
} catch (Exception e1) {
flag = false;
e1.printStackTrace();
}
if (osw != null) {
BufferedWriter bw = new BufferedWriter(osw);
try {
if (content != null && !"".equals(content)) {
bw.write(content);
flag = true;
}
} catch (IOException e) {
flag = false;
e.printStackTrace();
} finally {
try {
bw.close();
osw.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
}
}
}
return flag;
}

  全部代码

  

 package testIO;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List; public class testIO {
public static void main(String[] args) {
readFileByBytes("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
readFileByChars("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
readFileByLines("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
writerFile("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt",
"BufferedWriter");
writerFileIsAppend("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt",
"Append");
} /**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*
* @paramfileName:文件的名
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
/* 一次读多个字节 */
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(file);
/* 读入多个字节到字节数组中,byteread为一次读入的字节数 */
while ((byteread = in.read(tempbytes)) != -1) {
for (byte b : tempbytes) {
System.out.println((char) b);
}
System.out.println(byteread);
}
} catch (Exception e1) {
System.out.println("读取文本文件异常" + e1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
System.out.println("读取文本文件异常" + e1);
}
}
}
} /**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*
* @paramfileName:文件名
*/
public static void readFileByChars(String fileName) {
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
/* 一次读多个字符 */
char[] tempchars = new char[100];
int charread = 0;
if (fileName != null && !"".equals(fileName)) {
reader = new InputStreamReader(new FileInputStream(fileName));
/* 读入多个字符到字符数组中,charread为一次读取字符数 */
while ((charread = reader.read(tempchars)) != -1) {
for (char c : tempchars) {
System.out.println(c);
}
}
}
} catch (Exception e1) {
System.out.println("读取文本文件异常" + e1);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
System.out.println("读取文本文件异常" + e1);
}
}
}
} /**
* 以行为单位读取文件,常用于读面向行的格式化文件
*
* @paramfileName:文件名
*/
public static List<String> readFileByLines(String fileName) {
List<String> list = new ArrayList<String>();
if (fileName != null && !"".equals(fileName)) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
/* 一次读入一行,直到读入null为文件结束 */
while ((tempString = reader.readLine()) != null) {
System.out.println(tempString);
list.add(tempString);
}
} catch (IOException e) {
System.out.println("读取文本文件异常" + e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
System.out.println("读取文本文件异常" + e1);
}
}
}
}
return list;
} /**
* 把内容写到文件
*
* @paramfilePathName文件名
* @paramList<String>文件内容
*/
public static boolean writerFile(String filePathName, String content) {
boolean flag = false;
OutputStreamWriter osw = null;
try {
if (filePathName != null && !"".equals(filePathName)) {
osw = new OutputStreamWriter(new FileOutputStream(filePathName));
}
} catch (FileNotFoundException e1) {
flag = false;
e1.printStackTrace();
}
if (osw != null) {
BufferedWriter bw = new BufferedWriter(osw);
try {
if (content != null && !"".equals(content)) {
bw.write(content);
flag = true;
}
} catch (IOException e) {
flag = false;
e.printStackTrace();
} finally {
try {
bw.close();
osw.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
}
}
}
return flag;
} /**
* 把内容写到文件或追加到文件中
*
* @paramfilePathName文件名
* @paramList<String>文件内容
*/
public static boolean writerFileIsAppend(String filePathName, String content) {
boolean flag = false;
OutputStreamWriter osw = null;
try {
if (filePathName != null && !"".equals(filePathName)) {
osw = new OutputStreamWriter(new FileOutputStream(filePathName,
true));
}
} catch (Exception e1) {
flag = false;
e1.printStackTrace();
}
if (osw != null) {
BufferedWriter bw = new BufferedWriter(osw);
try {
if (content != null && !"".equals(content)) {
bw.write(content);
flag = true;
}
} catch (IOException e) {
flag = false;
e.printStackTrace();
} finally {
try {
bw.close();
osw.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
}
}
}
return flag;
}
}

  内容参考

http://www.2cto.com/kf/201206/136072.html

http://blog.****.net/liuhenghui5201/article/details/8292552