J2EE练习及面试题_chapter13 IO流_下

简介: J2EE练习及面试题_chapter13 IO流_下

题目24

/*
列出当前目录下全部java文件的名称
 */
    @Test
    public void test4() {
        File file = new File("E:\\CodeLife\\IdeaProject\\JVM\\chapter13");
        for (String s : file.list()) {
            if (s.startsWith("java")) {
                System.out.println(s);
            }
        }
    }

题目25

/*
列出d:\ch9目录下Java源文件的名字及其大小,并删除其中的一个Java源文件?
 */
    @Test
    public void test5() {
        File file = new File("D:\\ch9");
        for (String s : file.list()) {
            System.out.println(s);
            if (s.equalsIgnoreCase("hello.java")) {
            }
        }
        System.out.println(file.length());
    }

题目26

/*
使用File类下的常用方法获取某些文件的信息
 */
    @Test
    public void test6() throws IOException {
        File file = new File("test1.txt");
        System.out.println(file.createNewFile());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getName());
        System.out.println(file.length());
        System.out.println(file.lastModified());
        System.out.println(file.isDirectory());
        System.out.println(file.isFile());
        System.out.println(file.exists());
        System.out.println(file.canRead());
        System.out.println(file.canWrite());
        System.out.println(file.isHidden());
    }

题目27

/*
操作D盘下的my.txt文件
1)  判断my.txt文件是否存在
2)  若存在则删除;若不存在则创建
 */
    @Test
    public void test7() throws IOException {
        File file = new File("D:\\my.txt");
        if (file.exists()) {
            System.out.println(file.delete());
            System.out.println("删除成功!");
        } else {
            System.out.println(file.createNewFile());
            System.out.println(file.getName() + "创建成功!");
        }
    }

题目28

/*
使用File类删除某个文件夹(例如D盘下的temp文件夹)下的所有文件和文件夹:
1)  判断temp文件夹下的文件类型,如果是文件则直接删除
2)  如果是文件夹则获取该文件夹下的子文件和文件夹
3)  使用递归的方式删除所有temp文件夹下的文件和文件夹
 */
    @Test
    public void test8() {
        File dir = new File("D:\\temp");
        printSubFile(dir);
        deleteDir(dir);
    }
    public static void printSubFile(File dir) {
        if (dir != null) {
            for (File subFile : dir.listFiles()) {
                if (subFile.isDirectory()) {
                    printSubFile(subFile);
                } else {
                    System.out.println(subFile.getAbsolutePath());
                }
            }
        }
    }
    public static void deleteDir(File file) {
        if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                deleteDir(listFile);
            }
        }
        file.delete();
    }

题目29

/*
利用IO操作文件
1)利用代码在D盘下创建news文件夹。
2)利用代码在news文件夹下创建2个.docx文件,2个.java文件,2个.txt的文件(在控制台打印news文件夹下的.java文件
 */
    @Test
    public void test9() throws IOException {
        File dir = new File("D:\\news");
        if (!dir.exists()) {
            dir.mkdir();
        }
        File file1 = new File(dir, "1.docx");
        if (!file1.exists()) {
            file1.createNewFile();
        }
        File file2 = new File(dir, "2.docx");
        if (!file2.exists()) {
            file2.createNewFile();
        }
        File file3 = new File(dir, "1.java");
        if (!file3.exists()) {
            file3.createNewFile();
        }
        File file4 = new File(dir, "2.java");
        if (!file4.exists()) {
            file4.createNewFile();
        }
        File file5 = new File(dir, "1.txt");
        if (!file5.exists()) {
            file5.createNewFile();
        }
        File file6 = new File(dir, "2.txt");
        if (!file6.exists()) {
            file6.createNewFile();
        }
        //在控制台打印news文件夹下的.java文件
        String[] fileName = dir.list();
        for (String s : fileName) {
            if (s.endsWith("java")) {
                System.out.println(s);
            }
        }
    }

题目30

