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

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("使用字符流拷贝失败!");
            }
        }
    }
    
    
    点赞 评论
  • danielinbiti 2015-07-08 12:20
    点赞 评论
  • frank_20080215 2015-07-08 14:32
    import java.io.*;  
    public class FileOutputStreamTest   
    {  
        public static void main(String[] args) throws IOException  
        {  
            FileInputStream fis = null;  
            FileOutputStream fos = null;  
            try  
            {  
                //创建字节输入流  
                fis = new FileInputStream("FileOutputStreamTest.java");  
                //创建字节输出流  
                fos = new FileOutputStream("newFile.txt");  
                byte[] bbuf = new byte[32];  
                int hasRead = 0;  
                //循环从输入流中取出数据  
                while((hasRead = fis.read(bbuf)) > 0)  
                {  
                    //每读取一次,即写入文件输出流,读了多少,就写多少  
                    fos.write(bbuf,0,hasRead);  
                }  
            }  
            catch (IOException ioe)  
            {  
                ioe.printStackTrace();  
            }  
            finally  
            {  
                if(fis != null)  
                {  
                    fis.close();  
                }  
                if(fos != null)  
                {  
                    fos.close();  
                }  
            }  
        }  
    }  
    
    点赞 评论
  • blownewbee 2015-07-08 14:45
     import java.io.*; 
    public class CopyTest{ 
        public static void main(String[] args) throws Exception{ 
            String src = args[0]; 
            String dest = args[1]; 
            FileInputStream fis = new FileInputStream(src); 
            FileOutputStream fos = new FileOutputStream(dest); 
            int read = fis.read(); 
            while(read!= -1){ 
                fos.write(read); 
                read = fis.read(); 
            } 
            fos.flush(); 
            fos.close(); 
        } 
    } 
    
    点赞 评论

相关推荐 更多相似问题