Java IO 相关

       在 java.io 包中 File 是一个常用类。这个名字容易产生一些误会。它表示一个文件名
(或目录名),而不是文件本身。文件在不同的操作系统下有不同的命名规则,Java 提供
File 类,通过 File 类隐藏这些名字上的不同。

       File 的方法是对文件以一个整体形势进行操作:删除文件、创建目录、列出目录
、查询文件大小和修改日期等。虽然 File 类提供了操作目录和目录下文件的方法,但
File 类没有提供操作文件内容的方法,没法对字符或字进行读写。顺序文件的 I/O 是
通过一个抽象流来实现的。

        stream 是一个对象,数据能够顺序的从中读出,或者能够顺序的写入其中。
java.io 包中有 40多个 stream 类。InputStream, OutputStream 和他们各自
的子类都用于读写 字节流 对象(byte , 一个byte 8 位)。  Reader , Writer 和他
们各自的子类用于读写 Unicode 字符流对象(char, 一个字符包含 2个字节)。
除了这些流之外, java.util.zip 包含数据压缩解压缩的数据流类。


字节输入流    

BufferedInputStream    

从一个 InputStream 中读取一个缓冲区,然后返回整个缓冲区的内容,使小量读写更
有效。
ByteArrayInputStream

从一个字节数组中顺序读入多个字节

CheckedInputStream
这个 java.util.zip 包中的类计算从 InputStream 中读入内容的校验合

DataInputStream
从一个 InputStream 中读取 java 基本类型的二进制表达形式

FileInputStream
从文件中顺序读入多个字节

FilterInputStream
字节输入流的过滤器类的超类

GZIPInputStream
这个 java.io.zip 包中的类从 InputStream 中读入以 GZIP 形式压缩的内容

InflaterInputStream
GZIPInputStream 和 ZipInputStream 的超类

InputStream
所有字节输入流的超类

ObjectInputStream
从一个字节流读入二进制形式的 Java 对象和基本值,这个类用来反序列化对象。

PipedInputStream
读取写入到它所连接的 PipedOutputStream中的多个字节,在多线程程序中使用

PushbackInputStream
将一个固定大小的“回推缓冲区”添加到输入流中,从而这些字节内容可以是"读不出"
的,在某些解析程序中有用。

SequenceInputStream
从两个或两个以上的输入流中顺序读入多个字节的内容,这些输入流就好像单独的一个
输入流一样。

ZipInputStream
用来解压 ZIP 文件中的多个项目

 

字符输入流

BufferedReader
从 Reader 中读取多个字符到一个缓冲区,然后返回整个缓冲区内容。使小量读写更加
有效。

CharArrayReader
从一个 Char 数组中顺序读入多个字符

FileReader
从文件中顺序读入多个字符,是 InputStreamReader 的子类,能从一个自动创建的
FileInputStream 中读取数据。

FilterReader
字符输入流过滤器类的超类

InputStreamReader
从一个字节输入流读入字符,使用默认的编码方式或指定的编码方式将字节转化成字符

LineNumberReader
读取多行文本,并记录已经读取多少内容。

PipedReader
读取它所连接的 PipedOutputStream 中的多个字符。在多线程程序中使用。

PushbackReader
将固定大小的“回推缓冲区”添加到 Reader 中,从而这些内容可以是 “读不出”的
,这在某些解析程序中很有用。

Reader
所有字符输入流的超类

StringReader
从一个字符串中顺序读入字符

-------------------------------------------------------------
字节输出流

BufferedOutputStream
将字节按缓冲区输出,当缓冲区满时才将其中字节写往 OutputStream

ByteArrayOutputStream
往字节数组中写入字节

CheckedOutputStream
这个 java.util.zip 包中的类,计算写入 OutputStream 中数据的校验合

DataOutputStream
以 java 基本类型的二进制形式向 OutputStream 写入

DeflaterOutputStream
GZIPOutputStream 和 ZipOutputStream 的超类

FileOutputStream
向文件中顺序写入字节

FilterOutputStream
字节输出流过滤器类的超类

GZIPOutputStream
这个 java.util.zip 包中的类,使用 GZIP 格式压缩写入其中的内容
然后输出

ObjectOutputStream
向一个 OutputStream 写入二进制形式的 java 对象和基本类型,这个类用来序列化
对象。

