標籤:end entry 中心 channel etc txt ring getname sort
import java.io.File ;
import java.io.FileOutputStream ;
import java.nio.channels.FileChannel ;
import java.nio.channels.FileLock ;
public class FileLockDemo{
public static void main(String args[]) throws Exception {
File file = new File("d:" + File.separator + "mldn.txt") ;
FileOutputStream output = null ;
output = new FileOutputStream(file,true) ;
FileChannel fout = null ;
fout = output.getChannel() ;// 得到通道
FileLock lock = fout.tryLock() ; // 進行獨佔鎖的操作
if(lock!=null){
System.out.println(file.getName() + "檔案鎖定300秒") ;
Thread.sleep(300000) ;
lock.release() ; // 釋放
System.out.println(file.getName() + "檔案解除鎖定。") ;
}
fout.close() ;
output.close() ;
}
}
//****
import java.nio.charset.Charset ;
import java.util.SortedMap ;
import java.util.Iterator ;
import java.util.Map ;
public class GetAllCharsetDemo{
public static void main(String args[]){
SortedMap<String,Charset> all = null ;
all = Charset.availableCharsets() ; // 得到全部可用的字元集
Iterator<Map.Entry<String,Charset>> iter = null ;
iter = all.entrySet().iterator() ;
while(iter.hasNext()){
Map.Entry<String,Charset> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
}
//******
import java.nio.charset.Charset ;
import java.nio.charset.CharsetEncoder ;
import java.nio.charset.CharsetDecoder ;
import java.nio.CharBuffer ;
import java.nio.ByteBuffer ;
import java.util.SortedMap ;
import java.util.Iterator ;
import java.util.Map ;
public class CharsetEnDeDemo{
public static void main(String args[]) throws Exception {
Charset latin1 = Charset.forName("ISO-8859-1") ; // 只能表示的英文字元
CharsetEncoder encoder = latin1.newEncoder() ; // 得到編碼器
CharsetDecoder decoder = latin1.newDecoder() ; // 得到解碼器
// 通過CharBuffer類中的
// CharBuffer cb = CharBuffer.wrap("北京MLDN軟體實訓中心!") ;
CharBuffer cb = CharBuffer.wrap("北京MLDN軟體實訓中心!") ;
ByteBuffer buf = encoder.encode(cb) ; // 進行編碼操作
System.out.println(decoder.decode(buf)) ; // 進行解碼操作
}
}
Java新IO】_檔案鎖\代碼 與字元集