1. 程式人生 > >java io實例具體解釋

java io實例具體解釋

操作 org hello abc 設置 對象流 pan 愛我 不想


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.io.Serializable;
import java.io.Writer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;


import org.junit.Before;
import org.junit.Test;


/**
* 流操作
* @author Administrator
*
*/
public class FileOpera {
//String path="";
String str="";
@Before
public void init(){
//path="d:"+File.separator+"test";
str="d:"+File.separator+"test"+File.separator+"test.txt";
}
/*使用RandomAccessFile寫入文件*/
@Test
public void testAccess() throws IOException{
init();
File file=new File(str);
RandomAccessFile f=new RandomAccessFile(file,"rw");
f.writeBytes("abcdef");
f.writeInt(21);
f.writeDouble(32.11);
f.close(); //會亂碼
}
/*字節流輸出到文件OutputStream InputputStream*/
@Test
public void testByte1() throws IOException{
init();
File file =new File(str);
//OutputStream out=new FileOutputStream(file);
OutputStream out=new FileOutputStream(file,true);
String s="總共多少人";
out.write(s.getBytes());//按字節
out.flush();
out.close();
}
/*字符流輸出到文件 Reader(讀) Writer(寫)*/
@Test
public void testReader() throws IOException{
init();
File file=new File(str);
Writer writer=new FileWriter(file,true);
//Writer writer=new OutputStreamWriter(new FileOutputStream(file));//將字節輸出流轉換為字符輸出流
writer.write("你好世界");
writer.flush();
writer.close();
}
/*內存字節流-------------使用內存操作流(字節方式)ByteArrayInputStream ByteArrayOutputStream*/
@Test
public void testByte() throws IOException{
String str="ROLLENHOLT";
ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());//以字節數組方式讀入緩存區
ByteArrayOutputStream output=new ByteArrayOutputStream();
int temp=0;
while((temp=input.read())!=-1){
char ch=(char)temp;
output.write(Character.toLowerCase(ch));
}
String outStr=output.toString();
input.close();
output.close();
System.out.println(outStr);
}
/*字節管道流 主要是進行兩個線程之間的通信 PipedOutputStream PipedInputStream */
@Test
public void testPied(){
Send send=new Send();
Revice recive=new Revice();
try{
//管道連接
send.getOut().connect(recive.getInput());
}catch (Exception e) {
e.printStackTrace();
}
new Thread(send).start();
new Thread(recive).start();
}
class Send implements Runnable{
private PipedOutputStream out=null;
public Send(){
out= new PipedOutputStream();
}
public PipedOutputStream getOut(){
return this.out;
}
@Override
public void run() {
String message="hello 中國";
try{
out.write(message.getBytes());
}catch (Exception e) {
e.printStackTrace();
}try{
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}

}
class Revice implements Runnable{
private PipedInputStream input=null;
public Revice(){
this.input=new PipedInputStream();
}
public PipedInputStream getInput(){
return this.input;
}
public void run(){
byte[] b=new byte[1000];
int len=0;
try{
len=this.input.read(b);
}catch (Exception e) {
e.printStackTrace();
}try{
input.close();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("接受的內容為 "+(new String(b,0,len)));
}
}
/*打印流 PrintStream*/
@Test
public void testPrint() throws FileNotFoundException{
init();
File file=new File(str);
PrintStream print=new PrintStream(new FileOutputStream(file,true));
print.print("愛我中華");
print.flush();
print.close();
}
/*輸出到屏幕上*/
@Test
public void testSreen(){
OutputStream out=System.out;
try{
out.write("hello".getBytes());
}catch (Exception e) {
e.printStackTrace();
}
try{
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
/*輸入輸出重定向*/
@Test
public void testRedirect(){
// 此刻直接輸出到屏幕
System.out.println("hello");
init();
File file = new File(str);
try{
System.setOut(new PrintStream(new FileOutputStream(file,true)));
}catch(FileNotFoundException e){
e.printStackTrace();
}
System.out.println("這些內容在文件裏才幹看到哦!");

//重定向err輸出
System.err.println("這些在控制臺輸出");
try{
System.setErr(new PrintStream(new FileOutputStream(file,true)));
}catch(FileNotFoundException e){
e.printStackTrace();
}
System.err.println("這些在文件裏才幹看到哦!

");
}
/*BufferedReader僅僅能接受字符流的緩沖區。由於每個中文須要占領兩個字節,所以須要將System.in這個字節輸入流變為字符輸入流,採用*/
@Test
public void testBuffer(){
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));//將字節流轉化為字符流
String str = null;
System.out.println("請輸入內容");
try{
str = buf.readLine();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("你輸入的內容是:" + str);
}
/*數據操作流DataOutputStream、DataInputStream類*/
@Test
public void testData() throws IOException{
init();
File file=new File(str);
char[] ch = { ‘A‘, ‘B‘, ‘C‘ };
DataOutputStream out=new DataOutputStream(new FileOutputStream(file,true));
for(char temp : ch){
out.writeChar(temp);
}
out.close();
}
/*合並流 SequenceInputStream*/
@Test
public void testSequence() throws IOException{
File file1 = new File("d:" + File.separator +"test"+File.separator+"test.txt");
File file2 = new File("d:" + File.separator +"test"+File.separator+"hello2.txt");
File file3 = new File("d:" + File.separator +"test"+File.separator+"hello.txt");
if(!file1.exists()){
file1.createNewFile();
}
if(!file2.exists()){
file2.createNewFile();
}
if(!file3.exists()){
file3.createNewFile();
}
InputStream input1 = new FileInputStream(file1);
InputStream input2 = new FileInputStream(file2);
OutputStream output = new FileOutputStream(file3);
// 合並流
SequenceInputStream sis = new SequenceInputStream(input1, input2);
int temp = 0;
while((temp = sis.read()) != -1){
output.write(temp);
}
input1.close();
input2.close();
output.close();
sis.close();
}
/*文件壓縮 ZipOutputStream類*/
@Test
public void testZip() throws IOException{
init();
File file=new File(str);
String path="d:"+File.separator+"test"+File.separator+"test.zip";
File zipFile=new File(path);
InputStream in=new FileInputStream(file);
ZipOutputStream zip=new ZipOutputStream(new FileOutputStream(zipFile));
zip.putNextEntry(new ZipEntry(file.getName()));
// 設置凝視
zip.setComment("hello");
int temp = 0;
while((temp = in.read()) != -1){
zip.write(temp);
}
in.close();
zip.close();
}
/*解壓縮ZipFile類*/
@Test
public void testZipFile() throws ZipException, IOException{
init();
String str="d:"+File.separator+"test"+File.separator+"test.zip";
String path="d:"+File.separator+"test"+File.separator+"zip.txt";
File file=new File(str);
File zipf=new File(path);
ZipFile zipFile = new ZipFile(file);
System.out.println("壓縮文件的名稱為:" + zipFile.getName());
ZipEntry entry = zipFile.getEntry("test.txt");
InputStream input = zipFile.getInputStream(entry);
OutputStream output = new FileOutputStream(zipf);
int temp = 0;
while((temp = input.read()) != -1){
output.write(temp);
}
input.close();
output.close();
}
/*PushBackInputStream回退流*/
@Test
public void testPush() throws IOException{
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);
}
}
}
/*System 類的一些操作*/
@Test
public void testSys(){
System.out.println("系統默認編碼為:" + System.getProperty("file.encoding"));
}
/**
* 將一個對象流化即實現Serializable接口(默認將所有屬性序列化)還能夠實現一組對象的序列化
* 假設不想所有屬性被實例化能夠在實體中使用如private transient String name; transient屬性
* 能夠在對象輸入(ObjectInputStream)輸出(ObjectOutputStream)流中直接操作對象
*
* 實現Externalizable接口。能夠將部分屬性實例化
*
*
* @throws IOException
* @throws ClassNotFoundException
*/
/*ObjectOutputStream*/
@Test
public void testObject() throws IOException, ClassNotFoundException{
init();
File file = new File(str);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,true));
oos.writeObject(new Person("laoma", 20,"男"));
oos.close();

//查看
ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
Object obj = input.readObject();
input.close();
System.out.println(obj);
}

/*PushBackInputStream回退流*/
@Test
public void testRect(){

}

}



參考自:http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html

java io實例具體解釋