class CopyDemo{
public static void copydir (File dir,File
dest){
//文件夹部分
if(!dir.exists()) System. out. println("别逗" );
if(dir .isDirectory ()){
if(!dest.exists()) dest. mkdir();
for(String files :dir .list ())
{//将目录下的每一个文件名罗列出来,通过递归筛选文件夹或者文件执行相关代码
File
dirfile=new File (dir,files) ;
File
destfile=new File (dest,files) ;
copydir( dirfile, destfile);
}
}
//文件部分
else{
byte[] buff =new byte[ 1024];
int len;
try {
//文件复制就是把数据从硬盘的一个地方写到另一个地方
InputStream
ips =new FileInputStream(dir );
OutputStream
ops =new FileOutputStream(dest );
while((len =ips .read (buff)) !=-1 )ops.write( buff, 0, len);
ips .close ();
ops .close ();
} catch (Exception e ) {
// TODO: handle exception
e .printStackTrace ();
}
}
}
}
public class CopyTest {
public static void main (String[] args) {
// TODO Auto-generated method stub
CopyDemo cd=new CopyDemo();
File dir=new File(arg[0]);
File dest= new File(arg[1]);
try {
cd.copydir(dir, dest);
} catch (Exception e ) {
// TODO: handle exception
e .printStackTrace ();
}
}
}
try {
//字符转换流可以将原本是字节流输出,转成字符流输出
File
file = new File ("d:" +File . separator+ "singer.txt") ;
if (! file. exists()) file. createNewFile ();
Writer
ww = new OutputStreamWriter (new FileOutputStream (file ));
ww .write ( "我是歌手" );
ww .close ();
//同理也可以将字节输入流转换成字符流
//最常用的是把从控制台获得的字节流转换转换成字符流以便操作
Reader
rr = new InputStreamReader (System . in) ;
char [] chs= new char [ 100] ;
int len;
while ((len =rr .read ( chs)) !=-1 )
System . out. println( new String( chs, 0, len ));