標籤: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整理(中)