出现报错byte words[] =new char[1024];
改后byte words[] =new byte[1024];复制是成功但内容是乱码。
package Test2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
- @param args
-
上机练习2:(复制文本文件)247页
*/
public class FileInorOut2 {public static void main(String[] args) {
//声明流对象
FileInputStream fis=null;
FileOutputStream fos=null;
try {
//1、创建输入流对象,负责读取内容
fis=new FileInputStream("G:/我的青春谁做主.txt");
//2、创建输出流对象,将写入内容
fos=new FileOutputStream("E:/myFile/myPrime.txt");
//3、创建中转站数组,存放每次读取的内容
byte words[] =new byte[1024];
//4、通过循环实现文件读写
while ((fis.read())!=-1) {
fis.read (words); //读取文件
fos.write(words,0,words.length);//写入文件
}
System.out.println("内容复制成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5、关闭流
try {
if (fis!=null) {
fos.close();
}
if (fos!=null) {
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
}