欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java中字节输入流和输出流的简单使用例子

程序员文章站 2022-04-09 16:43:15
...
import java.io.IOException;

public class HelloWorld {
     public static void main(String []args) {
    	 File file=new File("D:\\HelloWorld.txt");//从D盘中的HelloWorld读取
    	 File filewrite=new File("D:\\HW.txt");//保存到HW中
    	 FileInputStream in;
		try {
			in = new FileInputStream(file);
			FileOutputStream out=new FileOutputStream(filewrite);
			byte [] b=new byte[(int)file.length()];
	    	 int t;
	    	 try {
				while((t=in.read(b))!=-1) {
					 out.write(b);
				 }
			} catch (IOException e) {
				e.printStackTrace();
			}
	    	 try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    	 try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
     }
}