파일 입 출력 작업 (바이트 흐름)

1770 단어 자바
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


public class ArraysDemo{
	public static void main(String args[]) throws IOException{
		write1();
		read1();
		write2();
		read2();
	}
	
	public static void write1() throws IOException{
		File f = new File("d:"+File.separator+"text.txt") ;		//    File    
		OutputStream out=new FileOutputStream(f);
		out.write("hello".getBytes());
		out.close();
	}
	
	public static void write2() throws IOException{
		File f = new File("d:"+File.separator+"text.txt") ;		//    File    
		OutputStream out=new FileOutputStream(f,true);
		out.write("\r
hello".getBytes());//\r
out.close(); } public static void read1() throws IOException{ File f = new File("d:"+File.separator+"text.txt") ; // File InputStream input=new FileInputStream(f); byte b[]=new byte[(int)f.length()]; input.read(b); input.close(); System.out.println("read1():
"+new String(b)); } public static void read2() throws IOException{ File f = new File("d:"+File.separator+"text.txt") ; // File InputStream input=new FileInputStream(f); byte temp=0; List<Byte> list=new ArrayList<Byte>(); while((temp=(byte) input.read())!=-1){ list.add(temp); } byte[] b=new byte[list.size()]; for(int i=0,j=list.size();i<j;i++){ b[i]=list.get(i); } input.close(); System.out.println("read2():
"+new String(b)); } }; /** read1(): hello read2(): hello hello **/

좋은 웹페이지 즐겨찾기