InputStream is = Thread.currentThread().getContextClassLoader() .getResourceAsStream("io/aa.txt"); String str = IOUtils.toString(is); System.out.println(str); File file = new File("fa"); FileUtils.writeStringToFile(file, str); System.out.println(FileUtils.readFileToString(file)); FileUtils.forceDeleteOnExit(file);
利用IOUTILS和FILEUTILS很方便的在FILE STRING 和 INPUTSTREAM之间进行转换java
InputStream is = Thread.currentThread().getContextClassLoader() .getResourceAsStream("io/aa.txt"); InputStreamReader isr = new InputStreamReader(is); //int defaultCharBufferSize = 1024*8; //BufferedReader br = new BufferedReader(isr,defaultCharBufferSize); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine())!=null){ System.out.println(data); }
InputStreamReader是字节流转成字符流的桥梁用来处理文本文件
利用BufferedReader包装InputStreamReader达到较好的性能
BufferedReader的readLine为读行数组
1 ) File 类介绍
File 类封装了对用户机器的文件系统进行操做的功能。例如,能够用 File 类得到文件上次修改的时间移动,
或者对文件进行删除、重命名。换句话说,流类关注的是文件内容,而 File 类关注的是文件在磁盘上的存储
File 类的主要方法有(),lastMod: getName(),getCanonicalFileified(),isDerector(),isFile(),getPath() 等;
2 ) File 类与 FileInputStream 类的区别:
流类关注的是文件内容,而 File 类关注的是文件在磁盘上的存储。
File 不属于文件流 , 只能表明一个文件或是目录的路径名而已。
提示:
若是处理文件或者目录名,就应该使用 File 对象,而不是字符串。例如, File 类的 equals 方法知道一些
文件系统对大小写是敏感的,目录尾的“ / ”字符可有可无。
FileInputStream 类或者 FileReader 类的构造函数有多个,其中典型的两个分别为:一个使用 File 对象为参数;而另外一个使用表示路径的 String 对象做为参数;本身之前一直以为直接用了 String 指定路径就能够了,一直不明白为何不少人都先构造一个 File 对象,如今终于明白了,“若是处理文件或者目录名,就应该使用 File 对象,而不是字符串。”!
FileInputStream 类
1 ) FileInputStream 类介绍:
以字节为单位的流处理。字节序列:二进制数据。与编码无关,不存在乱码问题。
FileInputStream 类的主要方法有:
Read (), read ( byte[] b ), read ( byte[],int off,int len ) ,available();
2 ) FileInputStream 类与 FileReader 类的区别:
两个类的构造函数的形式和参数都是相同的,参数为 File 对象或者表示路径的 String ,它们到底有何区别
呢?
FileInputStream :以字节流方式读取;
FileReader :把文件转换为字符流读入;
InputStream提供的是字节流的读取,而非文本读取,这是和Reader类的根本区别。用Reader读取出
来的是char数组或者String ,使用InputStream读取出来的是byte数组。
Reader类及其子类提供的字符流的读取char,inputStream及其子类提供字节流的读取byte,因此 FileReader类是将文件按字符流的方式读取,FileInputStream则按字节流的方式读取文件;InputStreamReader能够将读如stream转换成字符流方式,是reader和stream之间的桥梁
最初Java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类。
FileInputStream 类以二进制输入 / 输出, I/O 速度快且效率搞,可是它的 read ()方法读到
的是一个字节,很不利于人们阅读。 而 FileReader 类弥补了这个缺陷,能够以文本格式输入/ 输出,很是方便;好比可使用 while((ch = filereader.read())!=-1 ) 循环来读取文件;可使用BufferedReader 的 readLine() 方法一行一行的读取文本。
当咱们读写文本文件的时候,采用 Reader 是很是方便的,好比 FileReader , InputStreamReader 和 BufferedReader 。其中最重要的类是 InputStreamReader ,它是字节转换为字符的桥梁。 你能够在构造器中指定编码的方式,若是不指定的话将采用底层操做系统的默认编码方式,例如 GBK 等。
FileReader 与 InputStreamReader 涉及编码转换 ( 指定编码方式或者采用 os 默认编码 ) ,可能在不一样的平台上出现乱码现象!而 FileInputStream 以二进制方式处理,不会出现乱码现象 .
若是处理纯文本文件,建议使用 FileReader ,由于更方便,也更适合阅读;可是要注意编码问题!其余状况(处理非纯文本文件),FileInputStream是惟一的选择;FileInputStream是进Socket通信时会用到不少,如将文件流是Stream的方式传向服务器!
FileReader 类
FileReader 类介绍:
InputStreamReader 类的子类,全部方法(read ()等)都从父类 InputStreamReader 中继承来;
与 InputStreamReader 类的区别:
该类与它的父类 InputStreamReader 的主要不一样在于构造函数,主要区别也就在于构造函数!从
InputStreamReader 的构造函数中看到,参数为 InputStream 和编码方式,能够看出,当要指定编码方式时,必须使用 InputStreamReader 类;而 FileReader 构造函数的参数与 FileInputStream 同,为 File 对象或表示 path 的 String ,能够看出,当要根据 File 对象或者 String 读取一个文件时,用 FileReader 我想FileReader 子类的做用也就在于这个小分工吧。
通常用法:
FileReader fr = new FileReader("ming.txt");
char[] buffer = new char[1024];
int ch = 0;
while((ch = fr.read())!=-1 )
{
System.out.print((char)ch);
}
InputStreamReader 类
以文本格式输入 / 输出,能够指定编码格式;
主要方法:
getEncoding (),read();
通常用法:
InputStreamReader isr = new InputStreamReader(new FileInputStream("ming.txt"));
while((ch = isr.read())!=-1)
{
System.out.print((char)ch);
}
BufferedReader 类
BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,并且提供了很实用的readLine,
读取分行文本很适合,BufferedReader是针对Reader的,不直接针对文件,也不是只针对文件读取。
通常用法:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ming.txt")));
String data = null;
while((data = br.readLine())!=null)
{
System.out.println(data);
}
总结以上内容,得出比较好的规范用法:
1) File file = new File ("hello.txt");
FileInputStream in=new FileInputStream(file);
2) File file = new File ("hello.txt");
FileInputStream in=new FileInputStream(file);
InputStreamReader inReader=new InputStreamReader(in);
BufferedReader bufReader=new BufferedReader(inReader);
3) File file = new File ("hello.txt");
FileReader fileReader=new FileReader(file);
BufferedReader bufReader=new BufferedReader(fileReader);
1)
File file = new File ("hello.txt");
FileInputStream in=new FileInputStream(file);
InputStreamReader inReader=new InputStreamReader(in);
BufferedReader bufReader=new BufferedReader(inReader);
2)
FileInputStream in=null;
File file = new File ("hello.txt");
in=new FileInputStream(file);
BufferedReader bufReader=new BufferedReader(new InputStreamReader(in));
3)
File file = new File ("hello.txt");
BufferedReader bufReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
上述两种写法的微小区别:
a)第二种方式中把“FileInputStream in=null;”定义单独放在开始处,说明下面应该还有要用到in对象变量的地方;(BufferedReader处用了)
b)第二种方式没有定义InputStreamReader的对象变量,直接在BufferedReader的构造函数中new一个,
这种方式与第一种方式的主要区别:InputStreamReader对象只使用一次!
这对于在这里只须要使用一次这个InputStreamReader对象的应用来讲更好;无需定义InputStreamReader的对象变量,接收由new返回的该对象的引用,由于下面的程序中不须要这个 InputStreamReader的对象变量,因此无需定义;因此这种状况下,第二种方式比第一种更好一些。
c)第三种方式中,典型的三层嵌套委派关系,清晰看出Reader的委派模式(《corejava》12章有图描述该委派关系),FileInputStream和InputStreamReader都没有定义变量,new生成的对象都只是使用一次。
d)三种方式的区别也就在于FileInputStream和InputStreamReader对象是否都只使用一次,是否须要定义它们的对象变量,以及我的的编码习惯。
e)可是要注意异常处理,FileInputStream(file)会抛出NotFileFoundException,若是采用surround方式
(try&catch)处理,应该用第二种方式,这样能够用System.out.println提示文件未找到;
固然在函数名后使用throws Exception,而后用第三种方式也行,但彷佛这适合有用户界面的状况,把异常抛出在客户端在处理。服务器