java小筆記

  1. java的iterator.remove()注意
    iterator.remove()前不能改變原list的size().
  2. java使用BufferedReader中文輸出亂碼
    java
    //需要指定charset
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GB2312"));
  3. 使用FileChannel提高複製文件效率
/**
     * 複製文件
     * @param src    源文件
     * @param dest    目的文件
     * @throws IOException 
     */
    public static void copyFile(File src, File dest) throws IOException {
        if (!src.exists()) {
            throw new FileNotFoundException("source file not found.");
        }
        // make parent directory if not exists
        dest.getParentFile().mkdirs();
        FileInputStream inStream = null;
        FileOutputStream outStream = null;
        FileChannel in = null;
        FileChannel out = null;

        try {
            inStream = new FileInputStream(src);
            outStream = new FileOutputStream(dest);
            in = inStream.getChannel();
            out = outStream.getChannel();
            in.transferTo(0, in.size(), out);
        } catch (IOException e) {
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (inStream != null) {
                inStream.close();
            }
            if (outStream != null) {
                outStream.close();
            }
        }

    }  
發佈了29 篇原創文章 · 獲贊 2 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章