0
我需要從Java代碼創建.xls或.xlsx文件的精確副本。該文件有很多格式,公式和合並內容,需要保持原樣。該應用程序基本上是一個桌面應用程序,應該是平臺獨立的(沒有.bat或.sh pls)。 請建議最好的方式促語法以及性能明智...如何使用Java代碼創建.xls或.xlsx文件的精確副本?
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);
}
}
你不能簡單地複製文件嗎?爲什麼重要的是.xls? –
我嘗試了下面的代碼,它創建了一個文件,但是數據格式和編碼都在這個過程中丟失了---> – KDjava
@KetanDikshit如果您要發佈代碼,請將其置於您的問題中。但是複製一個文件很簡單 - 有一百萬個例子,沒有任何東西會丟失,因爲如果你做得對,它是字節相同的。 –