java io例子_java IO实例

import java.io.*;

/**

* Created by CLY on 2017/7/23.

*/

public class Main {

public static void main(String[] arg){

testFile();

testFilenameFilter();

testInputStream();

testOutStream();

testCopyImg();

}

/**

* File类:

* 能代表一个特定文件的“名称”,也能代表“一个目录下”的一组文件的“名称”。

*/

public static void testFile(){

//作为一个“目录列表器”使用

File file = new File("./");//当前目录

String list_url[] = file.list();

for (String one_url : list_url) {

System.out.println(one_url);

}

}

/**

* DirFilter接口:

* 该接口只规定了一个accept()方法。

* 我们可以使用匿名内部类的方式重写该方法。

* 该方法的作用为:判断传入的string 文件名,是否存在于传入的目录列表中

*/

public static void testFilenameFilter(){

File file = new File("./");

FilenameFilter filenameFilter = new FilenameFilter(){

public boolean accept(File dir, String name) {

for (String file_name :

dir.list()) {

if (file_name.equals(name))

return true;

}

return false;

}

};

//查看当前目录下是否存在"pom.xml"这个文件

boolean result = filenameFilter.accept(file, "pom.xml");

System.out.println(result);

}

/**

* 所有的流都是站在“内存”的角度说的。

* 如“输入流”,指的就是将其他媒介的流“输入到本机内存的流中(这个本机内存的流就是输入流)”。

* 然后用户再从“内存中的”输入流将数据读出来。

*

* 输出流的相关操作为:“将内存中的流(这个流指的就是输出流)”传到其他媒介的流中,

* 而我们需要做的,自然就是将数据“写”到内存的输入流中去,至于它是怎么传走的我们不用管。

*/

/**

* InputStream和Reader。

*

* InputStream是所有“字节”输入流相关类的超类。

* InputStream接口中的read方法提供“对字节的读取(byte)”

*

* Reader是所有用于“读取字符流”的相关类的超类

* Reader接口中的read方法提供“对字符的读取(char数组或者String)”

*

* 而我们操作时自然是操作字符更顺畅。

* 实际上InputStream的read方法一般也不是给开发者用的,而是给其他类使用的。

* 所以可以看出,整个流程为:

* 先获取到InputStream接口类型的“字节流”数据。

* 然后转化成reader接口类型的“字符流”数据。

* 最后调用reader接口提供的reader方法读出字符。

*/

public static void testInputStream(){

//获取本地“文件名”的对象

File file = new File("./pom.xml");

try {

//FileInputStream是InputStream的一个子实现类,可以从指定文件中读取字节流存到内存中的“InputStream(输入字节流)”中

FileInputStream inputstream = new FileInputStream(file);

/**

* InputStreamReader是Reader的一个子实现类

* 是字节流通向字符流的桥梁,“封裝”了InputStream在里头,

* 它以较高级的方式,reader()一次读取一个一个字符,默认读取的是字符的ASCII码

*/

InputStreamReader reader = new InputStreamReader(inputstream);

int ch;

//一次读取一个字符的ASCII码,如果为-1,就表示结束了。

while ((ch = reader.read())!=-1){

System.out.print((char)ch);

}

System.out.println();

/**

* BufferedReader

* 对之前的普通的InputStreamReader的又一次“装饰”

* 提供缓冲的方式读取文本,通过readLine()方法可以一次读取一行,效率高。

*/

FileInputStream inputstream2 = new FileInputStream(file);

InputStreamReader reader2 = new InputStreamReader(inputstream2);

BufferedReader bufferedReader = new BufferedReader(reader2);

String line;

while ((line = bufferedReader.readLine())!=null){

System.out.println(line);

}

bufferedReader.close();

reader2.close();

inputstream2.close();

reader.close();

inputstream.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* OutputStream和Writer

*

* 与上面同理

* OutputStream负责将“内存中的输出流”输出,

* 然后也提供给我们一个writer方法,让我们把“字节(byte)”写入内存中的输出流。

*

* 而Writer提供的writer方法则是让我们把“字符(char数组或者String)”写入内存中的输出流。

*

* 注意上面的描述,通过write()方法只能将数据写入“内存里的输出流”(相当于缓存起来)。

* 必须得通过flush()方法,才能将所有内存中缓冲的数据发送给目的地。

*/

public static void testOutStream(){

//在当前目录创建一个新文件

File new_file =new File("./test.txt");

try {

if (!new_file.exists()){

new_file.createNewFile();

}

/**

* FileWriter的write(String)方法已经可以将字符串写入输出流了。

* 可此处通过“装饰类”BufferedWriter装饰Writter的好处是:

* 加了一个缓冲,缓冲写满后再将数据写入硬盘

* 这样做极大的提高了性能

* 如果单独使用FileWriter也可以,

* 但你每写一个数据,硬盘就有一个写动作,性能极差

*/

String out_words = "hello world";

//true表示对文件再次写入时,会在该文件的结尾续写,并不会覆盖掉。

FileWriter fileWriter = new FileWriter(new_file,true);

BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

bufferedWriter.write(out_words);

bufferedWriter.flush();

bufferedWriter.close();

fileWriter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 实例:

* 依靠字节流传输复制图片

*/

public static void testCopyImg(){

//待复制的文件地址

File file_old = new File("./old.jpg");

//新文件地址

File file_new = new File("./new.jpg");

//读取文件到输入字符流中,然后再从输入字符流写入输出字符流,然后由输出字符流写到目标文件

try {

if (!file_new.exists()){

file_new.createNewFile();

}

FileInputStream fileInputStream = new FileInputStream(file_old);

FileOutputStream fileOutputStream = new FileOutputStream(file_new);

//创建缓存字节数组

byte data[] = new byte[1024];

// 一次性读入1024个字节,即读入到字节数组中,然后再将数组写入输出流

while ((fileInputStream.read(data))!=-1){

fileOutputStream.write(data);

fileOutputStream.flush();

}

// 依次 关闭流(先开后关,后开先关)

fileOutputStream.close();

fileInputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值