---------------------- android培训、java培训、期待与您交流! ----------------------
1. IO流常用基类
字节流的抽象基类:
InputStream OutputStream
字符流的抽象基类
Reader Writer
2. 字符流
write
1) 既然IO流是用于操作数据的,那么数据的最常见形式是:文件。
需求:在硬盘上,创建一个文件并写入一些文字数据。
import java.io.*;
class FileWriterDemo
{
publicstatic void main(String[] args) throws IOException
{
//创建一个FileWriter对象。该对象一被初始化就必须要明确被操作
//的文件。而且该文件会building创建到指定目录下。如果该目录下
//有同名文件,将被覆盖。其实该步就是在明确数据存放的目的地。
FileWriter fw = new FileWriter(“demo.txt”);
//调用write方法,将字符串写入到流中。
fw.write(“abcde”);
//刷新流对象中的缓冲中的数据。将数据刷的目的地中。
fw.flush(); //结果:abcde
//fw.close(); 关闭流资源,但关闭之前会刷新缓冲数据。
}
2) IO异常处理
import java.io.*;
class FileWriterDemo2
{
publicstatic void main(String[] args)
{
FileWriterfw = null;
try{
fw =new FileWriter(“demo.txt”);
fw.write(“abcde”);
fw.close();
}catch(IOExceptione)
{
System.out.println(e.toString());
}finally{
try{
if(fw!=null)
fw.close(); //为空不能调用close()
} catch(IOException e)
{
System.out.println(e.toString());
}
}
}
}
3)文件的续写
import java.io.*;
class FileWriterDemo3
{
publicstatic void main(String[] args) throws IOException
{
//传递一个true参数,代表不覆盖已有文件,并在已有文件的末尾处
// 进行数据续写。 //不要抛,要写异常处理
FileWriterfw = new FileWriter(“demo.txt”,true);
fw.write(“hello\r\nheima”);
fw.close();
}
}
reader
第一种方式:指定名称的文件相关联
import java.io.*;
class FileReaderDemo
{
publicstatic void main(String[] args) throws IOException
{
//创建一个文件读取流对象,和指定名称的文件相关联。要保证该文件
//是已经存在的,如果不存在,会发生异常FileNotFoundException
FileReader fr = new FileReader(“demo.txt”);
while(true){
//读取单个字符,而且自动往下读。到流的末尾,返回-1.
intch = fr.read();
if(ch==-1)
break;
System.out.println(“ch=”+(char)ch);
}
fr.close();
}
}
第二种方式:通过字符数组进行读取。
FileReader fr = new FileReader(“demo.txt”);
//该read(char[])返回的是读取到的字符个数。
char[] buf = new char[3];
int num = 0;
while((num=fr.read(buf))!==-1)
{
System.out.println(newString(buf,0,num));
}
fr.close();
练习:读取一个.java文件,并打印在控制台上。
import java.io.*;
class FileReaderDemo2
{
publicstatic void main(String[] args) throws IOException
{
FileReader fr = newFileReader(“DateDemo.java”);
char[] buf = newchar[1024];
int num = 0;
while((num=fr.read(buf))!==-1)
{
System.out.print(new String(buf,0,num));
}
fr.close();
}
}
3. 字符流的缓冲区
1) BufferedWriter
import java.io.*;
class BufferdWriterDemo
{
publicstatic void main(String[] args)
{
//创建一个字符写入流对象
FileWriter fw = newFileWriter(“buf.xtx”);
//为了提高字符写入流效率,加入了缓冲技术
//只要将需要被提高效率的流对象作为参数传递给缓冲区的构造函数
BufferedWriter bufw =new BufferedWriter(fw);
for(int x=1; x<5;x++)
{
bufw.write(“abcd”+x);
bufw.newLine();//换行
bufw.flush();
}
bufw.write(“abcde”);
//bufw.flush();//只要用到缓冲区,就要刷新。
bufw.close();//关闭缓冲区,就是关闭缓冲区中的流对象。
}
}
2) BuffererReader
public static void main(String[] args)throws IOException{
FileReaderfr = new FileReader(buf.txt);
BuffererReaderbufr = new BuffererReader(fr);
Stringline = null;
//readLine读一行,返回null表示读到文件末尾
while((line=bufr.readLine())!=null){
System.out.println(line);
}
bufr.close();
}
4. 字节流
InputStream读 OutputStream写
public static void readFile_1() throwsIOException //一个字节读
{
FileInputStreamfis = new FileInputStream();
intch = 0;
while((ch=fis.read())!=-1){
System.out.println((char)ch);
}
fis.close();
}
public static void readFile_2() throwsIOException
{
FileInputStreamfis = new FileInputStream(fos.txt);
byte[]buf = new byte[1024];
intlen = 0;
while((len=fis.read(buf))!=-1){
System.out.println(newString(buf,0,len));
}
fis.close();
}
public static void readFile_3() throwsIOException
{
FileInputStreamfis = new FileInputStream(fos.txt);
//intnm = fis.available();
//定义一个刚刚好的数组,就不用在循环了。数据大,不宜使用。
byet[]buf = new byte[fis.available()];
fis.read(buf);
System.out.println(newString(buf));
fis.close();
}
public static void writeFile() throwsIOException
{
FileOutputStream fos = newFileOutputStream(fos.txt);
fos.write(“abcde”,getBytes());
fos.close();
}
5. 读取键盘录入
System.out:对用的是标准输出设备,控制台。
System.in:对于的标准输入设备,键盘。
InputStreamin = System.in;
intby = in.read();
System.out.println(by); //输入abc,结果为:97。这只读了一次
流操作的基本规律:
1. 明确源和目的。
源:输入流,InputStream Reader
目的:输出流,OutputStream Writer
2. 操作的数据是否是纯文本。
是:字符流。
不是:字节流。
3. 当体系明确后,在明确要使用哪个具体的对象。
通过设备来进行区分:
源设备:内存,硬盘,键盘
目的设备:内存,硬盘,控制台
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima