Skip to content

Instantly share code, notes, and snippets.

@geo-stanciu
Last active December 30, 2015 08:49
Show Gist options
  • Save geo-stanciu/7805548 to your computer and use it in GitHub Desktop.
Save geo-stanciu/7805548 to your computer and use it in GitHub Desktop.
Split a large txt file in several files containing x lines in java 7
/*
Copyright (c) 2013, Gheorghita Stanciu gheorghita(dot)stanciu(at)gmail(dot)com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class SplitLargeTxt {
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.out.println("Usage: java SplitLargeTxt filename numberOfLinesPerfile");
return;
}
String fileName = args[0];
int n = Integer.valueOf(args[1]);
int i = 0;
int fileNumber = 0;
String name;
File file = new File(fileName);
String absolutePath = file.getAbsolutePath();
name = file.getName();
name = name.substring(0, name.lastIndexOf(".txt"));
String filePath = absolutePath.substring(0, absolutePath.lastIndexOf(name));
Path path = Paths.get(fileName);
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
while (true) {
String line = null;
Path newPath = Paths.get(filePath + name + String.format("%04d", fileNumber) + ".txt");
try (BufferedWriter writer = Files.newBufferedWriter(newPath, StandardCharsets.UTF_8)) {
while ((line = reader.readLine()) != null) {
i++;
writer.write(line);
writer.newLine();
if (i == n) {
i = 0;
fileNumber++;
break;
}
}
}
if (line == null)
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment