خلاصه مطلب
کپی کردن انواع فایل در جاوا را بصورت پروژه محور یاد خواهید گرفت، آموزش خط به خط کد های برنامه
نوع محتوامقاله آموزشی
موضوعآموزش برنامه نویسی »
زمان مطالعه۱ دقیقه
آخرین ویرایش۱۸ خرداد ۱۳۹۶
| package www.javapro.ir; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Scanner;public class CopyFiles { static void copyFileUsingStream(File source, File dest) { try { InputStream is = new FileInputStream(source); OutputStream os = new FileOutputStream(dest); byte[] buffer = newbyte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } is.close(); os.close(); } catch (IOException e) { } } public static void main(String[] args) { String addressSource = "E:\\picture\\gisekan.jpg"; String addressDestination = "F:\\image\\"; File fileSource = new File(addressSource); File fileDestination = new File(addressDestination + fileSource.getName()); copyFileUsingStream(fileSource, fileDestination); System.out.println("Succes :-)"); } } |
| static void copyFileUsingStream(File source, File dest) |
| InputStream is = new FileInputStream(source); |
| OutputStream os = new FileOutputStream(dest); |
| byte[] buffer = newbyte[1024]; |
| int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } |
| is.close(); os.close(); |
| publicstaticvoid main(String[] args) { |
| String addressSource = "E:\\picture\\gisekan.jpg"; |
.png)
| String addressDestination = "F:\\image\\"; |
| File fileSource = new File(addressSource); |
| File fileDestination = new File(addressDestination + fileSource.getName()); |
| fileSource.getName(); |
| "F:\\image\\"; |
| F:\image\gisekan.jpg |
| copyFileUsingStream(fileSource, fileDestination); System.out.println("Succes :-)"); |
| "E:\\picture\\gisekan.jpg"; |
| F:\image\gisekan.jpg |
.png)


