public class FileIOTest{
public void copyFile ( String filePath ) {
int len = 0 ;
FileInputStream fis = null ;
FileOutputStream fos = null ;
File file = new File(filePath) ;
try{
fis = new FileInputStream( filePath ) ;
String newFileName = file.getPath().replaceAll("\\.txt", ".xxx");
fos = new FileOutputStream( new File( newFileName ) );
byte[] bt = new byte[1024];
while ( ( len = fis.read( bt )) != -1){
fos.write( bt , 0 , len );
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
fis.close();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main (String args[]) throws Exception {
FileIOTest ft= new FileIOTest() ;
ft.copyFile("D:\\111.txt");
}
}
本文提供了一个使用Java进行文件复制的示例代码。通过创建FileInputStream和FileOutputStream对象读取和写入文件,实现了将一个文件的内容复制到另一个文件的功能。代码中还包括了异常处理和资源关闭的逻辑。
360

被折叠的 条评论
为什么被折叠?



