java io实例具体解释
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