1. 程式人生 > >java之IO整理(中)

java之IO整理(中)

getparent create olt highlight print enc buffered byte package

一:打印流
/*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整理(中)