java日常筆記2016 12 27

2021-07-25 15:56:01 字數 3692 閱讀 1343

1:file.separator

file file1 = new file (「c:\tmp\test.txt」);

file myfile = new file(「c:」 + file.separator + 「tmp」 + file.separator, 「test.txt」);

2:過濾器   filefilter

file ff = f.listfiles(new filefilter() {}
3:兩種流(位元組流、包裝流)

is = new fileinputstream(f);

int value = -1;

while((value=is.read())!=-1)

is = new fileinputstream(f);

int value = -1;

byte tong = new byte[512];

while((value=is.read(tong)) !=-1)

4:類路徑載入資源

第一種:獲取類載入的根路徑   d:\git\daotie\daotie\target\classes

file f = new file(this.getclass().getresource("/").getpath());

第三種:  file:/d:/git/daotie/daotie/target/classes/

url xmlpath = this.getclass().getclassloader().getresource("");

5:載入屬性檔案(properties檔案、預設十六機制寫中文會自動轉碼、寫注釋#)

6:

//資源路徑有兩種方式, 

//1:一種以 / 開頭,則這樣的路徑是指定絕對路徑,

//2:如果不以 / 開頭, 則路徑是相對與這個class所在的包的

public class relation_absolute

7:

/*

* 以 位元組流 形式讀取properties檔案

*/public class readpropertiesdemo_stream else

}}

for (string key : map.keyset())

} private static string gettxtcontentfrompath(string path)

} catch (unsupportedencodingexception e) catch (ioexception e)

} return strb.tostring();

}

/*

* 以 字元流 的形式讀屬性檔案

* */

public class readpropertiesdemo_reader

if((row!=null&&row.trim().length()>0)&&!row.trim().startswith("#"))else}}

for (string key : map.keyset())

} catch (unsupportedencodingexception e) catch (ioexception e)

}

8:

/*

* 位元組流

*/public class inputstreamdemo

} catch (filenotfoundexception e) catch (ioexception e) finally

} catch (ioexception e)

}} }

/*

* 輸出流

*/public class outputstreamdemo

file fileout = new file(dir,123456+".txt");

long currenttimemillis = system.currenttimemillis();

if(filein.exists())

system.out.println(system.currenttimemillis() - currenttimemillis);

} catch (filenotfoundexception e) catch (ioexception e) finally

} catch (ioexception e)

try

} catch (ioexception e)

}} }

9:

/*

* 基本的字元流讀複製文字

*/public class copy_print

fileout.createnewfile();

//先得到位元組流,轉換成字元流,這樣可以設定編碼

reader reader = new inputstreamreader(new fileinputstream(filein),"gbk");//new filereader(filein,"gbk");//這個**是錯的,直接讀取文字,不能指定編碼

bufferedreader br = new bufferedreader(reader);

bufferedwriter out = new bufferedwriter(new filewriter(fileout));

while(true)

out.write(txt+"\r\n");

out.flush();

system.out.println(txt);

} br.close();

out.close();

}

public static void main(string args) throws exception 

fileout.createnewfile();

bufferedreader br = new bufferedreader(new filereader(filein));

// bufferedwriter out = new bufferedwriter(new filewriter(fileout));

printwriter out = new printwriter(new bufferedwriter(new filewriter(fileout)));

while(true)

system.out.println(txt);

out.println(txt);

out.flush();//不能省!

} br.close();

out.close();

}

10:filereader:不能指定檔案編碼        inputstreamredader可以指定編碼

java日常筆記2016 12 10

1 string拼接中會產生多少個物件 string s1 sdasd 乙個 堆 string s2 new string dsdsa 二個 堆 尺 2 stringbuffer執行緒安全 效率低可以避免產生多餘的堆物件 str a b stringbuilder 執行緒不安全 4 字串轉換成基本資...

java日常筆記2016 12 14

1 兩個常用的正規表示式 1 郵箱驗證 a za z w a za z0 9 a za z0 9 a za z 432432 qq.com 2 private不可修飾一般類 內部類除外 3 protected和super呼叫問題與物件無關 只可在子類中呼叫,在測試類中呼叫會出錯 4 子類繼承父類 呼...

java日常筆記2016 12 21

1 web1.0和web2.0 2 集合的合併 public static void main string args list.addall set system.out.println list.size 3 集合順序的倒置將集合和陣列之間的互換 listlist new arraylist p...