欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java输入输出流,

来源: javaer 分享于  点击 18476 次 点评:82

java输入输出流,


在介绍输入输出流之前,先看一个类:File。应注意,它不是代表一个文件,而只是一个文件或一个目录的名称。先看一个例子:

import java.io.File;
import java.io.IOException;
import java.util.Stack;

public class FileIterator {	
	private Stack<File> filestack = new Stack<File>();	
	
	/*
	 * 利用栈以非递归方式实现文件夹下所有文件的遍历
	 * 参数为要遍历的文件夹的路径
	 * */
	public void printAllFile(String root) throws IOException{
		filestack.push(new File(root));//若root是相对路径,则它是相对于当前路径,即工程根目录
		File f;
		while(!filestack.isEmpty()){
			f = filestack.pop();
			if(f.isDirectory()){
				File[] files = f.listFiles();
				for(File file:files){
					if(file.isDirectory()){
						filestack.push(file);
					}
					else{
						System.out.println(file.getCanonicalPath());
					}
				}
			}
			
		}
	}
	
	/*
	 * 以递归方式遍历文件夹
	 * */
	public void printAllFile(File root) throws IOException{
		File[] files = root.listFiles();
		int length = files.length;
		for(int i = 0;i < length;i++){
			if(files[i].isDirectory())
				printAllFile(files[i]);
			else{
				System.out.println(files[i].getCanonicalPath());
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			//new FileIterator().printAllFile(".");
			new FileIterator().printAllFile(new File("./src"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

创建一个代表目录的File对象,然后输出该目录下所有的文件。

下面开始说InputStream类,下图是其继承结构图:


输出流OutputStream也是对应的了。

InputStream和OutputStream是面向字节(byte)的,如果要处理字符数据(两个字节,如汉字,char型数据)又该怎么办呢?答案是Reader和Writer接口。这里用到了适配器模式了。Reader和Writer是目标接口,需要引入适配器类:InputStreamReader。关于适配器模式的详细介绍请看:点击打开链接

下图是Reader接口的继承结构图:


Writer也是差不多了。需要注意的是,Writer另外有个子类PrintWriter,它是很常用的,其print(),println()可以自动清空数据。PrintWriter的构造也是很容易的,默认的已经是缓存的,所以使用还是很方便的。

可以看出BufferedReader对应BufferedInputStream,其他的类似。它们的构造方法有个Reader参数,这就是修饰器模式的应用了,通过不同的组合方式达到不同的功能效果。这里FilterInputStream就是被修饰的基类了,其他的修饰器都是对它进行修饰。详细请看:点击打开链接

下面是一些例子,其中的奥妙通过代码来说明吧:

使用FileReader:

public static String read(String file) throws IOException{
		File f = new File(file);
		StringBuilder sb = new StringBuilder();
		BufferedReader br = new BufferedReader(new FileReader(f));
		String s;
		while((s = br.readLine()) != null){
			sb.append(s + "\n");
		}
		br.close();
		return sb.toString();
	}

使用DataInputStream:

public static void byteArrayInputStream(String file) throws IOException{
		DataInputStream dis = new DataInputStream(new ByteArrayInputStream(read(file).getBytes("GBK")));
		while(dis.available() != 0){
			System.out.print((int)dis.readByte());
		}
		dis.close();
	}

使用PrintWriter:

public static void write(String filename,String text){
		PrintWriter pw = null;
		try{		
			pw = new PrintWriter(filename);
			try{	
				pw.print(text);
			}
			finally{
				pw.close();
			}
		}	
		catch(IOException e){
			throw new RuntimeException(e);
		}
	}

使用InputStreamReader:

public static void systemIn() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s;
		while((s = br.readLine()) != null){
			System.out.println(s+"\n");
		}
	}

恩,差不多就这些吧。

关于序列化和JDK1.4引入的nio包,以后再说吧。

相关文章

    暂无相关文章
相关栏目:

用户点评