Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 392|回复: 0

复制文件到当前工程下的文件夹,老出错是什么原因?

[复制链接]

该用户从未签到

发表于 2015-5-4 00:04:37 | 显示全部楼层 |阅读模式
10Java金币
  1. package com.machine.study;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import javax.swing.JFileChooser;

  9. public class SystemRead {
  10.         String currentPath;
  11.         String currentFilePath;
  12.         File selectFile;
  13.         JFileChooser fileChoorser =new JFileChooser();//文件选择器
  14.         public SystemRead() throws FileNotFoundException, IOException{
  15.                 int option =fileChoorser.showDialog(null, null);//显示文件选取的对话框
  16.                 selectFile= fileChoorser.getSelectedFile(); //获取选择文件的路径
  17.                 currentPath =System.getProperty("user.dir");//user.dir指定了当前的路径
  18.                 currentFilePath=currentPath+"\\data";       //得到当前文件夹的路径,当前工程下有个data文件夹想把文件复制到data中
  19.                 System.out.println(selectFile);
  20.                 System.out.println(currentFilePath);
  21.                 copyFile(selectFile, currentFilePath);
  22.         }
  23.        
  24.         public void copyFile(File oldFile, String newPath) throws  FileNotFoundException,IOException {
  25.                try {
  26.                    int bytesum = 0;
  27.                    int byteread = 0;
  28.                    
  29.                    if (oldFile.exists()) {                  //文件存在时
  30.                        InputStream inStream = new FileInputStream(oldFile);      //读入原文件
  31.                        FileOutputStream fs = new FileOutputStream(newPath);
  32.                        byte[] buffer = new byte[1444];
  33.                        int length;
  34.                        while ( (byteread = inStream.read(buffer)) != -1) {
  35.                            bytesum += byteread;            //字节数 文件大小
  36.                            System.out.println(bytesum);
  37.                            fs.write(buffer, 0, byteread);
  38.                        }
  39.                        inStream.close();
  40.                    }
  41.                }  catch (Exception e) {
  42.                    System.out.println("复制单个文件操作出错");
  43.                    e.printStackTrace();
  44.                }
  45.            }
  46.        
  47.                
  48.         public static void main(String[] args) throws  IOException {
  49.                         new SystemRead();
  50.                        
  51.         }
  52. }
复制代码
错误信息为:
D:\Documents\美图图库\示例图片_01.jpg
C:\Documents and Settings\康明轩\workspace\机器学习和测试\data
复制单个文件操作出错
java.io.FileNotFoundException: C:\Documents and Settings\康明轩\workspace\机器学习和测试\data (拒绝访问。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at com.machine.study.SystemRead.copyFile(SystemRead.java:33)
at com.machine.study.SystemRead.<init>(SystemRead.java:23)
at com.machine.study.SystemRead.main(SystemRead.java:51)


回复

使用道具 举报

该用户从未签到

 楼主| 发表于 2015-5-4 00:09:25 | 显示全部楼层
大家都睡了么??
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-3-29 05:16 , Processed in 0.316324 second(s), 36 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表