Java习题02

1.Java IO 课堂练习

题目需求:完成文件复制操作
请将附件中的demo1.txt文件拷贝到D:\test目录下。通过程序完成将d:\test\demo1.txt文件内容复制到一个新的文件d:\test\demo2.txt(注意:新文件在程序中使用createNewFile方法创建),并将其中的所有小写字母转换为大写字母,大写字母转换为小写字母。

import java.io.*;

public class Javaio01 {
    public static void main(String[] args) {
        String path1 = "D:" + File.separator + "test" + File.separator + "demo1.txt";
        String path2 = "D:" + File.separator + "test" + File.separator + "demo2.txt";
        File file1 = new File(path1);
        File file2 = new File(path2);
        if(!file2.exists()){
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        InputStream input = null;
        try {
            input = new FileInputStream(file1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(file2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(input!=null && out !=null){
            int temp=0;
            while (true){
                try {
                    if (!((temp=input.read())!=-1)) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (temp >= 'A' && temp <= 'Z') {
                    try {
                        out.write(temp+32);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                else if(temp>='a'&&temp<='z'){
                    try {
                        out.write(temp-32);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else if(temp==','){
                    try {
                        out.write(temp);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else if(temp=='.'){
                    try {
                        out.write(temp);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }


    }
}

2.Java IO 课堂练习

1.附件中有一张图片,文件名为1.jpg,编写一个程序,通过main方法传递源图片地址和目标地址,实现图片的复制。例如:类名为Test,通过调用java Test d:\test\1.jpg d:\test\2.jpg,完成将d盘test目录下的1.jpg图片复制为d盘test目录下的2.jpg图片。
提示:图片文件的内容是二进制代码表示时,通过字节流实现图片的复制。

2.附件中有一个文本文件demo1.txt,编写程序,统计文件中指定的单词出现次数,并在屏幕上输入结果。
例如:类名为Test,通过调用java Test d:\test\demo1.txt Trump,
屏幕输出:Trump出现次数:3。

import java.io.*;
import java.util.regex.*;

public class Javaio02 {
    public static void main(String[] args) {
        String path = "D:" + File.separator + "test" + File.separator + "demo1.txt";
        BufferedReader file = null;
        try {
            file = new BufferedReader(new FileReader(path));
            StringBuffer sb = new StringBuffer();
            String str = null;
            while((str = file.readLine()) != null) {
                sb.append(str);
            }

            String target = "Trump";
            Pattern pattern = Pattern.compile(target);
            Matcher matcher = pattern.matcher(sb);

            int num = 0;
            while(matcher.find()) {
                num++;
            }

            System.out.println("’Trump‘出现的次数为: " + num);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(null != file) {
                    file.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
import java.io.*;

public class Javaio03 {
    public static void main(String[] args) {
        String path1 = "D:" + File.separator + "test" + File.separator + "1.jpg";
        String path2 = "D:" + File.separator + "test" + File.separator + "2.jpg";
        FileInputStream input = null;
        FileOutputStream out=null;
        try {
            input=new FileInputStream(path1);
            out=new FileOutputStream(path2);
            int len=0;
            byte [] buf=new byte[1024];
            while((len=input.read(buf))!=-1) {
                out.write(buf, 0, len);
            }
            System.out.println("复制成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                input.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            if(out!=null) {
                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

posted @ 2021-11-16 12:29  壹怪  阅读(284)  评论(0编辑  收藏  举报