कॉपी करने का प्रयास करते समय एनआईओ के साथ त्रुटि मेरे पास एक फ़ाइल को किसी अन्य स्थान पर कॉपी करने के लिए कोड है।बड़ी फ़ाइल
public static void copyFile(String sourceDest, String newDest) throws IOException {
File sourceFile = new File(sourceDest);
File destFile = new File(newDest);
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
}
छोटे-छोटे टुकड़ों को कॉपी करते हैं, कहते हैं, 300-400 एमबी, सब कुछ जादू की तरह काम करता है। लेकिन जब मैंने फाइल को 1.5 जीबी के आकार की प्रतिलिपि बनाने की कोशिश की तो यह असफल रहा। ढेर है:
run: 12.01.2011 11:16:36 FileCopier main SEVERE: Exception occured while copying file. Try again. java.io.IOException: Map failed at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:748) at sun.nio.ch.FileChannelImpl.transferFromFileChannel(FileChannelImpl.java:527) at sun.nio.ch.FileChannelImpl.transferFrom(FileChannelImpl.java:590) at FileCopier.copyFile(FileCopier.java:64) at FileCopier.main(FileCopier.java:27) Caused by: java.lang.OutOfMemoryError: Map failed at sun.nio.ch.FileChannelImpl.map0(Native Method) at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:745) ... 4 more BUILD SUCCESSFUL (total time: 0 seconds)
मैं बारीकी से NIO साथ काम नहीं किया। क्या तुमसे मुझे मदद मिल सकती है? अग्रिम में बहुत बहुत धन्यवाद।
Files.copy http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/Files.html के साथ प्रयास करें यदि यह काम करता है तो src में एक नज़र डालें – oluies
धन्यवाद। लेकिन मैं इस विशिष्ट मामले में तीसरे पक्ष के पुस्तकालयों का उपयोग नहीं करना चाहूंगा। –