상용 파일 복사 방법
8222 단어 자바
package com.java.copyFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyByInputStreamAndOutputStream {
public static void main(String[] args) {
long time1=System.currentTimeMillis();
// File source=new File("E:\\Java .pdf");
// File target=new File("F:\\Java .a");
File source=new File("F:\\ \\[ -www.dy2018.net] BD .rmvb");
File target=new File("E:\\Java .a");
try {
Copy( source, target);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
}
public static void Copy(File source,File target) throws IOException{
if(target.exists())
target.delete();
FileInputStream fis=new FileInputStream(source);
FileOutputStream fos=new FileOutputStream(target);
byte bys[]=new byte[1024];
int len=0;
while((len=fis.read(bys))!=-1)
fos.write(bys);
fis.close();
fos.close();
}
}
2. 바이트 버퍼 흐름
package com.java.copyFile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyByBufferInputStreamAndBufferOutPutStream {
public static void main(String[] args) {
long time1=System.currentTimeMillis();
File source=new File("F:\\ \\[ -www.dy2018.net] BD .rmvb");
File target=new File("E:\\Java .a");
try {
Copy( source, target);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
}
private static void Copy(File source, File target) throws IOException {
if(target.exists())
target.delete();
FileInputStream fis=new FileInputStream(source);
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream(target);
BufferedOutputStream bos=new BufferedOutputStream(fos);
int len=0;
StringBuffer buufar=new StringBuffer();
byte by[]=new byte[1024];
while((len=bis.read(by))!=-1)
bos.write(by);
bos.flush();
bis.close();
bos.close();
}
}
3. 문자 흐름
package com.java.copyFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyByFileReaderAndFileWriter {
public static void main(String[] args) {
long time1=System.currentTimeMillis();
File source=new File("E:\\Java .pdf");
File target=new File("F:\\Java .a");
try {
Copy( source, target);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
}
private static void Copy(File source, File target) throws IOException {
if(target.exists())
target.delete();
FileReader fr=new FileReader(source);
FileWriter fw=new FileWriter(target);
char c[]=new char[1024];
int len=0;
while((len=fr.read(c))!=-1)
fw.write(c);
fr.close();
fw.close();
}
}
4. 문자 버퍼 흐름
package com.java.copyFile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyByBufferReaderAndBufferWriter {
public static void main(String[] args) {
long time1=System.currentTimeMillis();
File source=new File("E:\\Java .pdf");
File target=new File("F:\\Java .a");
try {
Copy( source, target);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
}
private static void Copy(File source, File target) throws IOException {
if(target.exists())
target.delete();
FileReader fr=new FileReader(source);
BufferedReader br=new BufferedReader(fr);
FileWriter fw=new FileWriter(target);
BufferedWriter bw=new BufferedWriter(fw);
String s=null;
StringBuffer buffer=new StringBuffer();
while((s=br.readLine())!=null){
buffer.append(s);
}
bw.write(new String(buffer));
bw.flush();
br.close();
bw.close();
}
}
5. 단일 스 레 드 채널
package com.java.copyFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
public class CopyByFileChannel {
public static void main(String[] args) {
long time1=System.currentTimeMillis();
// File source=new File("E:\\Java .pdf");
// File target=new File("F:\\Java .a");
File source=new File("F:\\ \\[ -www.dy2018.net] BD .rmvb");
File target=new File("E:\\Java .a");
try {
Copy( source, target);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
}
private static void Copy(File source, File target) throws IOException {
if(target.exists())
target.delete();
FileInputStream fis=new FileInputStream(source);
FileOutputStream fos=new FileOutputStream(target);
//input getChannel() FileChannei ( )
FileChannel fileChannel=fis.getChannel();
//WritableByetChannel( )
WritableByteChannel writableByteChannel=fos.getChannel();
// 。
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
writableByteChannel.close();
fileChannel.close();
fos.close();
fis.close();
}
}
6. 다 중 스 레 드 채널
package com.java.copyFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class CopyFileThread extends Thread {
private File source;
private File target;
private long start,end;
public CopyFileThread(File source, File target, long start, long end) {
this.source = source;
this.target = target;
this.start = start;
this.end = end;
}
@Override
public void run() {
long starttime=System.currentTimeMillis();
try {
RandomAccessFile in=new RandomAccessFile(source, "r");
RandomAccessFile out=new RandomAccessFile(target, "rw");
in.seek(start);
out.seek(start);
FileChannel inChannel=in.getChannel();
FileChannel outChanel=out.getChannel();
// 、 ( ) ,
FileLock lock=outChanel.lock(start, (end-start), false);
// position count ,
inChannel.transferTo(start, end-start, outChanel);
lock.release();
in.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endtime=System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+" "+(endtime-starttime));
}
}
package com.java.copyFile;
import java.io.File;
public class Test {
private static int blockcount=1;
public static void main(String[] args) {
File source=new File("F:\\ \\[ -www.dy2018.net] BD .rmvb");
File target=new File("E:\\Java .a");
if(target.exists())
target.delete();
long len=source.length();
long oneNum=len/blockcount;
for(int i=0;i
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.