java之IO整理(中)

來源:互聯網
上載者:User

標籤:out   注意   row   set   cat   import   exception   null   回退   

一:列印流
/*System.out.println()重新導向輸出*//*public static void main(String[] args) {System.out.println("MM");//直接輸出到控制台File file=new File("d:"+File.separator+"mm.txt");try {System.setOut(new PrintStream(new FileOutputStream(file)));} catch (FileNotFoundException e) {e.printStackTrace();}System.out.println("這些內容在檔案中才能看到哦!");}*//*System.err重新導向 這個例子也提示我們可以使用這種方法儲存錯誤資訊*//*public static void main(String[] args) {File file=new File("d:"+File.separator+"mm.txt");System.out.println("這些在控制台輸出!");try {System.setErr(new PrintStream(new FileOutputStream(file)));} catch (FileNotFoundException e) {e.printStackTrace();}System.out.println("這些內容在檔案中才能看到哦!");}*//*BufferedReader的小例子注意: BufferedReader只能接受字元流的緩衝區,因為每一個中文需要佔據兩個位元組,所以需要將System.in這個位元組輸入資料流變為字元輸入資料流,採用:BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));*/public static void main(String[] args) {BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));String str=null;System.out.println("請輸入內容:");try {str=bufferedReader.readLine();} catch (IOException e) {e.printStackTrace();}System.out.println("你輸入的內容是:"+str);}

  二:合并流:

package com.js.ai.modules.pointwall.testxfz;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.SequenceInputStream;/*將兩個文字檔合并為另外一個文字檔*/public class SequenceInputStreamDemo {public static void main(String[] args) throws IOException {File file1=new File("d:"+File.separator+"hello1.txt");File file2=new File("d:"+File.separator+"hello2.txt");File file3=new File("d:"+File.separator+"hello.txt");InputStream inputStream1=new FileInputStream(file1);InputStream inputStream2=new FileInputStream(file2);OutputStream outputStream=new FileOutputStream(file3);//合并流SequenceInputStream sequenceInputStream=new SequenceInputStream(inputStream1, inputStream2);int temp=0;while((temp=sequenceInputStream.read())!=(-1)){outputStream.write(temp);}inputStream1.close();inputStream2.close();outputStream.close();sequenceInputStream.close();}}

  三:檔案壓縮:

/*壓縮單個檔案*/public class ZipOutputStreamDemo1 {public static void main(String[] args) throws IOException {File file=new File("d:"+File.separator+"hello.txt");File zipFile=new File("d:"+File.separator+"hello.zip");InputStream inputStream=new FileInputStream(file);ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream(zipFile));zipOutputStream.putNextEntry(new ZipEntry(file.getName()));zipOutputStream.setComment("Hello");//設定注釋int temp=0;while((temp=inputStream.read())!=(-1)){zipOutputStream.write(temp);}inputStream.close();zipOutputStream.close();}}

  

/*壓縮多個檔案*/public class ZipOutputStreamDemo1 {public static void main(String[] args) throws IOException {File file=new File("d:"+File.separator+"temp");// 要被壓縮的檔案夾File zipFile=new File("d:"+File.separator+"zipFile.zip");InputStream inputStream=null;ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream(zipFile));zipOutputStream.setComment("Hello");//設定注釋if(file.isDirectory()){File[] files =file.listFiles();for(int i=0;i<files.length;i++){inputStream=new FileInputStream(files[i]);zipOutputStream.putNextEntry(new ZipEntry(file.getName()+File.separator+files[i].getName()));int temp=0;while((temp=inputStream.read())!=(-1)){zipOutputStream.write(temp);}inputStream.close();}}zipOutputStream.close();}}

  四:檔案解壓

/*解壓單個檔案*/
public class ZipOutputStreamDemo1 {public static void main(String[] args) throws IOException {File file=new File("d:"+File.separator+"hello.zip");File outFile=new File("d:"+File.separator+"outZipFile.txt");ZipFile zipFile=new ZipFile(file);ZipEntry entry=zipFile.getEntry("hello.txt");InputStream inputStream=zipFile.getInputStream(entry);OutputStream OutputStream=new FileOutputStream(outFile);int temp=0;while((temp=inputStream.read())!=(-1)){OutputStream.write(temp);}inputStream.close();OutputStream.close();}}

  

/*解壓一個壓縮檔中包含多個檔案的情況*/public class ZipOutputStreamDemo1 {public static void main(String[] args) throws IOException {File file=new File("d:"+File.separator+"zipFile.zip");File outFile=null;ZipFile zipFile=new ZipFile(file);ZipInputStream zipInputStream=new ZipInputStream(new FileInputStream(file));ZipEntry entry=null;InputStream inputStream=null;OutputStream OutputStream=null;while((entry=zipInputStream.getNextEntry())!=null){System.out.println("解壓檔案:"+entry.getName());outFile=new File("d:"+File.separator+entry.getName());if(!outFile.getParentFile().exists()){outFile.getParentFile().mkdirs();}if(!outFile.exists()){outFile.createNewFile();}inputStream=zipFile.getInputStream(entry);OutputStream=new FileOutputStream(outFile);int temp=0;while((temp=inputStream.read())!=(-1)){OutputStream.write(temp);}inputStream.close();OutputStream.close();}}}

  五、回退流

public class PushBackInputStreamDemo {public static void main(String[] args) throws Exception {String str="hello,rollenholt";PushbackInputStream push=null;ByteArrayInputStream bat=null;bat=new ByteArrayInputStream(str.getBytes());push=new PushbackInputStream(bat);int temp=0;while((temp=push.read())!=(-1)){if(temp==‘,‘){push.unread(temp);temp=push.read();System.out.print("(回退:"+(char)temp+")");}else {System.out.print((char)temp);}}}}

  

 

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.