كيفية إنشاء نسخة طبق الأصل من .زلس أو .ملف زلسكس باستخدام رمز جافا?[مغلق]

StackOverflow https://stackoverflow.com//questions/10668902

  •  11-12-2019
  •  | 
  •  

سؤال

أحتاج إلى إنشاء نسخة طبق الأصل من .زلس أو .ملف زلسكس من رمز جافا.يحتوي الملف على الكثير من التنسيق والصيغ والدمج فيه ، والذي يجب أن يظل سليما.التطبيق هو في الأساس تطبيق سطح المكتب ويجب أن يكون منصة مستقلة (لا .ش الثابتة والمتنقلة).يرجى اقتراح أفضل طريقة ممكنة للمحترفين نحويا وكذلك الأداء الحكيم...

import java.io.*;

public class CopyFile{
  private static void copyfile(String srFile, String dtFile){
  try{
  File f1 = new File(srFile);
  File f2 = new File(dtFile);
  InputStream in = new FileInputStream(f1);

  //For Append the file.
//  OutputStream out = new FileOutputStream(f2,true);

  //For Overwrite the file.
  OutputStream out = new FileOutputStream(f2);

  byte[] buf = new byte[1024];
  int len;
  while ((len = in.read(buf)) > 0){
  out.write(buf, 0, len);
  }
  in.close();
  out.close();
  System.out.println("File copied.");
  }
  catch(FileNotFoundException ex){
  System.out.println(ex.getMessage() + " in the specified directory.");
  System.exit(0);
  }
  catch(IOException e){
  System.out.println(e.getMessage());  
  }
  }
  public static void main(String[] args){
  switch(args.length){
  case 0: System.out.println("File has not mentioned.");
    System.exit(0);
  case 1: System.out.println("Destination file has not mentioned.");
  System.exit(0);
  case 2: copyfile(args[0],args[1]);
  System.exit(0);
  default : System.out.println("Multiple files are not allow.");
  System.exit(0);
  }
  }
هل كانت مفيدة؟

المحلول

إذا كنت تستخدم جافا 7 ، هناك طريقة سهلة حقا للقيام بذلك: Files.copy.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top