/*
利用IO流操作文件
1)利用Java代码创建D:\temp\temp1\temp2共3个文件夹
2)在temp文件夹下创建一个1.txt文件内容为hello,创建一个Hello.java文件
3)内容为public static void main(String[] args){},在temp1文件夹下创建同样的两个文件
4)输出temp文件夹下包括其子文件夹下,所有的.java文件
 */
    @Test
    public void test10() throws IOException {
        File dir = new File("D:\\temp\\temp1\\temp2");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file1 = new File("D:\\temp", "1.txt");
        if (!file1.exists()) {
            file1.createNewFile();
        }
        FileWriter fw1 = new FileWriter(file1, true);
        fw1.write("hello");
        fw1.close();
        File file2 = new File("D:\\temp", "Hello.java");
        if (!file2.exists()) {
            file2.createNewFile();
        }
        FileWriter fw2 = new FileWriter(file2, true);
        fw2.write("public static void main(String[] args){}");
        fw2.close();
        File file3 = new File("D:\\temp\\temp1", "1.txt");
        if (!file3.exists()) {
            file3.createNewFile();
        }
        File file4 = new File("D:\\temp\\temp1", "Hello.java");
        if (!file4.exists()) {
            file4.createNewFile();
        }
        File dir1 = new File("D:\\temp");
        printSubFile(dir1);
    }
    public static void printSubFileName(File dir) {
        if (dir.isDirectory()) {
            File[] subDir = dir.listFiles();
            for (File file : subDir) {
                printSubFileName(file);
            }
        } else {
            String[] fileNames = dir.list();
            for (String fileName : fileNames) {
                if (fileName.endsWith(".java")) {
                    System.out.println(fileName);
                }
            }
        }
    }

题目31

/*
利用IO流操作文件
1)  利用java代码在D盘下创建一个mytemp文件夹
2)  显示D盘下所有的.Java文件,包括D盘的子文件夹下的.java文件
3)  把上述显示的文件都复制到mytemp文件夹中
 */
    @Test
    public void test11() throws IOException {
        File dir = new File("D:\\myTemp");
        if (!dir.exists()) {
            dir.mkdir();
        }
        File dir1 = new File("D:\\");
        printSubFile(dir1);
    }

题目32

/*
先将“欢迎您来北京”写入到文件”hello2.txt”中,再读取该文件中的内容。
 */
    @Test
    public void test12() throws IOException {
        FileWriter fw = new FileWriter(new File("hello2.txt"));
        fw.write("欢迎您来北京");
        fw.close();
        FileReader fr = new FileReader("hello2.txt");
        char[] cbuf = new char[10];
        int len;
        while ((len = fr.read(cbuf)) != -1) {
            System.out.println(new String(cbuf, 0, len));
        }
        fr.close();
    }

题目33

/*
 编写一个应用程序,将用户从键盘输入的10个整数存入文件,再顺序读出。
 */
    @Test
    public void test13() throws IOException {
        System.out.println("用户从键盘输入的10个整数存入文件: ");
        FileWriter fw = new FileWriter(new File("num.txt"));
        for (int i = 0; i < 10; i++) {
            int num = (int) (Math.random() * 10);
            fw.write(String.valueOf(num));
        }
        fw.close();
        FileReader fr = new FileReader("num.txt");
        char[] cbuf = new char[10];
        int len;
        while ((len = fr.read(cbuf)) != -1) {
            System.out.println(new String(cbuf, 0, len));
        }
        fr.close();
    }

题目34

/*
编写程序向文本文件中写入自己的信息,格式为:姓名:XXX 性别:X 年龄:XX 班级:XXX,将该信息读出后显示的屏幕上后把文件删除
 */
    @Test
    public void test14() throws IOException {
        File file = new File("myInfo.txt");
        FileWriter fw = new FileWriter(file);
        fw.write("姓名:Jerry");
        fw.write("性别:M");
        fw.write("年龄:18");
        fw.write("班级:2022");
        fw.flush();
        fw.close();
        FileReader fr = new FileReader("myInfo.txt");
        char[] cbuf = new char[20];
        int len;
        while ((len = fr.read(cbuf)) != -1) {
            System.out.println(new String(cbuf, 0, len));
        }
        fr.close();
        if (file.exists()) {
            System.out.println(file.delete());
        }
    }

题目35

/*
用缓冲字节流实现文件复制的功能
1)  首先判断d盘是否存在a.txt文件。
2)  若不存在则创建a.txt文件,然后把a.txt文件复制成acopy.txt
 */
    @Test
    public void test15() throws IOException {
        File file = new File("D:\\a.txt");
        if (!file.exists()) {
            file.createNewFile();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            File file1 = new File("aCopy.txt");
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
            byte[] buffer = new byte[10];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
                bos.flush();
            }
            bos.close();
            bis.close();
        }
    }

题目36

