編寫一個程式,在E盤下建立一個abc.txt的文字文件,通過輸出流在文件內新增資料,然後在把abc.txt複製到F盤下
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//編寫一個程式,在E盤下建立一個abc.txt的文字文件,
//通過輸出流在文件內新增資料,然後在把abc.txt複製到F盤下
public class Lianxi01 {
public static void main(String[] args) throws IOException {
String a = "這個檔案要從e盤複製到f盤";
byte[] b = a.getBytes();
FileOutputStream fos = new FileOutputStream("E:\\abc.txt");
fos.write(b);
fos.close();
}
}
____________________________分割線_____________________________________________________________________
package com.zhidi.lianxi;
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 = "這個檔案要從e盤複製到f盤";
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();
}
}