码迷,mamicode.com
首页 > 编程语言 > 详细

java IO流学习笔记

时间:2015-05-04 23:35:01      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.*;
class hello{
public static void main(String[] args) {
System.out.println(File.separator);
System.out.println(File.pathSeparator);
}
}
【运行结果】:

\

;

String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
if(f.exists()){
f.delete();


File f=new File("D:\\hello.txt");
创建新文件
f.createNewFile();
创建新文件夹
f.mkdir();
删除文件
f.delete();


列出指定目录的全部文件(包括隐藏文件):
listFiles输出的是完整路径
String fileName="D:"+File.separator;
File f=new File(fileName);
File[] str=f.listFiles();
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}else{
System.out.println("文件不存在");
}

字节流:

预先申请了一个指定大小的空间,但是有时候这个空间可能太小,有时候可能太大,我们需要准确的大小,这样节省空间,那么我
们可以这样干
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[(int)f.length()];
for (int i = 0; i < b.length; i++) {
b[i]=(byte)in.read();
}
in.close();
System.out.println(new String(b));
}
}


有时候我们不知道文件有多大,这种情况下,我们需要判断是否独到文件的末尾
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[1024];
int count =0;
int temp=0;
while((temp=in.read())!=(-1)){
b[count++]=(byte)temp;
}
in.close();
System.out.println(new String(b));
}
当读到文件末尾的时候会返回-1.正常情况下是不会返回-1的

 

字符流:
写:
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
Writer out =new FileWriter(f);
String str="hello";
out.write(str);
out.close();
}

其实这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。

当你如果想问文件中追加内容的时候,可以使用将上面的声明out的哪一行换为:

Writer out =new FileWriter(f,true);

这样,当你运行程序的时候,会发现文件内容变为:

hellohello如果想在文件中换行的话,需要使用“\r\n”

比如将str变为String str="\r\nhello";

这样文件追加的str的内容就会换行了。

读:
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
char[] ch=new char[100];
Reader read=new FileReader(f);
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("内容为"+new String(ch,0,count));
}


实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候下后是会用到缓冲区的
,是通过缓冲区来操作文件的。

读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话
,文件中已经存在内容,但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。


OutputStreramWriter将输出的字符流转化为字节流

InputStreamReader将输入的字节流转换为字符流

但是不管如何操作,最后都是以字节的形式保存在文件中的。

 

public static void main(String[] args){
// 此刻直接输出到屏幕
System.out.println("hello");
File file = new File("d:" + File.separator + "hello.txt");
try{
System.setOut(new PrintStream(new FileOutputStream(file)));
}catch(FileNotFoundException e){
e.printStackTrace();
}
System.out.println("这些内容在文件中才能看到哦!");
}
}

【运行结果】:

eclipse的控制台输出的是hello。然后当我们查看d盘下面的hello.txt文件的时候,会在里面看到:这些内容在文件中才能看到哦

 

public static void main(String[] args){
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str = null;
System.out.println("请输入内容");
try{
str = buf.readLine();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("你输入的内容是:" + str);
}

BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入
流,

 

字节流:一次读入或读出是8位二进制。
字符流:一次读入或读出是16位二进制。
字节流和字符流的原理是相同的,只不过处理的单位不同而已。后缀是Stream是字节流,而后缀是Reader,Writer是字符流。

使用对象流需要实现Serializable接口,否则会报错。


以stream都是字节流,以reader、writer都是字符流,其中2个是充当的转换器intputStreamReader和OntputStreamWriter,缓冲流
就是加上了buffered,为什么要缓冲流呢:
把低级流用缓冲流封装,可以加快读写的速度


最原始的字节流没有用到缓冲区,但是你可以给它套一个缓冲流吧


转化流:InputStreamReader/OutputStreamWriter,把字节转化成字符
Buffered缓冲流::BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter,是带缓冲区的处理流,缓冲
区的作用的主要目的是:避免每次和硬盘打交道,提高数据访问的效率。
对象流:ObjectInputStream,ObjectOutputStream,把封装的对象直接输出,而不是一个个在转换成字符串再输出。

 

 

何时使用转换流?
1.
如果使用非默认编码保存文件或者读取文件时,需要用到转换流,因为字节流的重载构造方法中有指定编码格式的参数,而FielReader 与 FileWriter 是默认编码的文本文件
比如:

当我们使用默认GBK编码保存文本时,下面2句代码其实是一样的效果,

new OutputStreamWriter(new FileOutputStream("out.txt"))

new FileWriter("out.txt")

当要求保存为其他编码比如UTF-8时,就要这样写

new OutputStreamWriter(new FileOutputStream("out.txt"),"UTF-8")

而如果要读取一个UTF-8编码的文本文件时,同样的要用

new InputStreamReader(new FileInputStream("in.txt"),"UTF-8");

而不能用new FileWriter("in.txt")

java IO流学习笔记

标签:

原文地址:http://www.cnblogs.com/lxfeng/p/4310271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!