当前位置:编程学习 > JAVA >>

java文件IO操作

 

Java代码 

public static void main(String[] args) { 

        String filename = "F:/zhangming_test/test.txt"; 

        String filename1 = "F:/zhangming_test/test1.txt"; 

        String docfileName = "F:/猪猪猫xp系统.rar"; 

        // String filename = "F:/zhangming_test/test_doc.doc"; 

        // readFile(filename); 

        // copyFileBychar(filename); 

        copyFileByByte(docfileName); 

        System.out.println("ddd"); 

 

    } 

 

    /**

     * 该方法只支持读取字符文件并复制文件. 按行读取的  文件锁未考虑

     * 

     * @param filename

     */ 

    public static void copyFileBychar(String filename) { 

 

        FileReader fileReader = null; 

        BufferedWriter bufferedWriter = null; 

        try { 

            bufferedWriter = new BufferedWriter(new PrintWriter(new File( 

                    "F:/zhangming_test/test2.txt"))); 

            fileReader = new FileReader(new File(filename)); 

        } catch (FileNotFoundException e) { 

            e.printStackTrace(); 

        } 

        BufferedReader bufferedReader = new BufferedReader(fileReader); 

        String linecontent = null; 

        try { 

            // 調用readLine方法會移動流的指針 

            while ((linecontent = bufferedReader.readLine()) != null) { 

                System.out.println(linecontent); 

                bufferedWriter.write(linecontent); 

            } 

            bufferedWriter.flush(); 

        } catch (IOException e) { 

            e.printStackTrace(); 

        } finally { 

            try { 

                bufferedReader.close(); 

                bufferedWriter.close(); 

            } catch (IOException e) { 

                e.printStackTrace(); 

            } 

        } 

    } 

 

    /**

     * 每次读取一定数量byte的文件流. 文件锁未考虑

     * @param inputFileName

     */ 

    public static void copyFileByByte(String inputFileName) { 

        InputStream inputStream = null; 

        OutputStream outputStream = null; 

         

        try { 

            inputStream = new FileInputStream(new File(inputFileName)); 

            // ==方法一(一次性把文件的所有内容都添加读取到byte中)== 

            // inputStream.read(bs); 

            // outputStream = new FileOutputStream(new File( 

            // "F:/zhangming_test/test6.doc")); 

            // outputStream.write(bs); 

 

            // ==方法二== 

            outputStream = new FileOutputStream(new File( 

                    "F:/猪猪猫xp系统1.rar")); 

            byte[] tmpBytes = new byte[1024]; 

            while (inputStream.read(tmpBytes) != -1) { 

                outputStream.write(tmpBytes, 0, tmpBytes.length); 

            } 

            outputStream.flush(); 

        } catch (FileNotFoundException e) { 

            e.printStackTrace(); 

        } catch (IOException e) { 

        } finally { 

            try { 

                outputStream.close(); 

                inputStream.close(); 

            } catch (IOException e) { 

                e.printStackTrace(); 

            } 

        } 

    } 

Java代码 

public static void copyFileWithNio(String srcFile, String destFile) { 

 

        try { 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,