java 實現簡單的文件復制功能


  • 話不多說 上代碼
  • maven依賴
1 <dependency>
2   <groupId>org.springframework</groupId>
3   <artifactId>spring-core</artifactId>
4   <version>5.2.2.RELEASE</version>
5</dependency>
  • 測試類
  • public class IoTest {
        @Test
        public void fileCopyTest(){
            try {
                // 源目錄
                String oldfilePath = "C:\\Users\\Desktop\\測試數據.txt";
                // 目的地
                String newfilePath = "C:\\Users\\Desktop\\test";
                // 目的地文件名稱
                String fileName = "test.txt";
                fileCopy(oldfilePath,newfilePath,fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void fileCopy(String oldfilePath, String newfilePath, String fileName) throws IOException {
            // 源目錄
            File oldPath = new File(oldfilePath);
            // 目的地
            File newPath = new File(newfilePath);
            //判斷該文件是否存在 為否則創建
            if(!newPath.exists()){
                newPath.mkdirs();
            }
            InputStream input = new FileInputStream(oldPath);
            FileOutputStream output = new FileOutputStream(newPath+File.separator+fileName);
            FileCopyUtils.copy(input,output);
            input.close();
            output.close();
        }
    }

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM