zoukankan      html  css  js  c++  java
  • io流

     2.java IO原理:
    • IO流用来处理设备之间的数据传输。
    • java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行;
    • java.io包下提供了各种"流"类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据
    输入input:读取外部数据(磁盘等存储设备的数据)到程序(内存)中;
    输出output:将程序数据输出到磁盘等存储设备中;
    3.流的分类:
    • 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit) 
    • 按数据流的流向不同分为:输入流,输出流
    • 按流的角色的不同分为:节点流
      可以从一个特定的 IO 设备读/写数据的流

                    处理流(加速节点流的传输):
      对一个已存在的流进行连接和封装,通过封装后的流来实现数据读/写操作 
    字节流:以byte为单位传输;
    字符流:以char为单位传输;
    节点流:直接作用在文件上的,可以从一个特定的数据源读写数据;
    处理流:(加速节点流的传输)“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。
    注意:使用FileReader  FileWriter可以实现文本文件的复制。对于非文本文件(图像,音频)只能使用字节流
    节点流:(输入)FileInputStream、FileReader
                   (输出) FileOutputStream、FileWriter
    缓冲流:(输入流):BufferedInputStream、BufferedReader
                     (输出流):BufferedOutputStream、BufferedWriter
    (抽象基类)
    字节流
    字符流
    输入流
    InputStream
    Reader
    输出流
    OutputStream
    Writer
     
    java.io.Reader和java.io.InputStream组成了java输入类,Reader用于读入16位字符,也就是Unicode编码的字符;
    而InputStream用于读入ASC||字符和二进制数据。
    reader支持16位的UNicode字符输出,inputstream支持8位的字符输入;
     
    java中的字节流:
    inputstream是所有字节输入流的祖先,而outputstream是所有字节输出流的祖先
     
    java中的字符流:
    reader是所有读取字符串输入流的祖先,而writer是所有输出字符串的祖先。
     
    字节流读写时读的是一个一个的字节。而字符流读写的时候是一个一个的字符
    4。常用方法
    • 写入文件:

      1.创建流对象,建立数据存放文件

      • FileWriterfw= new FileWriter(“Test.txt”);

      2.调用流对象的写入方法,将数据写入流

      • fw.write(“text”);

      3.关闭流资源,并将流中的数据清空到文件中。

      • fw.close();
    1.异常
    try {
                //可能产生异常的代码
            } catch (Exception e) {
                // TODO: handle exception //异常代码处理
            }
            finally {
                //无论异常不异常都会执行的代码
            }
    oos.close();//文件流结束必须有close
    2.  找到目录,输出制定文件下的所有文件夹名
    /**
         * 输出指定文件下的所有文件夹名  只能输出其下一级的文件夹名
         * @param file
         */public static void show(File file){
            if (file.isDirectory()) {
                File[] file3=file.list Files();
                for (File file2 : file3) {
                    System.out.println(file2.getName());
                }
            }
        }
    3.  
    /**
         * 
         *输入字节流
         * @param file
         * @throws IOException  抛异常
         */public static void get(File file) throws IOException{
            FileInputStream fis=new FileInputStream(file);
            System.out.println("可读字节数:"+fis.available());
            System.out.println("文件内容为:");
            int data;
            while ((data=fis.read())!=-1) {//读不到字节fis.read())为-1
                System.out.println(String.valueOf((char)data));//如果文件中能读到字节,就以字符串形式输出,
            }
        }
    4.  
    /**
         * 输出字节
         * @param file
         * @throws IOException     
         */public static void showf(Filefile) throws IOException{
            String str="good good study";
            byte[] words=str.getBytes();
       //第二个参数不写时默认为false,就是替换,为true就是追加
            FileOutputStream fe=new FileOutputStream(file,true);
            fe.write(words, 0, words.length);
        }

    5.
    InputStream&read:输入
    从硬盘存在的一个文件中,读取其内容到程序中。使用FileInputStream;
    要读取的文件一定要存在。否则抛FileNotFoundException;
    • InputStream(典型实现:FileInputStream)
    • int  read():从输入流中读取单个字节,返回所读取的字节数据
    • int read(byte[] b)
    • intread(byte[] b,intoff,intlen)
     
    public void testFileInputStream1() throws Exception {
            // 1.创建一个File类的对象。
            File file = new File("hello.txt");
            // 2.创建一个FileInputStream类的对象
            FileInputStream fis = new FileInputStream(file);
            // 3.调用FileInputStream的方法,实现file文件的读取。
            System. out.println( "可读取字节数" + fis.available());
           
            /*
             * read():读取文件的一个字节。当执行到文件结尾时,返回-1
             */
            // int b = fis.read();
            // while(b != -1){
            // System.out.print((char)b);
            // b = fis.read();
            // }
            int b;
            while ((b = fis.read()) != -1) {
                System.out.print((char) b);
            }
            // 4.关闭相应的流
            fis.close();
        }
     
     
    // 使用try-catch的方式处理如下的异常更合理:保证流的关闭操作一定可以执行
        @Test
        public void testFileInputStream2() {
            // 2.创建一个FileInputStream类的对象
            FileInputStream fis = null;
            try {
                // 1.创建一个File类的对象。
                File file = new File("hello.txt");
                fis = new FileInputStream(file);
                // 3.调用FileInputStream的方法,实现file文件的读取。
                int b;
                while ((b = fis.read()) != -1) {
                    System.out.print((char) b);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 4.关闭相应的流
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
     
    ****int  read(byte[] b):重点
    @Test
        public void testFileInputStream3() { // abcdefgcde
            FileInputStream fis = null;
            try {
                File file = new File("hello.txt");
                fis = new FileInputStream(file);
                byte[] b = new byte[5];// 读取到的数据要写入的数组。
                int len;// 每次读入到byte中的字节的长度
                while ((len = fis.read(b)) != -1) {
                    // for (int i = 0; i < len; i++) {
                    // System.out.print((char) b[i]);
                    // }
                  !!  String str = new String(b, 0, len);//相当于把字结数组转换成字符串
                    System.out.print(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
     
                }
     
            }
        }
     
    Reader:
    • Reader(典型实现:FileReader)
    • int read()
    • int read(char [] c)   :c:一次读并存入数组多少个字节!!!!!!!
    • int read(char [] c,int off,int len)
    • 程序中打开的文件IO资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件IO资源。
    6.OutputStream& Writer:输出
    • OutputStream和Writer也非常相似:
    • void write(intb/intc);
    • void write(byte[] b/char[]cbuf);
    • void write(byte[] b/char[] buff,int off,intlen);
    • void flush();
    • void close();需要先刷新,再关闭此流
    • 因为字符流直接以字符作为操作单位,所以Writer可以用字符串来替换字符数组,即以String对象作为参数
    • void write(Stringstr);
    • void write(String str,int off,int len);
     
    @Test
        public void testFileOutputStream() {
            // 1.创建一个File对象,表明要写入的文件位置。
            // 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,默认是会将原有的文件覆盖
            //默认和第二个参数是false,就是替换,如果第二个参数是true,则是追加
            File file = new File("hello2.txt",true);
            // 2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
                // 3.写入的操作
    //string str=new string("I love China!");
    //fos.write(str.getBytes()
                fos.write(new String("I love China!").getBytes()//转换成字节);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 4.关闭输出流
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
     
     
     
    // 从硬盘读取一个文件,并写入到另一个位置。(相当于文件的复制)
        @Test
        public void testFileInputOutputStream() {
            // 1.提供读入、写出的文件
            File file1 = new File("C:\Users\shkstart\Desktop\1.jpg");
            File file2 = new File("C:\Users\shkstart\Desktop\2.jpg");
            // 2.提供相应的流
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(file1);
                fos = new FileOutputStream(file2);
                // 3.实现文件的复制
                byte[] b = new byte[20];
                int len;
                while ((len = fis.read(b)) != -1) {
                    ////错误的写法两种: fos.write(b);= fos.write(b,0,b.length);
                    fos.write(b, 0, len);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
     
            }
        }
     
    文件复制的方法
     
    @Test
        public void testCopyFile(){
            long start = System.currentTimeMillis();
            String src = "dbcp.txt";
            String dest = "dbcp2.txt";
            copyFile(src,dest);
            long end = System.currentTimeMillis();
            System.out.println("花费的时间为:" + (end - start));//3198
        }
     
        // 实现文件复制的方法
        public void copyFile(String src, String dest) {
            // 1.提供读入、写出的文件
            File file1 = new File(src);
            File file2 = new File(dest);
            // 2.提供相应的流
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(file1);
                fos = new FileOutputStream(file2);
                // 3.实现文件的复制
                byte[] b = new byte[1024];
                int len;
                while ((len = fis.read(b)) != -1) {
                    // fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
                    fos.write(b, 0, len);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
     
            }
        }
     
     
    7.文件流:(节点流)
    读取文件:
    1.建立一个流对象,将已存在的一个文件加载进流;
       FileReader fr=new FileReader("Test.txt");
    2.创建一个临时存放数据的数组。
       char[]ch=new char[1024];
    3.调用流对象的读取方法将流中的数据读入到数组中。
        fr.read(ch);
    FileReader fr = null;
      try{
      fr = new FileReader("c:\test.txt");
      char[] buf = new char[1024];
      int len= 0;
      while((len=fr.read(buf))!=-1){
      System.out.println(new String(buf ,0,len));}
      }catch (IOException e){
      System.out.println("read-Exception :"+e.toString());}
      finally{
      if(fr!=null){
      try{
      fr.close();
      }catch (IOException e){
      System.out.println("close-Exception :"+e.toString());
      }
    }
    }
     
    写入文件:

    1.创建流对象,建立数据存放文件

    • FileWriter  fw= new   FileWriter(“Test.txt”);

    2.调用流对象的写入方法,将数据写入流

    • fw.write(“text”);

    3.关闭流资源,并将流中的数据清空到文件中。

    • fw.close();
    FileWriter fw = null;
      try{
      fw= new FileWriter("Test.txt");
      fw.write("text");
      }
      catch (IOException e){
      System.out.println(e.toString());
      }
      finally{
      If(fw!=null)
      try{
       fw.close();
      }
      catch (IOException e){
      System.out.println(e.toString());
    }
    • 定义文件路径时,注意:可以用“/”或者“\”。
    • 在写入一个文件时,如果目录下有同名文件将被覆盖。
    • 在读取文件时,必须保证该文件已存在,否则出异常。
    8.缓冲流(处理流的一种)
    BufferedWriter 相其对应的节点流多了  newline()换行方法    
    
    
    bw.flush();
    
    
    刷新缓冲区
    
    
    
    
    BufferedReader 相其对应的节点流多了 readline()  读一行方法
    **用BufferedReader     BufferedWriter实现文件内容复制
    方法一:
    BufferedReader br = null;
    BufferedWriter bw = null;		
    try {
    //step1:创建缓冲流对象:它是过滤流,是对节点流的包装
    br = newBufferedReader(new FileReader("d:\IOTest\source.txt"));
    bw = new BufferedWriter(new FileWriter("d:\IOTest\destBF.txt"));
    Stringstr = null;
    while ((str = br.readLine()) != null) { //一次读取字符文本文件的一行字符
    bw.write(str); //一次写入一行字符串
    bw.newLine();  //写入行分隔符
    } bw.flush();  //step2:刷新缓冲区
    } catch (IOException e) {
    e.printStackTrace();
    } 
    finally {
    // step3: 关闭IO流对象try {
    if (bw != null) {
    bw.close();  //关闭过滤流时,会自动关闭它所包装的底层节点流
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    try {
    if (br != null) {
    br.close();
    }  } catch (IOException e) {
    e.printStackTrace();
    }  }
    方法二:创建一个文件复制         方法
    public class FileReaderTest {
    	publicstaticvoid main(String[] args) {
    		bufferReader("d:\cc.txt", "d:\ii.txt");
    	}
    
    	publicstaticboolean bufferReader(String file1, String file2) {
    		try {
    			File file = new File(file1);
    			if (file.exists()) {
    				BufferedReader bufferedReader = newBufferedReader(new FileReader(file1));
    				BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file2));
    				Stringline = null;
    				while ((line = bufferedReader.readLine()) != null) {
    					bufferedWriter.write(line);
    					bufferedWriter.newLine();
    					bufferedReader.close();
    					bufferedWriter.close();
    					returntrue;
    				}
    			} else {
    				return  false;
    			}
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    		return  false;
    	}
    }
    用bufferwriter实现写入内容         方法
    public static void bufferedWriter(String path,String[] strs,boolean isNew) throws IOException{
    		FileWriter fwrite=new FileWriter(path, isNew);
                   BufferedWriter bWrite=new BufferedWriter(fwrite);
                  for (String string : strs) {
                            bWrite.write(string);
                            bWrite.newLine();
    		}
    		bWrite.flush();
                    bWrite.close();
    	}
    9.转换流(处理流之二)
    转换流:inputStreamreader    outputStreamwriter   
    编码:字符串-->字节数组相当于 outputStreamwriter   
    解码:字节数组-->字符串相当于  inputStreamreader 
    publicvoid testMyInput() throws Exception{
        FileInputStream fis = new FileInputStream("dbcp.txt");
        FileOutputStream fos = new FileOutputStream("dbcp5.txt");
    
        InputStreamReader isr = new InputStreamReader(fis,"GBK");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
    
        BufferedReader br = newBufferedReader(isr);
        BufferedWriter bw = new BufferedWriter(osw);
    
        String  str = null;
        while((str = br.readLine()) != null){
            bw.write(str);
            bw.newLine();
            bw.flush();
    }    bw.close();  br.close();}
    10.标准输入输出流:处理流之三
    FileInputStream fis=new FileInputStream("d:\c.txt");
    		//一种读取数据的简单方式 用 fileinputstrea
    		System.setIn(fis);//将默认从键盘录入改为从指定文件流录入,
    Scanner scanner=
    new Scanner(System.in); String xString=scanner.next(); System.out.println("读取到的文件为"+xString);


                    PrintStream psPrintStream=null;
    		psPrintStream=new PrintStream(new FileOutputStream("d:\c.txt"));
    		System.setOut(psPrintStream);//将默认从控制台输出改为输出到文件流
    		System.out.println("qwertyuytrewdfgfdfgh");
    		psPrintStream.close();
    System.out.println("请输入信息(退出输入e或exit):");
    //把"标准"输入流(键盘输入)这个字节流包装成字符流,再包装成缓冲流BufferedReader br = newBufferedReader(
    	new InputStreamReader(System.in));
    String s = null;
    try {
    while ((s = br.readLine()) != null) {  //读取用户输入的一行数据 --> 阻塞程序if (s.equalsIgnoreCase("e") || s.equalsIgnoreCase("exit")) {
    	 System.out.println("安全退出!!");
    		break;
    }
    /将读取到的整行字符串转成大写输出
    System.out.println("-->:"+s.toUpperCase());
    System.out.println("继续输入信息");
    }	} catch (IOException e) {
    	e.printStackTrace();
    	} finally { 
    	try {
    	if (br != null) {
    	br.close();  //关闭过滤流时,会自动关闭它包装的底层节点流
    }	} catch (IOException e) {
    	e.printStackTrace();
    }	}
    11.处理流之四:打印流(了解)
    FileOutputStream fos = null;
    	try {
    		fos = new FileOutputStream(newFile("D:\IO\text.txt"));
    	} catch (FileNotFoundException e) {
    		e.printStackTrace();
    	}//创建打印输出流,设置为自动刷新模式(写入换行符或字节 '
    ' 时都会刷新输出缓冲区)
    	PrintStream ps = new PrintStream(fos,true);
    	if (ps != null) {	// 把标准输出流(控制台输出)改成文件
    		System.setOut(ps);}
    	for (int i = 0; i <= 255; i++) {  //输出ASCII字符
    		System.out.print((char)i);
    		if (i % 50 == 0) {   //每50个数据一行
    			System.out.println(); // 换行
    		}  }
    	ps.close();
    12.数据流(了解):处理流之五
    DataOutputStream dos = null;
    	try {	//创建连接到指定文件的数据输出流对象
    		dos = new DataOutputStream(new FileOutputStream(
    					"d:\IOTest\destData.dat"));
    			dos.writeUTF("ab中国");  //写UTF字符串
    			dos.writeBoolean(false);  //写入布尔值
    			dos.writeLong(1234567890L);  //写入长整数
    			System.out.println("写文件成功!");
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {	//关闭流对象try {
    			if (dos != null) {
    			// 关闭过滤流时,会自动关闭它包装的底层节点流
    			dos.close(); 
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}	}

    13.对象流:处理流之六
    序列化:将对象写入到磁盘或者进行网络传输。
    要求对象必须实现序列化
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test3.txt"));
    Person p = new Person("韩梅梅",18,"中华大街",new Pet());
    oos.writeObject(p);
    oos.flush();
    oos.close();
    //反序列化:将磁盘中的对象数据源读出。
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test3.txt"));
    Person p1 = (Person)ois.readObject();
    System.out.println(p1.toString());
    ois.close();
    14.randomAccessFile类
    /*
     * RandomAccessFile:支持随机访问
     * 1.既可以充当一个输入流,有可以充当一个输出流
     * 2.支持从文件的开头读取、写入
     * 3.支持从任意位置的读取、写入(插入)
     */publicclass TestRandomAccessFile {
    //实现插入的效果:在d字符后面插入“xy”
    publicvoid test4(){ RandomAccessFile raf = null; try { raf = new RandomAccessFile(newFile("hello1.txt"),"rw"); raf.seek(4); byte[] b = newbyte[10]; int len; StringBuffer sb = new StringBuffer(); while((len = raf.read(b)) != -1){ sb.append(new String(b,0,len)); } raf.seek(4); raf.write("xy".getBytes()); raf.write(sb.toString().getBytes()); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(raf != null){ try { raf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
  • 相关阅读:
    pom.xml配置
    Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
    jar文件运行打断点
    prim算法
    Kruscal algorithm
    递归程序设计思想(看图思考2小时)
    广义表的实现(法二)
    广义表的实现
    有错误的地宫寻宝问题
    写一个数的所有加法算式
  • 原文地址:https://www.cnblogs.com/gmj0113/p/5665618.html
Copyright © 2011-2022 走看看