Last active
December 31, 2015 08:39
-
-
Save eiryu/7962060 to your computer and use it in GitHub Desktop.
This file contains 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
<dependency> | |
<groupId>commons-collections</groupId> | |
<artifactId>commons-collections</artifactId> | |
<version>3.2.1</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-io</groupId> | |
<artifactId>commons-io</artifactId> | |
<version>2.4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-lang3</artifactId> | |
<version>3.1</version> | |
</dependency> | |
@Test | |
public void fixTestData_removeColumn() throws IOException { | |
int removeColumnIndex = 2; | |
String separator = "\t"; | |
File testDataFile = new File("user.tsv"); | |
File resultTestDataFile = new File("user_result.tsv"); | |
ArrayList<String> result = new ArrayList<>(); | |
for (String row : FileUtils.readLines(testDataFile)) { | |
String[] columns = row.split(separator); | |
if (columns.length < removeColumnIndex + 1) { | |
System.out.println("removeColumnIndex is too large!"); | |
System.exit(-1); | |
} | |
List<String> columnsList = new LinkedList<>(Arrays.asList(columns)); | |
columnsList.remove(removeColumnIndex); | |
String joined = StringUtils.join(columnsList, separator); | |
result.add(joined); | |
} | |
System.out.println(result); | |
FileUtils.writeLines(resultTestDataFile, result); | |
} | |
@Test | |
public void fixTestData_addColumn() throws IOException { | |
int addColumnIndex = 2; | |
String addHeaderValue = "deleted"; | |
String addValue = "false"; | |
String separator = "\t"; | |
File testDataFile = new File("user.tsv"); | |
File resultTestDataFile = new File("user_result.tsv"); | |
boolean first = true; | |
ArrayList<String> result = new ArrayList<>(); | |
for (String row : FileUtils.readLines(testDataFile)) { | |
String[] columns = row.split(separator); | |
if (columns.length < addColumnIndex + 1) { | |
System.out.println("addColumnIndex is too large!"); | |
System.exit(-1); | |
} | |
List<String> columnsList = new LinkedList<>(Arrays.asList(columns)); | |
// header | |
if (first && StringUtils.isNotEmpty(addHeaderValue)) { | |
columnsList.add(addColumnIndex, addHeaderValue); | |
first = false; | |
} else { | |
columnsList.add(addColumnIndex, addValue); | |
} | |
String joined = StringUtils.join(columnsList, separator); | |
result.add(joined); | |
} | |
System.out.println(result); | |
FileUtils.writeLines(resultTestDataFile, result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment