파일 입 출력 동작 (문자 흐름)

1692 단어 자바
package test;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
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    
		Writer out=new FileWriter(f);
		out.write("hello");
		out.close();
	}
	
	public static void write2() throws IOException{
		File f = new File("d:"+File.separator+"text.txt") ;		//    File    
		Writer out=new FileWriter(f,true);
		out.write("\r
word");//\r
out.close(); } public static void read1() throws IOException{ File f = new File("d:"+File.separator+"text.txt") ; // File Reader input=new FileReader(f); char[] c=new char[1024]; int len=input.read(c); input.close(); System.out.println("read1():
"+new String(c,0,len)); } public static void read2() throws IOException{ File f = new File("d:"+File.separator+"text.txt") ; // File Reader input=new FileReader(f); int temp=0; List<Character> list=new ArrayList<Character>(); while((temp=input.read())!=-1){ list.add((char)temp); } char[] b=new char[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 **/

좋은 웹페이지 즐겨찾기