import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//NIO读数据
public class NIOReadAndWrite {
public void readTest(File fileName){
String content = null;
FileInputStream fis = null;
FileChannel fc = null;
try {
fis = new FileInputStream(fileName);
fc = fis.getChannel();
ByteBuffer bb = ByteBuffer.allocate(10000);
fc.read(bb);
bb.flip();
content = new String(bb.array());
System.out.println(content);
fc.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String str = "dfkvmlsmvhsdgkahkanckjbasdchbsdkcsklcnjsbckjsbcvls";
FileOutputStream fos = null;
FileChannel fc = null;
byte[] b = str.getBytes();
System.out.println(str.getBytes());
try {
fos = new FileOutputStream(file,true);
fc = fos.getChannel();
ByteBuffer bb = ByteBuffer.allocate(b.length);
bb.put(b);
bb.flip();
fc.write(bb);
fc.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long starttime = System.currentTimeMillis();
ByteBuffer bb = ByteBuffer.allocate(100000);
File oldf = new File(oldPath);
File newf = new File(newPath);
if(!newf.exists()){
newf.mkdirs();
}
int len = -1;
File temp = null;
String[] list = oldf.list();
for(int i=0;i<list.length;i++){
if(oldPath.endsWith(File.separator)){
temp = new File(oldPath+list[i]);
}else{
temp = new File(oldPath+File.separator+list[i]);
}
System.out.println(temp.isFile());
if(temp.isFile()){
FileInputStream fis = new FileInputStream(temp);
FileChannel in = fis.getChannel();
FileOutputStream fos = new FileOutputStream(newf+"/"+temp.getName().toString());
FileChannel out = fos.getChannel();
while((len=in.read(bb)) != -1){
bb.flip();
out.write(bb);
bb.clear();
}
out.close();
fos.close();
in.close();
fis.close();
}
if(temp.isDirectory()){
copyFile(oldPath+"/"+list[i], newPath+"/"+list[i]);
}
}
long endtime = System.currentTimeMillis();
System.out.println("复制总时长:"+(endtime - starttime));
}
public static void main(String[] args) throws IOException{
//File file = new File("d:/test.txt");
String oldFile = "D:\\VC98";
String newFile = "E:/猎豹下载/111";
//new NIOReadAndWrite().readTest(file);
//new NIOReadAndWrite().writeTest(file);
new NIOReadAndWrite().copyFile(oldFile,newFile);
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//NIO读数据
public class NIOReadAndWrite {
public void readTest(File fileName){
String content = null;
FileInputStream fis = null;
FileChannel fc = null;
try {
fis = new FileInputStream(fileName);
fc = fis.getChannel();
ByteBuffer bb = ByteBuffer.allocate(10000);
fc.read(bb);
bb.flip();
content = new String(bb.array());
System.out.println(content);
fc.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//NIO写数据
String str = "dfkvmlsmvhsdgkahkanckjbasdchbsdkcsklcnjsbckjsbcvls";
FileOutputStream fos = null;
FileChannel fc = null;
byte[] b = str.getBytes();
System.out.println(str.getBytes());
try {
fos = new FileOutputStream(file,true);
fc = fos.getChannel();
ByteBuffer bb = ByteBuffer.allocate(b.length);
bb.put(b);
bb.flip();
fc.write(bb);
fc.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//NIO复制数据
long starttime = System.currentTimeMillis();
ByteBuffer bb = ByteBuffer.allocate(100000);
File oldf = new File(oldPath);
File newf = new File(newPath);
if(!newf.exists()){
newf.mkdirs();
}
int len = -1;
File temp = null;
String[] list = oldf.list();
for(int i=0;i<list.length;i++){
if(oldPath.endsWith(File.separator)){
temp = new File(oldPath+list[i]);
}else{
temp = new File(oldPath+File.separator+list[i]);
}
System.out.println(temp.isFile());
if(temp.isFile()){
FileInputStream fis = new FileInputStream(temp);
FileChannel in = fis.getChannel();
FileOutputStream fos = new FileOutputStream(newf+"/"+temp.getName().toString());
FileChannel out = fos.getChannel();
while((len=in.read(bb)) != -1){
bb.flip();
out.write(bb);
bb.clear();
}
out.close();
fos.close();
in.close();
fis.close();
}
if(temp.isDirectory()){
copyFile(oldPath+"/"+list[i], newPath+"/"+list[i]);
}
}
long endtime = System.currentTimeMillis();
System.out.println("复制总时长:"+(endtime - starttime));
}
public static void main(String[] args) throws IOException{
//File file = new File("d:/test.txt");
String oldFile = "D:\\VC98";
String newFile = "E:/猎豹下载/111";
//new NIOReadAndWrite().readTest(file);
//new NIOReadAndWrite().writeTest(file);
new NIOReadAndWrite().copyFile(oldFile,newFile);
}
}
本文详细介绍了如何使用Java的非阻塞I/O(NIO)API进行文件和文件夹的复制操作,包括读取和写入数据的关键步骤。
 NIO读取数据与写数据&spm=1001.2101.3001.5002&articleId=17795369&d=1&t=3&u=8866bd8e08b74e7d9cdd1a8628c4a65b)
374

被折叠的 条评论
为什么被折叠?



