zoukankan      html  css  js  c++  java
  • io 流demo

    /*
     * 需求:作业:将c盘的一个文本文件复制到d盘。
     *
     * 思路:
     * 1,需要读取源,
     * 2,将读到的源数据写入到目的地。
     * 3,既然是操作文本数据,使用字符流。
     *
     */

    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;

        /**
         * @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();
                    }
            }
        }

    }

    //使用装饰流

    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");
            
            char[] buf = new char[1024];
            
            int len = 0;
            while((len=fr.read(buf))!=-1){
                System.out.println(new String(buf,0,len));
            }
            
            fr.close();
        }

    }

    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();
            }
            
            
            //使用缓冲区的刷新方法将数据刷目的地中。
    //        bufw.flush();
            
            
            //关闭缓冲区。其实关闭的就是被缓冲的流对象。
            bufw.close();
            
    //        fw.write("hehe");
            
    //        fw.close();
        }

    }

    //文本流的复制

    public static void main(String[] args) throws IOException {

            FileReader fr = new FileReader("buf.txt");        
            BufferedReader bufr = new BufferedReader(fr);
            
            FileWriter fw = new FileWriter("buf_copy.txt");
            BufferedWriter bufw = new BufferedWriter(fw);
            
            
            String line = null;
            while((line=bufr.readLine())!=null){
                bufw.write(line);
                bufw.newLine();
                bufw.flush();
            }
            
            
            
            
            /*
            int ch = 0;
            
            while((ch=bufr.read())!=-1){
                
                bufw.write(ch);
            }
            */
            bufw.close();
            bufr.close();
        }

        public static void main(String[] args) {
            File file = new File("E:/dyz");
            if(!file.exists()){
                 System.out.println(file + " not exists");
                return ;
        }else{
            showFiles (file);
        }
        }

        private static void showFiles(File file) {
            File fa[] = file.listFiles();
                    for (int i = 0; i < fa.length; i++) {
                         File fs = fa[i];
                         if (fs.isDirectory()) {
                             System.out.println(fs.getName() + " [目录]");
                             showFiles(fs);
                         } else {
                             System.out.println(fs.getName());
                         }
                     }
            
        }

  • 相关阅读:
    QQ浏览器X5内核问题汇总
    jQuery全屏滚动插件fullPage.js
    CSS3 Animation
    CSS3 Transition
    CSS3 Transform
    HTML5学习笔记(2):input type file的特性
    HTML5学习笔记(1):HTML5介绍与语法
    你必须知道的28个HTML5特征、窍门和技术
    Java内存释放——《Thinking in Java》随笔004
    构造器调用构造器——《Thinking in Java》随笔003
  • 原文地址:https://www.cnblogs.com/chizizhixin/p/5424266.html
Copyright © 2011-2022 走看看