package Method;
|
|
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;
|
|
public class NIOFileChannel {
|
static FileOutputStream fileoutputstream=null;//ÎļþÊä³öÁ÷
|
|
//´´½¨Ò»¸öÊä³öÁ÷->channle
|
|
/**ÓÃNIO½«ÎļþдÈëij¸öÎļþ¼Ð
|
* @param filepath ÎļþдÈë·¾¶*/
|
public static void setFileToFolder(String filepath,String str){
|
try {
|
fileoutputstream=new FileOutputStream(filepath);
|
|
//ͨ¹ýfileputstream »ñÈ¡¶ÔÓ¦µÄFileChannle
|
//filechannelµÄÕæÊµÀàÐÍÊÇfilechannleImpl
|
FileChannel fileChannle=fileoutputstream.getChannel();
|
|
//´´½¨Ò»¸ö»º³åÇø ByteBuffel
|
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
|
|
//½«Str·ÅÈë byteBuffer
|
byteBuffer.put(str.getBytes());
|
|
//¶Ô byteBuffer½øÐÐflip£¬¶ÁдÇл»
|
byteBuffer.flip();
|
|
//½«byteBufferÖеÄÊý¾ÝдÈëµ½filechannel
|
try {
|
fileChannle.write(byteBuffer);
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
if(fileoutputstream !=null) {
|
try {
|
fileoutputstream.close();
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
}
|
} catch (FileNotFoundException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
}
|
|
/**½«Îļþ´ÓÎļþ¼Ð¶ÁÈë
|
* @throws FileNotFoundException */
|
public static void setFolderToFile(String pathname) throws FileNotFoundException {
|
//´´½¨ÎļþµÄÊäÈëÁ÷
|
File file=new File(pathname);
|
|
FileInputStream fileInputStream=new FileInputStream(file);
|
|
//ͨ¹ýfileInputStream»ñÈ¡¶ÔÓ¦µÄfilechannle
|
FileChannel fileChannle=fileInputStream.getChannel();
|
|
//´´½¨»º³åÇø
|
ByteBuffer byteBuffer=ByteBuffer.allocate((int)file.length());
|
|
//½«Í¨µÀµÄÊý¾Ý¶ÁÈëµ½bufferÖÐ
|
try {
|
fileChannle.read(byteBuffer);
|
//½«byteBufferתΪStringÊä³ö
|
|
System.out.println(new String(byteBuffer.array()));
|
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
|
try {
|
fileInputStream.close();//¹Ø±ÕÊäÈëÁ÷
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
|
}
|
|
/**Îļþ¿½±´*/
|
public static void fileTofile(File in,File out) {
|
FileInputStream fileInputStream=null;
|
FileOutputStream fileOutputStream=null;
|
try {
|
fileInputStream = new FileInputStream(in);
|
} catch (FileNotFoundException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
FileChannel fileChannel01 = fileInputStream.getChannel();
|
|
|
try {
|
fileOutputStream = new FileOutputStream(out);
|
} catch (FileNotFoundException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
FileChannel fileChannel02 = fileOutputStream.getChannel();
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(512);
|
|
while (true) { //Ñ»·¶ÁÈ¡
|
|
//ÕâÀïÓÐÒ»¸öÖØÒªµÄ²Ù×÷£¬Ò»¶¨²»ÒªÍüÁË
|
byteBuffer.clear(); //Çå¿Õbuffer
|
int read=0;
|
try {
|
read = fileChannel01.read(byteBuffer);
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
System.out.println("read =" + read);
|
if(read == -1) { //±íʾ¶ÁÍê
|
break;
|
}
|
//½«buffer ÖеÄÊý¾ÝдÈëµ½ fileChannel02 -- 2.txt
|
byteBuffer.flip();
|
try {
|
fileChannel02.write(byteBuffer);
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
}
|
|
//¹Ø±ÕÏà¹ØµÄÁ÷
|
try {
|
fileInputStream.close();
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
try {
|
fileOutputStream.close();
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
}
|
|
/**Îļþ¿ìËÙ¿½±´·½·¨
|
* @param sourse ÎļþÔ´
|
* @param destÎļþÄ¿±êµØÖ·*/
|
|
public static void file_kao_bei(File sourse,File dest) {
|
//´´½¨Ïà¹ØÁ÷
|
FileInputStream fileInputStream=null;
|
FileOutputStream fileOutputStream=null;
|
try {
|
fileInputStream = new FileInputStream(sourse);
|
} catch (FileNotFoundException e1) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e1.printStackTrace();
|
}
|
|
try {
|
fileOutputStream = new FileOutputStream(dest);
|
} catch (FileNotFoundException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
|
//»ñÈ¡¸÷¸öÁ÷¶ÔÓ¦µÄfilechannel
|
FileChannel sourceCh = fileInputStream.getChannel();
|
FileChannel destCh = fileOutputStream.getChannel();
|
|
//ʹÓÃtransferFormÍê³É¿½±´
|
try {
|
destCh.transferFrom(sourceCh,0,sourceCh.size());
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
|
//¹Ø±ÕÏà¹ØÍ¨µÀºÍÁ÷
|
try {
|
sourceCh.close();
|
destCh.close();
|
fileInputStream.close();
|
fileOutputStream.close();
|
} catch (IOException e) {
|
// TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
|
e.printStackTrace();
|
}
|
|
|
}
|
|
}
|