간단한 파일 읽기 InputStream 및 쓰기 OutputStream

2239 단어 입출력 흐름
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 *  IO 
 *  1、  File  
 *  2、       InputStream  FileInputStream 
 *  3、 :byte car[] = new byet[1024]; +read+        
 *  4. --> 。 
 */
public class ByteIo {
	public static void main(String[] args) {
		// 
		File src = new File("E:/Media/a.txt");
		// 
		InputStream it = null;
		try {
			it = new FileInputStream(src);
			//    
			byte car[] = new byte[50];
			int len =0 ;
			while(-1!=(len=it.read(car))){
				String info = new String(car,0,len);
				System.out.println(info);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println(" ");
		} catch (IOException e) {
			System.out.println(" ");
			e.printStackTrace();
		}finally{
			if(null!=it){
				try {
					it.close();
				} catch (IOException e) {
					System.out.println(" ");
					e.printStackTrace();
				}
			}
		}
	}
}

=======================================================================================================================================
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 *  IO 
 *  1、  File  
 *  2、       OutputStream  FileOutputStream 
 *  3、 :write() +flush
 *  4. --> 。 
 */
public class ByteIo02 {
	public static void main(String[] args) {
		File src = new File("E:/Media/a.txt");
		OutputStream os = null;
		try {
                        // true  
                        os = new FileOutputStream(src,true);
			String str = "zhang san qu mai bao zi ! \r
"; byte date[] = str.getBytes(); os.write(date, 0, date.length); os.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println(" "); } catch (IOException e) { e.printStackTrace(); System.out.println(" "); }finally{ if(null!=os){ try { os.close(); } catch (IOException e) { e.printStackTrace(); System.out.println(" "); } } } } }

좋은 웹페이지 즐겨찾기