本帖最后由 张少威 于 2012-6-4 19:43 编辑
7、编写程序拷贝一个文件. 尽量使用效率高的方式。(15分)
我用两种方式,第一种方式没有问题,代码如下:- @Test
- public void testCopyFile() throws IOException {
- File file = new File("D:\\googleLogo.jpg.bak.jpg");
- if (file.exists()) {
- file.delete();
- }
- String fileName = "D:\\googleLogo.jpg";
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName + ".bak.jpg"));
- byte[] temp = new byte[1024];
- while(bis.read(temp) != -1){
- bos.write(temp);
- }
- bos.close();
- bis.close();
- }
复制代码 以下是第二种方式的代码:- @Test
- public void testProperties() throws IOException {
- boolean flag = System.getProperty("os.name").contains("Win");
- if (!flag) {
- return;
- }
- Runtime runtime = Runtime.getRuntime();
- String cmd = "copy /y d:\\googleLogo.jpg d:\\googleLogo.jpg.bak.jpg";
- Process p = runtime.exec(cmd);
- p.destroy();
- }
复制代码 第二种方式会抛出以下异常:- java.io.IOException: Cannot run program "copy": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
- at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
- at java.lang.Runtime.exec(Runtime.java:593)
- at java.lang.Runtime.exec(Runtime.java:431)
- at java.lang.Runtime.exec(Runtime.java:328)
- at com.itheima.CommonTest.testProperties(CommonTest.java:313)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
- at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
- at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:46)
- at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
- at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
- at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
- at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:1)
- at org.junit.runners.ParentRunner$3.run(ParentRunner.java:246)
- at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:56)
- at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:244)
- at org.junit.runners.ParentRunner.access$0(ParentRunner.java:242)
- at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:237)
- at org.junit.runners.ParentRunner.run(ParentRunner.java:289)
- at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
- at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
- at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:182)
- at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
- Caused by: java.io.IOException: CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
- at java.lang.ProcessImpl.create(Native Method)
- at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
- at java.lang.ProcessImpl.start(ProcessImpl.java:30)
- at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
- ... 30 more
复制代码 |