输入流和输出流的简单应用

来源:互联网 发布:手机录入软件 编辑:程序博客网 时间:2024/06/08 06:14

编写一个程序,在E盘下创建一个abc.txt的文本文档,通过输出流在文档内添加数据,然后在把abc.txt复制到F盘下

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {
public FileInputStream fis;
public FileOutputStream fos;

public static void main(String[] args) throws IOException {
Test t = new Test();
t.create();
t.copy();

}
public void create() throws IOException{
fos = new FileOutputStream("E:\\abc.txt");
String s = "这是写入的内容";
fos.write(s.getBytes());
fos.close();
}
public void copy() throws IOException{
fis = new FileInputStream("E:\\abc.txt");
byte[] b = new byte[1024];
int len = fis.read(b);
String s = new String(b,0,len);
fos = new FileOutputStream("F:\\abc.txt");
fos.write(s.getBytes());
fos.close();
fis.close();
}

}

0 0
原创粉丝点击