复制文件FileStreamDemo.java的内容至文件file.txt文件,并显示输出 file.txt文件内容
package
sky;
import java.io. * ;
public class FileStreamDemo ... {
public static void main(String[] args)...{
try...{
byte[] buffer=new byte[1024];
//源文件
FileInputStream fis=new FileInputStream(new File("FileStreamDemo.java"));
//目的文件
FileOutputStream fos=new FileOutputStream(new File("file.txt"));
System.out.println("复制文件: "+fis.available()+"字节");
while(true)...{
//复制小于1024的数据
if(fis.available()<1024)...{
int remain=-1;
while((remain=fis.read())!=-1)...{
fos.write(remain);
}
break;
}
//复制大于1024的数据
else...{
fis.read(buffer);
fos.write(buffer);
}
}
fis.close();
fos.close();
System.out.println("复制完成");
}catch(ArrayIndexOutOfBoundsException e)...{
System.out.println("using: java FileSreamDemo src des");
e.printStackTrace();
}catch(IOException e)...{
e.printStackTrace();
}
//输出file.txt文件的内容
System.out.println("***********************");
System.out.println("输出file.txt文件内容");
try...{
FileInputStream fs=new FileInputStream(new File("file.txt"));
byte[] buf=new byte[fs.available()];
fs.read(buf);
String str=new String(buf);
System.out.println(str);
}catch(ArrayIndexOutOfBoundsException e)...{
System.out.println("using: java FileSreamDemo src des");
e.printStackTrace();
}catch(IOException e)...{
e.printStackTrace();
}
System.out.println();
}
}
import java.io. * ;
public class FileStreamDemo ... {
public static void main(String[] args)...{
try...{
byte[] buffer=new byte[1024];
//源文件
FileInputStream fis=new FileInputStream(new File("FileStreamDemo.java"));
//目的文件
FileOutputStream fos=new FileOutputStream(new File("file.txt"));
System.out.println("复制文件: "+fis.available()+"字节");
while(true)...{
//复制小于1024的数据
if(fis.available()<1024)...{
int remain=-1;
while((remain=fis.read())!=-1)...{
fos.write(remain);
}
break;
}
//复制大于1024的数据
else...{
fis.read(buffer);
fos.write(buffer);
}
}
fis.close();
fos.close();
System.out.println("复制完成");
}catch(ArrayIndexOutOfBoundsException e)...{
System.out.println("using: java FileSreamDemo src des");
e.printStackTrace();
}catch(IOException e)...{
e.printStackTrace();
}
//输出file.txt文件的内容
System.out.println("***********************");
System.out.println("输出file.txt文件内容");
try...{
FileInputStream fs=new FileInputStream(new File("file.txt"));
byte[] buf=new byte[fs.available()];
fs.read(buf);
String str=new String(buf);
System.out.println(str);
}catch(ArrayIndexOutOfBoundsException e)...{
System.out.println("using: java FileSreamDemo src des");
e.printStackTrace();
}catch(IOException e)...{
e.printStackTrace();
}
System.out.println();
}
}