Last active
October 10, 2015 08:40
-
-
Save veryyoung/3b43dbc8c8e7dbfa432f to your computer and use it in GitHub Desktop.
合并文件,可用来制作传说中的图种。运行时把文件路径当做参数传入,最后一个参数为输出文件的路径。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.nio.channels.FileChannel; | |
| public class FileGathering { | |
| public static void main(String[] args) throws IOException { | |
| combineFiles(args); | |
| } | |
| public static void combineFiles(String[] paths) throws IOException { | |
| int pathsSize = paths.length; | |
| if (pathsSize < 2) { | |
| System.err.println("请至少输入两个文件路径"); | |
| return; | |
| } | |
| File outputFile = new File(paths[pathsSize - 1]); | |
| if (!outputFile.exists()) { | |
| outputFile.createNewFile(); | |
| } | |
| FileChannel outputFileChannel = new FileOutputStream(outputFile).getChannel(); | |
| FileChannel inFileChannel; | |
| for (int i = 0; i < pathsSize; i++) { | |
| inFileChannel = new FileInputStream(new File(paths[i])).getChannel(); | |
| inFileChannel.transferTo(0, inFileChannel.size(), outputFileChannel); | |
| inFileChannel.close(); | |
| } | |
| outputFileChannel.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
perfect!