java⑤

输入流和输出流相对于内存设备而言.
将外设中的数据读取到内存中:输入

将内存的数写入到外设中:输出。

IO流:输入流:输出流:

      字节流:

            字符流:为了处理文字数据方便而出现的对象。其实这些对象的内部使用的还是字节流(因为文字最终也是字节数据)只不过,通过字节流读取了相对应的字节数,没有对这些字节直接操作。而是去查了指定的(本机默认的)编码表,获取到了对应的文字。简单说:字符流就是  :  字节流+编码表。

字节流的两个顶层父类:
1,InputStream 2,OutputStream.

字符流的两个顶层父类:
1,Reader          2,Writer

这些体系的子类都以父类名作为后缀。 而且子类名的前缀就是该对象的功能。

FileWriter演示:

public class FileWriterDemo {
private  static  final  String  LINE_SEPARATOR  = 
System.getProperty("line.separator");
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
//创建一个可以往文件中写入字符数据的字符输出流对象。
/*
*  既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地)。
*  如果文件不存在,则会自动创建。 如果文件存在,则会被覆盖。
*  如果构造函数中加入true,可以实现对文件进行续写!
*/
FileWriter fw = new FileWriter("demo.txt",true);
/*
*  调用Writer对象中的write(string)方法,写入数据。 其实数据写入到临时存储缓冲区中。
*/
fw.write("abcde"+LINE_SEPARATOR+"hahaha");
fw.flush();<span style="font-family: Arial, Helvetica, sans-serif;">//进行刷新,将数据直接写到目的地中。</span>
fw.close();//<span style="font-family: Arial, Helvetica, sans-serif;">关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。
</span>//    fw.write("haha");// java.io.IOException: Stream closed
}
}
FileReader演示1:

public class FileReaderDemo {
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
//1,创建读取字符数据的流对象。
/* 在创建读取流对象时,必须要明确被读取的文件。一定要确定该文件是存在的。 用一个读取流关联一个已存在文件。*/
FileReader fr = new FileReader("demo.txt");
int ch = 0;
while((ch=fr.read())!=-1){
System.out.println((char)ch);
}
//用Reader中的read方法读取字符。
int ch = fr.read();
System.out.println((char)ch);
int ch1 = fr.read();
System.out.println(ch1);
int ch2 = fr.read();
System.out.println(ch2);
fr.close();
}
}
字符流(Reader、Writer)基本使用:

public class CopyTextTest {
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
//1,读取一个已有的文本文件,使用字符读取流和文件相关联。
FileReader fr = new FileReader("IO流_2.txt");
//2,创建一个目的,用于存储读到数据。
FileWriter fw = new FileWriter("copytext_1.txt");
//3,频繁的读写操作。
int ch = 0;
while((ch=fr.read())!=-1){
fw.write(ch);
}
//4,关闭流资源。
fw.close();
fr.close();
}
}
public class CopyTextTest_2 {
private static final int BUFFER_SIZE = 1024;
201
/**
* @param args
*/
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("IO流_2.txt");
fw = new FileWriter("copytest_2.txt");
//创建一个临时容器,用于缓存读取到的字符。
char[] buf = new char[BUFFER_SIZE];//这就是缓冲区。
//定义一个变量记录读取到的字符数,(其实就是往数组里装的字符个数  )
int len = 0;
while((len=fr.read(buf))!=-1){
fw.write(buf, 0, len);
}
} catch (Exception e) {
//      System.out.println("读写失败");
throw new RuntimeException("读写失败");
}finally{
if(fw!=null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fr!=null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedReader演示:

public class BufferedReaderDemo {
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("buf.txt");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
while((line=bufr.readLine())!=null){
System.out.println(line);
}
/*
String line1 = bufr.readLine();
System.out.println(line1);
String line2 = bufr.readLine();
System.out.println(line2);
String line3 = bufr.readLine();
System.out.println(line3);
String line4 = bufr.readLine();
System.out.println(line4);
String line5 = bufr.readLine();
System.out.println(line5);
*/
bufr.close();
}
/**
* @throws FileNotFoundException
* @throws IOException
*/
public static void demo() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("buf.txt");
203
char[] buf = new char[1024];
int len = 0;
while((len=fr.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
fr.close();
}
}
BufferedWriter演示:

public class BufferedWriterDemo {
private  static  final  String  LINE_SEPARATOR  = 
System.getProperty("line.separator");
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("buf.txt");
//为了提高写入的效率。使用了字符流的缓冲区。
//创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联
BufferedWriter bufw = new BufferedWriter(fw);
//使用缓冲区的写入方法将数据先写入到缓冲区中。
//    bufw.write("abcdefq"+LINE_SEPARATOR+"hahahha");
//    bufw.write("xixiixii");
//    bufw.newLine();
//    bufw.write("heheheheh");
for(int x=1; x<=4; x++){
bufw.write("abcdef"+x);
bufw.newLine();
bufw.flush(); 
204
}
//使用缓冲区的刷新方法将数据刷目的地中。
//    bufw.flush();
//关闭缓冲区。其实关闭的就是被缓冲的流对象。
bufw.close();
//    fw.write("hehe");
//    fw.close();
}}
字节流(InputStream 、OutputStream)使用演示:

public class ByteStreamDemo {
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
demo_read();
}
public static void demo_read() throws IOException {
//1,创建一个读取流对象。和指定文件关联。
FileInputStream fis = new FileInputStream("bytedemo.txt");
//    System.out.println(fis.available());
//    byte[] buf = new byte[fis.available()];  
//    fis.read(buf);
//    System.out.println(new String(buf));
//建议使用这种读取数据的方式
//    byte[] buf = new byte[1024];   
//    int len = 0;
//   
//    while((len=fis.read(buf))!=-1){
//      System.out.println(new String(buf,0,len));
//    }
//    int ch = 0;
//    while((ch=fis.read())!=-1){
//      System.out.println((char)ch);
//    }
//一次读取一个字节。
//    int ch = fis.read();   
//    System.out.println(ch);
fis.close();
}
public static void demo_write() throws IOException {
//1,创建字节输出流对象。用于操作文件.
FileOutputStream fos = new FileOutputStream("bytedemo.txt"); 
211
//2,写数据。直接写入到了目的地中。
fos.write("abcdefg".getBytes());
//    fos.flush();
fos.close();//关闭资源动作要完成。
}
}
.IO流操作规徇及需求演示:
流的操作规律:
之所以要弄清楚这个规律,是因为流对象太多,开发时不知道用哪个对象合适。
想要知道开发时用到哪些对象。只要通过四个明确即可。
1,明确源和目的(汇)
源:InputStream Reader
目的:OutputStream Writer
2,明确数据是否是纯文本数据。
源:是纯文本:Reader
否:InputStream
目的:是纯文本  Writer
否:OutputStream
到这里,就可以明确需求中具体要使用哪个体系。
3,明确具体的设备。
源设备:
硬盘:File
键盘:System.in
内存:数组
网络:Socket流
目的设备:
硬盘:File
控制台:System.out
内存:数组
网络:Socket流
4,是否需要其他额外功能。
1,是否需要高效(缓冲区);
是,就加上buffer.
2,转换。
227
--------------------------------------------------------------------------------需求1:复制一个文本文件。
1,明确源和目的。
源:InputStream Reader
目的:OutputStream Writer
2,是否是纯文本?
是!
源:Reader
目的:Writer
3,明确具体设备。
源:
硬盘:File
目的:
硬盘:File
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");
4,需要额外功能吗?
需要,需要高效。
BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值