黑马程序员技术交流社区

标题: 根据指定文件路径拷贝文件 [打印本页]

作者: て淡莣了陌生    时间: 2013-5-15 17:07
标题: 根据指定文件路径拷贝文件
本帖最后由 て淡莣了陌生 于 2013-5-16 15:34 编辑

package cn.itcast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

public class File_Exercise {

        /**
         * @param args
         */
        public static void main(String[] args) throws IOExceptio {
                System.out.println("请输入要拷贝的文件路径");
                Scanner scanner = new Scanner(System.in);
               
                while(true){
                        String path = scanner.nextLine();
                        String fileName = path.substring(path.lastIndexOf("\\")+1);
                        
                        try(
                                FileInputStream fis = new FileInputStream(path);
                                FileOutputStream fos = new FileOutputStream(fileName);
                        ){
                                byte[] buffer = new byte[1024];
                                int len;
                                while((len = fis.read(buffer))!=-1)
                                        fos.write(buffer,0,len);
                                break;
                        }catch(Exception e){
                                System.out.println("您输入的路径有误,请重新输入:");
                        }
                }
               
                scanner.close();
                System.out.println();
        }

}
这段代码,应该没什么问题吧,但是在eclipse中无论我怎样输入文件路径都说"您输入的路径有误,请重新输入"呢??研究了一个小时了都不知道怎么回事,为什么总是执行System.out.println("您输入的路径有误,请重新输入:");
求解啊,谢谢了!
作者: 殇_心。    时间: 2013-5-15 17:43
本帖最后由 殇_心。 于 2013-5-15 17:45 编辑

确实你路径输入错误了。
你可以按你的代码打印path然后filename  你就可以看出错误了。

我举个正确例子吧:
E:\\1.txt   注意是双杆  拷贝的是1.txt
作者: て淡莣了陌生    时间: 2013-5-16 15:34
殇_心。 发表于 2013-5-15 17:43
确实你路径输入错误了。
你可以按你的代码打印path然后filename  你就可以看出错误了。

谢谢您的帮助,现在找到问题了,我在eclipse中运行是只要输入的路径带有中文它就会提示输入的路径错误,我试了一下不带中文的就可以跑了!
作者: 殇_心。    时间: 2013-5-16 15:37
て淡莣了陌生 发表于 2013-5-16 15:34
谢谢您的帮助,现在找到问题了,我在eclipse中运行是只要输入的路径带有中文它就会提示输入的路径错误,我试 ...

嘿嘿。  问题解决了就好```




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2