io流用法

InputStream 与OutputSream

package test20141212;

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;

public class demo1 {
	public static void main(String[] args) throws IOException {
		String fileName="test1.txt";
//		FileOutputStreamTest(fileName);
		FileInputStreamTest(fileName);
	}
	/*创建文件*/
	public static void createFile(String s) throws IOException{
		File TestFile=new File(s);
		if(!TestFile.exists())
			{
				TestFile.createNewFile();
				System.out.println("创建成功");
			}
		else
				System.out.println("文件已存在创建失败");
		
	}
	public static void FileOutputStreamTest(String filename) throws IOException{
		OutputStream output=null;
		try {
			/*创建文件夹*/
			createFile(filename);
			File f=new File(filename);
			output=new FileOutputStream(f);
			String content="hello wrold";
			byte[] pool=content.getBytes();
			output.write(pool);
		} catch (FileNotFoundException e) {
			System.out.println("找不到数据源");
		} catch (IOException e) {
			System.out.println("找不到输出源");
		}finally{
			output.close();
		}
	}
	public static void FileInputStreamTest(String filename) throws IOException{
		File f=new File(filename);
		InputStream is=new FileInputStream(f);
		int i=-1;
		byte[] pool=new byte[200];
		while((i=is.read(pool))!=-1){
			System.out.println(new String(pool,0,i));
		}
	}
}


 Reader与Writer(读取中文字符串时用此流)

package demo4;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

public class FileInputStreamTest{
	public static void main(String[] args) throws IOException {
		
//		readTest();
	}
	/*read方法读取字节流一个字节*/
	public static void readTest() throws IOException{
		InputStream is=new FileInputStream(new File("test1.properties"));
		int i=is.read();
		System.out.println((char)i);
	}
	//while循环读取字节流所有字节
	public static String InputStreamRead(String value) throws IOException{
		String result=null;
		InputStream is=new FileInputStream(new File(value));
		int flag=0;
		byte[] b=new byte[1024];
		while((flag=is.read(b))>0){
			//将文件中读取的字节总数赋值给flag
			System.out.println(flag);
			//将byte数组转成字符串
			System.out.println(new String(b,0,flag) );
		}
		return result;
	}
	public static String ReaderTest(String value) throws IOException{
		String result=null;
		Reader is=new FileReader(new File(value));
		int flag=0;
		byte[] b=new byte[1024];
		while((flag=is.read())>0){
			//将文件中读取的字节总数赋值给flag
			System.out.println(flag);
			//将byte数组转成字符串
			System.out.println(new String(b,0,flag) );
		}
		return result;
	}
}

InputStreamReader与OutputStreamWriter

package test20141212;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class demo3 {
	public static void main(String[] args) throws IOException {
		File f=new File("test1.txt");
		InputStreamReaderTest(f);
//		OutputStreanWriterTest(f);
	}
	public static void createFile(File  filename) throws IOException{
		if(!filename.exists()){
			filename.createNewFile();
			System.out.println("创建成功");
		}else{
			System.out.println("已存在");
		}
	}
	public static void InputStreamReaderTest(File f) throws IOException{
		createFile(f);
		InputStreamReader is=new InputStreamReader(new BufferedInputStream(new FileInputStream(f)));
		char[] pool=new char[100];
		int i=-1;
		while((i=is.read(pool))!=-1){
			System.out.println(new String(pool,0,i));
		}
		is.close();
	}
	public static void OutputStreanWriterTest(File f) throws IOException{
		Writer out=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(f)));
		String content="content";
		out.write(content);
		out.close();
	}
}


LineNumberReader

package j2se.core.io.charsetStream;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;

/**
 * LineNumberReader 使用范例
 */
public class LineNumberReaderDemo {

	public static void main(String[] args) throws IOException {
		File file = new File("src/j2se/core/io/charsetStream/LineNumberReaderDemo.java");
		/*LineNumberReader reader = new LineNumberReader(
				new BufferedReader(
						new FileReader(file)));*/
		
		LineNumberReader reader = new LineNumberReader(new FileReader(file));
		String s;
		while ( (s = reader.readLine()) != null ) {
			System.out.print(reader.getLineNumber() + "\t:\t");
			System.out.println(s);
		}
			
	}
	
	

}

ObjectInputStream与ObjectOutputStream

package test20141212;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import demo4.Human;

public class demo4 {
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Human[] human={
			new Human("czk",22),
			new Human("czk",23),
			new Human("czk",23)
		};
		File f=new File("test1.txt");
//		objectOutputStramTest(f,human);
		List<Human> list=new ArrayList<Human>();
		list=objectInputStreamTest(f);
		for(Human h:list){
			System.out.println(h.toString());
		}
	}
	public static List<Human> objectInputStreamTest(File f) throws FileNotFoundException, IOException, ClassNotFoundException{
		ObjectInputStream o=new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
		List<Human> list=new ArrayList<Human>();
		while(true){
			try {
				while(true){
				Human h=(Human)o.readObject();
				list.add(h);
				}
			} catch (EOFException  e) {
				e.printStackTrace();
			}finally{
				o.close();
			}
			return list;
		}
	}
	public static void objectOutputStramTest(File f,Human[] h) throws FileNotFoundException, IOException{
		OutputStream o=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
		for(Human human:h){
			((ObjectOutputStream) o).writeObject(human);
		}
		o.close();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值