zhang-chang-qi 2015-07-08 12:12 采纳率: 100%
浏览 1682
已采纳

java编程练习题,求解?

这道题不会做了,求程序代码。。。图片

  • 写回答

4条回答 默认 最新

  • JonsonJiao 2015-07-08 15:19
    关注
    package com.test.io;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class JavaIODemo {
    
        /**
         * 使用字符流实现文件拷贝
         * 
         * @param srcFile
         *            源文件
         * @param destFile
         *            目的文件
         * @return
         */
        public boolean copyByChar(File srcFile, File destFile) {
            try {
                FileReader fr = new FileReader(srcFile);
                FileWriter fw = new FileWriter(destFile);
                char[] buff = new char[1024];
                int len = fr.read(buff);
                while (len != -1) {
                    fw.write(buff, 0, len);
                    len = fr.read(buff);
                }
                fr.close();
                fw.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
    
        /**
         * 使用字节流实现文件拷贝功能
         * 
         * @param srcFile
         *            源文件
         * @param destFile
         *            目的文件
         * @param bufferSize
         *            缓冲区大小
         * @return
         */
        public boolean copyByByte(File srcFile, File destFile, int bufferSize) {
            try {
                FileInputStream fins = new FileInputStream(srcFile);
                FileOutputStream fous = new FileOutputStream(destFile);
                byte[] buff = new byte[bufferSize];
                int len = fins.read(buff);
                while (len != -1) {
                    fous.write(buff, 0, len);
                    len = fins.read(buff);
                }
                fins.close();
                fous.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
    
        public static void main(String[] args) {
            JavaIODemo ioDemo = new JavaIODemo();
            String srcFilePath = "src/com/test/io/JavaIODemo.java";
            String destFilePath_Byte = "D:\\JavaIODemo_Byte.txt"; // 字节流拷贝文件
            String destFilePath_Char = "D:\\JavaIODemo_Char.txt"; // 字符流拷贝文件
            File srcFile = new File(srcFilePath);
            File destFile_Char = new File(destFilePath_Char);
            File destFile_Byte = new File(destFilePath_Byte);
    
            // 使用字节流拷贝文件
            int bufferSize = 1024; // 指定缓冲区大小
            boolean flag = ioDemo.copyByByte(srcFile, destFile_Byte, bufferSize);
            if (flag) {
                System.out.println("使用字节流拷贝成功!");
            } else {
                System.out.println("使用字节流拷贝失败!");
            }
    
            // 使用字符流拷贝文件
            flag = ioDemo.copyByChar(srcFile, destFile_Char);
            if (flag) {
                System.out.println("使用字符流拷贝成功!");
            } else {
                System.out.println("使用字符流拷贝失败!");
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?