利用Java的IO流进行操作复制,代码如下,不知道为什么出现了错误 20
packageIOTest;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutp...
package IOTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
String str ;
String desc;
@SuppressWarnings("unused")
public CopyFile(String str,String desc){
File f1 = new File(this.str);
File f2 = new File(this.desc);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(f1);
fos = new FileOutputStream(f2);
byte b[] = new byte[20];
int len ;
while((len = fis.read(b)) != -1){
fos.write(b,0,len);
}
boolean R = f2.exists();
if(R = true){
System.out.println("文件复制成功");
}else{
System.out.println("文件复制失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package IOTest;
import java.util.Scanner;
public class CopyFileTest {
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入需要复制的文件的地址和想复制到的文件地址:");
String a = scanner.nextLine();
String b = scanner.nextLine();
long Start = System.currentTimeMillis();
CopyFile C = new CopyFile(a,b);
long end = System.currentTimeMillis();
System.out.println("时间:"+"end - start");
}
}
输出结果:
输入需要复制的文件的地址和想复制到的文件地址:
C:\\Users\\jianyuan\\Desktop\\1.txt
C:\\Users\\jianyuan\\Desktop\\2.txt
Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at IOTest.CopyFile.<init>(CopyFile.java:15)
at IOTest.CopyFileTest.main(CopyFileTest.java:14) 展开
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
String str ;
String desc;
@SuppressWarnings("unused")
public CopyFile(String str,String desc){
File f1 = new File(this.str);
File f2 = new File(this.desc);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(f1);
fos = new FileOutputStream(f2);
byte b[] = new byte[20];
int len ;
while((len = fis.read(b)) != -1){
fos.write(b,0,len);
}
boolean R = f2.exists();
if(R = true){
System.out.println("文件复制成功");
}else{
System.out.println("文件复制失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package IOTest;
import java.util.Scanner;
public class CopyFileTest {
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入需要复制的文件的地址和想复制到的文件地址:");
String a = scanner.nextLine();
String b = scanner.nextLine();
long Start = System.currentTimeMillis();
CopyFile C = new CopyFile(a,b);
long end = System.currentTimeMillis();
System.out.println("时间:"+"end - start");
}
}
输出结果:
输入需要复制的文件的地址和想复制到的文件地址:
C:\\Users\\jianyuan\\Desktop\\1.txt
C:\\Users\\jianyuan\\Desktop\\2.txt
Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at IOTest.CopyFile.<init>(CopyFile.java:15)
at IOTest.CopyFileTest.main(CopyFileTest.java:14) 展开
1个回答
展开全部
我今天正好写这里,贴给你看看
file1写给file2
FileInputStream is= new FileInputStream(file1);
OutputStream os = new FileOutputStream(file2);
int read = 0;
byte[] buffer = new byte[8096];
while ((read = is.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
file1写给file2
FileInputStream is= new FileInputStream(file1);
OutputStream os = new FileOutputStream(file2);
int read = 0;
byte[] buffer = new byte[8096];
while ((read = is.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
追问
我知道是这样的写的,我的代码在eclipse中也没有显示错误。但是不知道为什么最后运行的时候出了错。想问一下原因
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询