public static void main(String[] args) throwsIOException {
io();
buffer();
}/*** 以流的形式读写 可以使用任何文件 特别是二进制文件
*
*@authorhh
* @date 2014-12-11
*@throwsIOException*/
public static void io() throwsIOException {
String fname= "C:/Users/Administrator/Desktop/T.jar";
String fname2= "C:/Users/Administrator/Desktop/T2.rar";
File file= newFile(fname);
File file2= newFile(fname2);
FileInputStream in= newFileInputStream(file);
FileOutputStream out= newFileOutputStream(file2);byte[] bytes= new byte[2048];int k = 0;while((k=in.read(bytes))!=-1){
out.write(bytes);/*for (int i = 0; i < bytes.length; i++) {//这样效率很低
out.write(bytes[i]);
}*/}/*或者
* int b = 0 ;
while((b=in.read())!=-1) {
out .write(b);
}*/out.close();
in.close();
System.out.println("end");
}/*** 以字符的形式读写 实用文文本文件
* 不适用与压缩等二进制文件 否者写出来的文件会比源文件大
*@authorhh
* @date 2014-12-11
*@throwsIOException*/
public static void buffer() throwsIOException {
String fname= "C:/Users/Administrator/Desktop/1234.txt";
String fname2= "C:/Users/Administrator/Desktop/kk.txt";
File file= newFile(fname);
InputStreamReader read= new InputStreamReader(new FileInputStream(file),"GBK");//解决中文乱码//FileReader read = new FileReader(file); 也可以直接使用FileReader 但是会出想中文乱码
FileWriter fw = newFileWriter(fname2);
BufferedReader bufferedReader= newBufferedReader(read);
BufferedWriter b= newBufferedWriter(fw);
String s=null;while ((s = bufferedReader.readLine()) !=null) {
System.out.println(s);
b.write(s);
b.newLine();//换行
b.flush();
}
b.close();
read.close();
fw.close();
System.out.println("---end");
}