Java中的I/O (一)
Java中的I/O (1)目标:1. I/O 操作的目标;2. I/O 分类;3. 读取文件和写入文件的方法。?流是指:数据源与程序
Java中的I/O (1)
目标:
1. I/O 操作的目标;
2. I/O 分类;
3. 读取文件和写入文件的方法。
?
流是指:数据源与程序之间的通道。
?
I/O的流向:
? [文件]???? Input ?????????????????? Output?????? [文件]
? [键盘]???? ------> ? Java程序? -------> ? ? ?? [屏幕]
? [网络]??????????????????????????????????????????????????? [网络]
?
--------------------
?
第1种分类:1. 输入流 2. 输出流
第2种分类:1. 字节流 2.字符流
第3种分类:1. 节点流 2.处理流
?
---------------------------------------
?
字节流的核心类
???????????????????? ?? ? extends
InputStream?? <------------? FileInputStream
???????????????????????? extends
OutputStream <-----------?? FileOutputStream
?
----------------------------------------
?
InputStream:
???????? int read(byte [] buffer, int offset, int length);
OutputStream:
???????? void write (byte [] buffer, int offset, int length);
?
调用一个String对象的tring()方法去除掉这个字符串的首尾空格和空字符。
//导入类import java.io.InputStream;import java.io.FileInputStream;import java.io.OutputStream;import java.io.FileOutputStream;class Test{public static void main(String args []){//声明输入流的引用FileInputStream fis = null;//声明输出流的引用FileOutputStream fos = null;try{//生成代表输入流的对象fis = new FileInputStream("m:/src/from.txt"); //生成代表输出流的对象fos = new FileOutputStream("m:/src/to.txt");//生成一个字节数组byte [] buffer = new byte[1024];while (true) {//调用输入流对象的read方法,读取数据int temp = fis.read(buffer, 0, buffer.length);if (temp == -1) {break;}fos.write(buffer, 0, temp);}//String s = new String(buffer);//Copies this string removing white space characters//from the beginning and end of the string.//s = s.trim(); //System.out.println(s);}catch (Exception e) {System.out.println(e);}finally {try{fis.close();fos.close();}catch (Exception e) {System.out.println(e);}}}}?
?
?