Java新IO】_檔案鎖\代碼 與字元集

來源:互聯網
上載者:User

標籤: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】_檔案鎖\代碼 與字元集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.