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

时间: 2024-12-17 03:17:40

java io实例具体解释的相关文章

java IO实例

import java.io.*; /** * Created by CLY on 2017/7/23. */ public class Main { public static void main(String[] arg){ testFile(); testFilenameFilter(); testInputStream(); testOutStream(); testCopyImg(); } /** * File类: * 能代表一个特定文件的“名称”,也能代表“一个目录下”的一组文件的“

java IO 实例分析

初学java,一直搞不懂java里面的io关系,在网上找了很多大多都是给个结构图草草描述也看的不是很懂.而且没有结合到java7 的最新技术,所以自己来整理一下,有错的话请指正,也希望大家提出宝贵意见. 首先看个图:(如果你也是初学者,我相信你看了真个人都不好了,想想java设计者真是煞费苦心啊!) 这是java io 比较基本的一些处理流,除此之外我们还会提到一些比较深入的基于io的处理类,比如console类,SteamTokenzier,Externalizable接口,Serializa

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.FileO

java之IO实例

实例1:从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印 分析:这里用到了file类的方法和递归 public  class  Test10{ public  static  void   main(String[ ]  args) { Scanner  sc=new Scanner(System.in); String  path=sc.nextLine( ); File   f=new  File(path); System.out.println(f.getName

java IO流文件的读写具体实例(转载)

引言: 关于java IO流的操作是非常常见的,基本上每个项目都会用到,每次遇到都是去网上找一找就行了,屡试不爽.上次突然一个同事问了我java文件的读取,我一下子就懵了第一反应就是去网上找,虽然也能找到,但自己总感觉不是很踏实,所以今天就抽空看了看java IO流的一些操作,感觉还是很有收获的,顺便总结些资料,方便以后进一步的学习... IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader  低端流:所有的外界设备中的流都是低端流

IO的详细解释:It's all about buffers: zero-copy, mmap and Java NIO

There are use cases where data need to be read from source to a sink without modification. In code this might look quite simple: for example in Java, you may read data from one InputStream chunk by chunk into a small buffer (typically 8KB), and feed

JAVA IO流小实例

package demo; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io

java.io 字节流与字符流及简单实例

java io是java中非常基础的知识点,对于通信和不涉及到数据库的项目java io应该是要经常使用.java io分为字节流和字符流,分清字节流和字符流的异同是掌握java io的起点. 字节流,最小单位是字节,通信协议中的X-MODEM和Y-MODEM协议传输单位就是字节流.java io中与字节流相关的主要类有:InputStream,OutputStream,FileInputStream,FileOutputStream.与字符流有关的主要类有:Writer,Reader,File

Java.io.ObjectOutputStream.writeObject()方法实例

java.io.ObjectOutputStream.writeObject(Object obj) 方法将指定对象写入ObjectOutputStream.该对象的类,类的签名,以及类及其所有超类型的非瞬态和非静态字段的值被写入.默认的序列化的类可以使用writeObject和readObject方法被重写.由此对象引用的对象都写及物动词使对象的完全等价的图形可以由ObjectInputStream重建. 声明 以下是java.io.ObjectOutputStream.writeObject