Java学习——读写txt文件

package HHH;
import java.io.*;
import static java.lang.System.out;

public class OpenFile {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        try{
            FileInputStream rf = new FileInputStream("openFile.txt");
            FileOutputStream wf = new FileOutputStream("write.txt");
            int n=512;
            byte buffer[] = new byte[n];//buffer就是个byte类型数组
            while((rf.read(buffer,0,n) != -1) && (n>0))//读取输入流
            {
                out.print(new String(buffer));//在屏幕中输出,强制转换成字符串
                wf.write(buffer,0,buffer.length);
                
            }
            out.println();
            rf.close();
            wf.close();
        }catch(IOException ioe){
            out.println(ioe);
        }catch(Exception e){
            out.println(e);
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/caiyishuai/p/9983804.html