PipedOutputStream
将字节写入与他连接的  PipedInputStream 中,在多线程程序中使用

PrintStream
写入文本形式的 java 对象和基本类型,不推荐使用 PrintStream ,使用 PrintWriter 代替

ZipOutputStream
这个  java.util.zip 包中的类压缩 ZIP 文件中的数据

------------------------------------------------------------
字符输出流

BufferedWriter
为提高效率将字符按缓冲区输出,仅当缓冲区满时才将字符写往输出流。

CharArrayWriter
向字符数组中写入字符

FileWriter
向文件中顺序写入字符,它是 OutputStreamWriter 的一个子类,能自动创建
一个FileOutputStream.

FilterWriter
字符输出流过率器类的超类

OutputStreamWriter
将字符写入字节输出流,使用默认编码方式或指定编码方式将字符转化成字节。

PipedWriter
将字符写入他连接的 PipedReader 中,在多线程程序中用到

StringWriter
将字符顺序写入一个内部创建的 StringBuffer 中

PrintWriter
在Writer 中写入文本形式的 Java 对象和基本类型

Writer
字符输出流的超类

----------------------
文件 copy 的 例子 代码

Java代码 复制代码
  1. /**  
  2.  * io 例子,文件copy 的一个类,加入了相关检验  
  3.  */  
  4. package cn.lokvin.examples.io;   
  5.   
  6. import java.io.BufferedReader;   
  7. import java.io.File;   
  8. import java.io.FileInputStream;   
  9. import java.io.FileOutputStream;   
  10. import java.io.IOException;   
  11. import java.io.InputStreamReader;   
  12.   
  13. /**  
  14.  * @author lokvin  
  15.  *  
  16.  */  
  17. public class FileCopy {   
  18.   
  19.     /**  
  20.      * @param args  
  21.      */  
  22.     public static void main(String[] args) {   
  23.         if(args.length != 2)    
  24.             System.err.println("Usage: java FileCopy <source> <destination>");   
  25.         else {   
  26.             try {   
  27.                 copy(args[0], args[1]);    
  28.             }catch(IOException ex) {   
  29.                 System.err.println(ex.getMessage());   
  30.             }   
  31.         }   
  32.   
  33.     }   
  34.        
  35.     /**  
  36.      * 文件复制的静态方法,复制前进行检查  
  37.      * @param from_name  
  38.      * @param to_name  
  39.      */  
  40.     public static void copy(String from_name, String to_name) throws IOException {   
  41.   
  42.         File fromFile = new File(from_name);   
  43.         File toFile = new File(to_name);   
  44.            
  45.         //确定源文件存在,可读   
  46.         if(!fromFile.exists()) {   
  47.             abort("no such source file: " + from_name);   
  48.         }   
  49.            
  50.         if(!fromFile.canRead()) {   
  51.             abort("source file is unreadble: " + from_name);   
  52.         }   
  53.            
  54.         //如果目标是目录,用源文件名命名目标   
  55.         if(toFile.isDirectory()) {   
  56.             toFile = new File(toFile, fromFile.getName());   
  57.            
  58.         }   
  59.            
  60.         if(toFile.exists()) {   
  61.             if(!toFile.canRead()) {   
  62.                 abort("destination file is unreadble: " + to_name);   
  63.             }   
  64.                
  65.             // 询问是否覆盖   
  66.             System.out.println("Overwrite existing file " + toFile.getName() +   
  67.                     " ? (Y/N):");   
  68.             System.out.flush();   
  69.                
  70.             //获得用户响应   
  71.             BufferedReader in = new BufferedReader   
  72.                 (new InputStreamReader(System.in));   
  73.                
  74.             String response = in.readLine();   
  75.                
  76.             if(!response.equalsIgnoreCase("y")) {   
  77.                 abort("existing file was not overwritten. ");   
  78.             }   
  79.         }   
  80.                
  81.             else {   
  82.                 String parent = toFile.getParent();   
  83.                 if(parent == null) {//如果不存在,使用当前目录   
  84.                     parent = System.getProperty("user.dir");   
  85.                 }   
  86.                    
  87.                 File dir = new File(parent);   
  88.                 if(!dir.exists()) {   
  89.                     abort("destination directory doesn't exist: "+  parent);   
  90.                        
  91.                 }   
  92.                 if(dir.isFile()) {   
  93.                     abort("destination is not a directory: " + parent);   
  94.                 }   
  95.                 if(!dir.canRead()) {   
  96.                     abort("destination dir is unwirteable: " + parent);   
  97.                        
  98.                 }   
  99.             }   
  100.                
  101.             //到此说明一切正常   
  102.                
  103.             FileInputStream fromStream = null;   
  104.             FileOutputStream toStream = null;   
  105.                
  106.             try{   
  107.                 fromStream = new FileInputStream(fromFile);   
  108.                 toStream = new FileOutputStream(toFile);   
  109.                    
  110.                 byte[] buffer = new byte[4096];   
  111.                 int bytes_read;   
  112.                    
  113.                 while((bytes_read = fromStream.read(buffer)) != -1 ){   
  114.                     toStream.write(buffer, 0, bytes_read);   
  115.                        
  116.                 }   
  117.                    
  118.                    
  119.             }   
  120.                
  121.             finally {   
  122.                    
  123.                 if(fromStream != nulltry{fromStream.close(); } catch(IOException ex) {}   
  124.                 if(toStream != nulltry {toStream.close();} catch(IOException ex) {}   
  125.                    
  126.             }   
  127.                
  128.         }   
  129.   
  130.        
  131.     private static void abort(String msg) throws IOException{   
  132.         throw new IOException("FileCopy: " + msg);   
  133.     }   
  134.   
  135. }  
/**
 * io 例子,文件copy 的一个类,加入了相关检验
 */
package cn.lokvin.examples.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author lokvin
 *
 */
public class FileCopy {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length != 2) 
			System.err.println("Usage: java FileCopy <source> <destination>");
		else {
			try {
				copy(args[0], args[1]); 
			}catch(IOException ex) {
				System.err.println(ex.getMessage());
			}
		}

	}
	
	/**
	 * 文件复制的静态方法,复制前进行检查
	 * @param from_name
	 * @param to_name
	 */
	public static void copy(String from_name, String to_name) throws IOException {

		File fromFile = new File(from_name);
		File toFile = new File(to_name);
		
		//确定源文件存在,可读
		if(!fromFile.exists()) {
			abort("no such source file: " + from_name);
		}
		
		if(!fromFile.canRead()) {
			abort("source file is unreadble: " + from_name);
		}
		
		//如果目标是目录,用源文件名命名目标
		if(toFile.isDirectory()) {
			toFile = new File(toFile, fromFile.getName());
		
		}
		
		if(toFile.exists()) {
			if(!toFile.canRead()) {
				abort("destination file is unreadble: " + to_name);
			}
			
			// 询问是否覆盖
			System.out.println("Overwrite existing file " + toFile.getName() +
					" ? (Y/N):");
			System.out.flush();
			
			//获得用户响应
			BufferedReader in = new BufferedReader
				(new InputStreamReader(System.in));
			
			String response = in.readLine();
			
			if(!response.equalsIgnoreCase("y")) {
				abort("existing file was not overwritten. ");
			}
		}
			
			else {
				String parent = toFile.getParent();
				if(parent == null) {//如果不存在,使用当前目录
					parent = System.getProperty("user.dir");
				}
				
				File dir = new File(parent);
				if(!dir.exists()) {
					abort("destination directory doesn't exist: "+  parent);
					
				}
				if(dir.isFile()) {
					abort("destination is not a directory: " + parent);
				}
				if(!dir.canRead()) {
					abort("destination dir is unwirteable: " + parent);
					
				}
			}
			
			//到此说明一切正常
			
			FileInputStream fromStream = null;
			FileOutputStream toStream = null;
			
			try{
				fromStream = new FileInputStream(fromFile);
				toStream = new FileOutputStream(toFile);
				
				byte[] buffer = new byte[4096];
				int bytes_read;
				
				while((bytes_read = fromStream.read(buffer)) != -1 ){
					toStream.write(buffer, 0, bytes_read);
					
				}
				
				
			}
			
			finally {
				
				if(fromStream != null) try{fromStream.close(); } catch(IOException ex) {}
				if(toStream != null) try {toStream.close();} catch(IOException ex) {}
				
			}
			
		}

	
	private static void abort(String msg) throws IOException{
		throw new IOException("FileCopy: " + msg);
	}

}

 

你可能感兴趣的:(java,多线程)