JAVA-文件类
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
緩沖輸入文件
如果想要打開一個(gè)文件,讀取里面的內(nèi)容,將內(nèi)容作為字符。我們可以用FileInputReader,它接受String類型的文件路徑或File類型的文件。為了提高速度,我們希望對文件內(nèi)容進(jìn)行緩沖,所以我們將FileInputReader傳給一個(gè)BufferReader,然后通過它的readLine()來讀取。一旦讀取到文件末尾,readLine()將返回null,所以我們用它來終止讀取。代碼如下:
BufferedReader in =?new?BufferedReader(new?FileReader("E:/test.txt"));
String s, s2 =?new?String();
while?((s = in.readLine()) !=?null) {
????s2 += s +?"\n";
}
in.close();
System.out.println(s2);
如上所述,new?FileReader("E:/test.txt")也可以傳入一個(gè)File類。
在讀取時(shí),readLine()會將換行符刪除。所以我們一定要自己加上。另外,在最后我們一定要記得調(diào)用close()來關(guān)閉流。
下面這段代碼展示了如何從標(biāo)準(zhǔn)輸入讀取:
BufferedReader stdin =?new?BufferedReader(new?InputStreamReader(
??????????????System.in));
????System.out.println("請輸入一行字,以回車結(jié)束:");
????System.out.println("從標(biāo)準(zhǔn)輸入讀取內(nèi)容:"?+ stdin.readLine());
當(dāng)程序運(yùn)行時(shí),程序會提示用戶輸入,輸入完成后按回車。這時(shí)控制臺會將用戶輸入的內(nèi)容打印出來。
從內(nèi)存輸入
StringReader in2 =?new?StringReader(s2);
???????int?c;
???????while?((c = in2.read()) != -1) {
???????????System.out.println((char) c);
???????}
???????in2.close();
s2是已經(jīng)被讀取到內(nèi)存中的字符串。這里將它讀入流,然后打印。當(dāng)然,直接就可以打印它,不用這么麻煩。
格式化的內(nèi)存輸入
格式化數(shù)據(jù)意思就是讀取特定類型的數(shù)據(jù)。這里要用到DataInputStream,它是一個(gè)面向字節(jié)的I/O類,不是面向字符的。因此我們必須用InputStream類,而不是Reader類.
try?{
???????????DataInputStream in3 =?new?DataInputStream(new?ByteArrayInputStream(
??????????????????s2.getBytes()));
???????????while?(true) {
??????????????System.out.println((char) in3.readByte());
???????????}
???????}?catch?(EOFException e) {
???????????System.out.println("流結(jié)束");
???????}
這里我們是從內(nèi)存中讀取的,也可以從文件中讀取,如:
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(“E:/test.txt”)));
while(in.avaliable() != 0){
???????System.out.print((char)in.readByte());
}
文件輸出
向一個(gè)文件輸入內(nèi)容,先要創(chuàng)建一個(gè)指定文件的FileWriter,然后再用BufferedWriter將其包裝一下以緩沖輸入提高性能(不是必須,但有好處)。最后轉(zhuǎn)換成PrintWriter。
try?{
???????????BufferedReader in4 =?new?BufferedReader(new?StringReader(s2));
???????????PrintWriter out1 =?new?PrintWriter(new?BufferedWriter(
??????????????????new?FileWriter("E:/out.txt")));
???????????int?lineCount = 1;
???????????while?((s = in4.readLine()) !=?null) {
??????????????out1.print(lineCount++ +?":"?+ s);
???????????}
???????????out1.close();
???????}?catch?(EOFException e) {
???????????System.out.println("流結(jié)束");
???????}
一定要記住關(guān)閉流。
?
從標(biāo)準(zhǔn)輸入讀取
按照I/O模型,JAVA提供了System.in,System.out和System.err。其中System.out已經(jīng)被封裝成了PrintStream對象,System.err也被封裝成了PrintStream,但System.in卻是一個(gè)未被封裝過的InputStream。所以在讀取它之前要進(jìn)行封裝。就象前面的:
BufferedReader stdin =?new?BufferedReader(new?InputStreamReader(
??????????????System.in));
System.out轉(zhuǎn)換成PrintWriter
System.out是一個(gè)PrintStream,而PrintStream是一個(gè)OutputStream.PrintWriter可以接收OutputStream作為構(gòu)造函數(shù)的參數(shù)。所以在需要時(shí),我們可以把System.out轉(zhuǎn)換成PrintWriter。
PrintWriter out = new PrintWriter(System.out,true);?
?
Gzip簡單壓縮
BufferedReader in =?new?BufferedReader(new?FileReader("E:/test.txt"));
???????BufferedOutputStream out =?new?BufferedOutputStream(
??????????????new?GZIPOutputStream(new?FileOutputStream("E:/test.gz")));
???????System.out.println("壓縮成GZIP文件");
???????int?c;
???????while?((c = in.read()) != -1) {
???????????out.write(c);
???????}
???????in.close();
???????out.close();
???????System.out.println("讀取GZIP文件內(nèi)容");
???????BufferedReader in2 =?new?BufferedReader(new?InputStreamReader(
??????????????new?GZIPInputStream(new?FileInputStream("E:/test.gz"))));
???????String s;
???????while?((s = in2.readLine()) !=?null) {
???????????System.out.println(s);
???????}
Zip的多文件保存
public?static?void?main(String[] args)?throws?IOException {
???????String[] arg = {"E:/test.txt","E:/out.txt","E:/out2.txt"};
???????zip(arg);
????}
?
????public?static?void?zip(String[] args)?throws?IOException {
???????FileOutputStream f =?new?FileOutputStream("E:/test.zip");
???????CheckedOutputStream csum =?new?CheckedOutputStream(f,?new?Adler32());
???????ZipOutputStream zos =?new?ZipOutputStream(csum);
???????BufferedOutputStream out =?new?BufferedOutputStream(zos);
???????zos.setComment("測試JAVA的zip壓縮");
???????for?(int?i = 0; i < args.length; i++) {
???????????System.out.println("壓縮文件:"?+ args[i]);
???????????BufferedReader in =?new?BufferedReader(new?FileReader(args[i]));
???????????zos.putNextEntry(new?ZipEntry(args[i]));
???????????int?c;
???????????while?((c = in.read()) != -1) {
??????????????out.write(c);
???????????}
???????????in.close();
???????}
???????out.close();
???????System.out.println("CheckSum:"?+ csum.getChecksum().getValue());
?
???????System.out.println("解壓文件:");
???????FileInputStream fi =?new?FileInputStream("E:/test.zip");
???????CheckedInputStream csumi =?new?CheckedInputStream(fi,?new?Adler32());
???????ZipInputStream in2 =?new?ZipInputStream(csumi);
???????BufferedInputStream bis =?new?BufferedInputStream(in2);
???????ZipEntry ze;
???????while?((ze = in2.getNextEntry()) !=?null) {
???????????System.out.println("正在讀取:"?+ ze);
???????????int?x;
???????????while?((x = bis.read()) != -1) {
??????????????System.out.write(x);
???????????}
???????}
???????System.out.println("CheckSum:"?+ csumi.getChecksum().getValue());
???????bis.close();
???????ZipFile zf =?new?ZipFile("E:/test.zip");
???????Enumeration?e = zf.entries();
???????while?(e.hasMoreElements()) {
???????????ZipEntry ze2 = (ZipEntry) e.nextElement();
???????????System.out.println("文件:"?+ ze2);
???????}
????}
?
轉(zhuǎn)載于:https://my.oschina.net/xiahuawuyu/blog/57782
總結(jié)
- 上一篇: RAC crs_stat unknown
- 下一篇: 0.IT-解决方案-0-VOIP