/*
通过Io流实现MP3文件创建和复印功能
1)  判断d:/歌曲.mp3文件是否存在
2)  若不存在则创建d:/歌曲.mp3文件,创建完成后复制到 e:/歌曲.mp3
 */
    @Test
    public void test16() throws IOException {
        File file = new File("D:\\歌曲.mp3");
        if (!file.exists()) {
            file.createNewFile();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            File file1 = new File("F:\\歌曲.mp3");
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
            byte[] buffer = new byte[10];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
                bos.flush();
            }
            bos.close();
            bis.close();
        }
    }

题目37

/*
用(字节流)读取一张图片,读进来之后再输出到另一个文件中。
 */
    @Test
    public void test17() throws IOException {
        FileInputStream fis = new FileInputStream("java.jpg");
        FileOutputStream fos = new FileOutputStream("D:\\myTemp");
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
            fos.flush();
        }
        fos.close();
        fis.close();
    }

题目38

/*
(字符流)读取一个文本文件,每行都追加一个“好”,在文件结尾再追加“完毕”。
 */
    @Test
    public void test18() throws IOException {
        FileReader fr = new FileReader("dbcp.txt");
        FileWriter fw = new FileWriter("dbcp_2.txt", true);
    }

题目39

/*
用缓冲字符流读入一个文件中的内容,并把内容输出到一个新的文件中
 */
    @Test
    public void test19() throws IOException {
        RandomAccessFile raf = new RandomAccessFile("dbcp.txt", "r");
        long length = raf.length();
        raf.seek(length);
        byte[] buffer = new byte[1024];
        int off = 0;
        int len = 5;
        raf.read(buffer, off, len);
        System.out.println(new String(buffer, off, len));
        raf.close();
    }

END

相关文章
|
4月前
|
存储 网络协议 Java
程序员的23大IO&NIO面试问题及答案
程序员的23大IO&NIO面试问题及答案
|
5月前
|
安全 网络安全 数据安全/隐私保护
CocosCreator 面试题(十四)Cocos Creator WebSocket 、Socket.IO分别是什么?
CocosCreator 面试题(十四)Cocos Creator WebSocket 、Socket.IO分别是什么?
127 0
|
8天前
|
存储 Java 数据库
[Java 基础面试题] IO相关
[Java 基础面试题] IO相关
|
7月前
|
Java
【面试题精讲】Java IO 模型
【面试题精讲】Java IO 模型
|
6月前
|
存储 网络协议 安全
探索Java通信面试的奥秘:揭秘IO模型、选择器和网络协议,了解面试中的必备知识点!
通过深入探索Java通信面试的奥秘,我们将揭秘Java中的三种I/O模型(BIO、NIO和AIO)、选择器(select、poll和epoll)以及网络协议(如HTTP和HTTPS),帮助您了解在面试中必备的知识点。这些知识点对于网络编程和系统安全方面的求职者来说至关重要,掌握它们将为您的职业发展打下坚实的基础!
|
7月前
|
Java C++
多线程使用HashMap,HashMap和HashTable和ConcurrentHashMap区别(面试题常考),硬盘IO,顺便回顾volatile(二)
多线程使用HashMap,HashMap和HashTable和ConcurrentHashMap区别(面试题常考),硬盘IO,顺便回顾volatile
|
7月前
|
存储 安全 Java
多线程使用HashMap,HashMap和HashTable和ConcurrentHashMap区别(面试题常考),硬盘IO,顺便回顾volatile(一)
多线程使用HashMap,HashMap和HashTable和ConcurrentHashMap区别(面试题常考),硬盘IO,顺便回顾volatile
|
7月前
|
Java 文件存储
每日一道面试题之java 中 IO 流分为几种?
每日一道面试题之java 中 IO 流分为几种?
|
2月前
|
Java 程序员
java线程池讲解面试
java线程池讲解面试
62 1
|
3月前
|
存储 关系型数据库 MySQL
2024年Java秋招面试必看的 | MySQL调优面试题
随着系统用户量的不断增加,MySQL 索引的重要性不言而喻,对于后端工程师,只有在了解索引及其优化的规则,并应用于实际工作中后,才能不断的提升系统性能,开发出高性能、高并发和高可用的系统。 今天小编首先会跟大家分享一下MySQL 索引中的各种概念,然后介绍优化索引的若干条规则,最后利用这些规则,针对面试中常考的知识点,做详细的实例分析。
253 0
2024年Java秋招面试必看的 | MySQL调优面试题