A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小K哥 中级黑马   /  2016-5-4 22:41  /  372 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

(4)需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合

                          分析:
                                        通过题目的意思我们可以知道如下的一些内容,
                                                数据源是一个文本文件。
                                                目的地是一个集合。
                                                而且元素是字符串。                  
                          数据源:
                                        b.txt -- FileReader -- BufferedReader
                          目的地:
                                        ArrayList<String>
        代码实现:
                public class FileToArrayListDemo {
                        public static void main(String[] args) throws IOException {
                                // 封装数据源
                                BufferedReader br = new BufferedReader(new FileReader("b.txt"));
                                // 封装目的地(创建集合对象)
                                ArrayList<String> array = new ArrayList<String>();
                                // 读取数据存储到集合中
                                String line = null;
                                while ((line = br.readLine()) != null) {
                                        array.add(line);
                                }
                                // 释放资源
                                br.close();
                                // 遍历集合
                                for (String s : array) {
                                        System.out.println(s);
                                }
                        }
                }
        (5)需求:我有一个文本文件中存储了几个名称,请大家写一个程序实现随机获取一个人的名字。
               
                分析:
                                A:把文本文件中的数据存储到集合中
                                B:随机产生一个索引
                                C:根据该索引获取一个值
        代码实现:
                public class GetName {
                        public static void main(String[] args) throws IOException {
                                // 把文本文件中的数据存储到集合中
                                BufferedReader br = new BufferedReader(new FileReader("b.txt"));
                                ArrayList<String> array = new ArrayList<String>();
                                String line = null;
                                while ((line = br.readLine()) != null) {
                                        array.add(line);
                                }
                                br.close();
                                // 随机产生一个索引
                                Random r = new Random();
                                int index = r.nextInt(array.size());
                                // 根据该索引获取一个值
                                String name = array.get(index);
                                System.out.println("该幸运者是:" + name);
                        }
                }
        (6)需求:复制单极文件夹
                          
                          数据源:e:\\demo
                          目的地:e:\\test                          
                          分析:
                                          A:封装目录
                                          B:获取该目录下的所有文本的File数组
                                          C:遍历该File数组,得到每一个File对象
                                          D:把该File进行复制
        代码实现:
                public class CopyFolderDemo {
                        public static void main(String[] args) throws IOException {
                                // 封装目录
                                File srcFolder = new File("e:\\demo");
                                // 封装目的地
                                File destFolder = new File("e:\\test");
                                // 如果目的地文件夹不存在,就创建
                                if (!destFolder.exists()) {
                                        destFolder.mkdir();
                                }
                                // 获取该目录下的所有文本的File数组
                                File[] fileArray = srcFolder.listFiles();
                                // 遍历该File数组,得到每一个File对象
                                for (File file : fileArray) {
                                        // System.out.println(file);
                                        // 数据源:e:\\demo\\e.mp3
                                        // 目的地:e:\\test\\e.mp3
                                        String name = file.getName(); // e.mp3
                                        File newFile = new File(destFolder, name); // e:\\test\\e.mp3
                                        copyFile(file, newFile);
                                }
                        }
                        private static void copyFile(File file, File newFile) throws IOException {
                                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                                                file));
                                BufferedOutputStream bos = new BufferedOutputStream(
                                                new FileOutputStream(newFile));
                                byte[] bys = new byte[1024];
                                int len = 0;
                                while ((len = bis.read(bys)) != -1) {
                                        bos.write(bys, 0, len);
                                }
                                bos.close();
                                bis.close();
                        }
                }
        (7)需求:复制多极文件夹
                  
                  数据源:E:\JavaSE\day21\code\demos
                  目的地:E:\\                  
                          分析:
                                        A:封装数据源File
                                        B:封装目的地File
                                        C:判断该File是文件夹还是文件
                                                a:是文件夹
                                                        就在目的地目录下创建该文件夹
                                                        获取该File对象下的所有文件或者文件夹File对象
                                                        遍历得到每一个File对象
                                                        回到C
                                                b:是文件
                                                        就复制(字节流)
        代码实现:
                public class CopyFoldersDemo {
                        public static void main(String[] args) throws IOException {
                                // 封装数据源File
                                File srcFile = new File("E:\\JavaSE\\day21\\code\\demos");
                                // 封装目的地File
                                File destFile = new File("E:\\");
                                // 复制文件夹的功能
                                copyFolder(srcFile, destFile);
                        }
                        private static void copyFolder(File srcFile, File destFile)
                                        throws IOException {
                                // 判断该File是文件夹还是文件
                                if (srcFile.isDirectory()) {
                                        // 文件夹
                                        File newFolder = new File(destFile, srcFile.getName());
                                        newFolder.mkdir();
                                        // 获取该File对象下的所有文件或者文件夹File对象
                                        File[] fileArray = srcFile.listFiles();
                                        for (File file : fileArray) {
                                                copyFolder(file, newFolder);
                                        }
                                } else {
                                        // 文件
                                        File newFile = new File(destFile, srcFile.getName());
                                        copyFile(srcFile, newFile);
                                }
                        }
                        private static void copyFile(File srcFile, File newFile) throws IOException {
                                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                                                srcFile));
                                BufferedOutputStream bos = new BufferedOutputStream(
                                                new FileOutputStream(newFile));
                                byte[] bys = new byte[1024];
                                int len = 0;
                                while ((len = bis.read(bys)) != -1) {
                                        bos.write(bys, 0, len);
                                }
                                bos.close();
                                bis.close();
                        }
                }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马