java读写文件_java读取文件与写文件

前言

java中io流一直是一个令人头疼的东西,主要是各种流,各种缓冲器太多,不好记忆,所以感觉学起来很费劲,选择起来也比较烦恼。

本文主要针对java的io流读取数据使用io流读取文件和向文件中写数据,并根据个人经验讲解应该怎么选择和使用。

需要的知识点:

1.字节流和字符流选择

字节流:字节流读取的时候,读到一个字节就返回一个字节;  主要用于读取图片,MP3,AVI视频文件。

字符流:字符流使用了字节流读到一个或多个字节,如读取中文时,就会一次读取2个字节。只要是处理纯文本数据,就要优先考虑使用字符流。

31830

2.处理流和节点流

节点流:和操作系统紧密连接的流。该层的与文件输入输出的操作都比较原始,没有进过优化,功能比较单一。

(FileReader,FileWriter和FileInputStream,FileOutStream都是输入节点流。)

处理流:该层流是对节点流的封装,对节点流中进行优化,加入缓冲器,提供更加丰富的API等。我们一般都使用处理流,在创建处理流对象的时候一般都需要节点流对象作为其构造参数。

(BufferedInPutStream,BufferedOutPutStream和BufferedWriter,BufferedReader)

对于这种带缓冲器的处理流与对应的节点流的最大区别可以参考:http://fjding.iteye.com/blog/2311233

本文重点讲解文件的读取和输出。

1. 文件的读取(节点流FileInputStream读取字节流)

public static void readFileByBytes(String fileName) {

// 一般先创建file对象

FileInputStream fileInput = null;

try {

File file = new File(fileName);

if (!file.exists()) {

file.createNewFile();

}

byte[] buffer = new byte[1024];

fileInput = new FileInputStream(file);

int byteread = 0;

// byteread表示一次读取到buffers中的数量。

while ((byteread = fileInput.read(buffer)) != -1) {

System.out.write(buffer, 0, byteread);

}

} catch (Exception e) {

// TODO: handle exception

} finally {

try {

if (fileInput != null) {

fileInput.close();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

2. 文件读取(节点流FileReader读取字符流)

public static void readFileByChars(String fileName) {

FileReader reader = null;

try {

File file = new File(fileName);

if (!file.exists()) {

file.createNewFile();

}

reader = new FileReader(file);

char[] buffer = new char[1024];

int charread = 0;

while ((charread = reader.read(buffer)) != -1) {

System.out.print(buffer);

}

} catch (IOException e) {

// TODO: handle exception

} finally {

try {

if (reader != null) {

reader.close();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

其实FileReader也是需要FileInputStream对象转换的,要借助于InputStreamReader转换流。

3.文件的读取通过BufferedReader读取数据。

public static void readByBufferedReader(String fileName) {

try {

File file = new File(fileName);

// 读取文件,并且以utf-8的形式写出去

BufferedReader bufread;

String read;

bufread = new BufferedReader(new FileReader(file));

while ((read = bufread.readLine()) != null) {

System.out.println(read);

}

bufread.close();

} catch (FileNotFoundException ex) {

ex.printStackTrace();

} catch (IOException ex) {

ex.printStackTrace();

}

}

4.向文件中写入数据(字节流FileOutputStream)

public void writeByFileOutputStream() {

FileOutputStream fop = null;

File file;

String content = "This is the text content";

try {

file = new File("c:/newfile.txt");

fop = new FileOutputStream(file);

// if file doesnt exists, then create it

if (!file.exists()) {

file.createNewFile();

}

// get the content in bytes

byte[] contentInBytes = content.getBytes();

fop.write(contentInBytes);

fop.flush();

fop.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (fop != null) {

fop.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

5. 向文件中写数据(FileReader)【注意使用FileReader(“path”,true)可以往文件后面追加内容,否则就直接覆盖了】

public static void writeByFileReader() {

try {

String data = " This content will append to the end of the file";

File file = new File("javaio-appendfile.txt");

// if file doesnt exists, then create it

if (!file.exists()) {

file.createNewFile();

}

// true = append file

FileWriter fileWritter = new FileWriter(file.getName(), true);

fileWritter.write(data);

fileWritter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

6.采用BufferedWriter向文件中写数据

public static void writeByBufferedReader() {

try {

String content = "This is the content to write into file";

File file = new File("/users/mkyong/filename.txt");

// if file doesnt exists, then create it

if (!file.exists()) {

file.createNewFile();

}

FileWriter fw = new FileWriter(file, true);

BufferedWriter bw = new BufferedWriter(fw);

bw.write(content);

bw.flush();

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

总结:

对于我来说,一般使用io来操作文件比较多一些,一般都喜欢用BufferedReader和BufferedWriter,使用它们的一般流程是,先创建File对象(可以对路径和文件进行更多的操作),然后通过File对象创建FileReader(FileWriter)【当然在这里也可以通过创建InputStreamReader(new InputStream)来获取reader对象,看个人爱好】,然后通过FileReader(FileWriter)获得BufferedReader(BufferedWriter)对象。对于读入文件,还可以使用Scanner对读取的文件进行操作,这个比较方便,Scanner也是处理流,因此也是需要节点流作为其参数的。

参考:

http://blog.csdn.net/yuebinghaoyuan/article/details/7388059

http://www.docin.com/p-747385065.html

http://blog.csdn.net/zzp_403184692/article/details/8057693

http://www.jb51.net/article/47062.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值