Reputation: 117
I currently have a simple program that inserts data into a mysql database table like so:
System.out.println("New path created: " + newPath);
String newPathfinal = newPath.toString();
try (Connection connection = DBConnection.getConnection())
{
String file = "C:/Users/User/Desktop/Test/" + newPathfinal;
String loadQuery = "LOAD DATA LOCAL INFILE '" + file + "' INTO TABLE txn_tbl FIELDS TERMINATED BY ','"
+ " LINES TERMINATED BY '\n' " + "IGNORE 1 LINES(txn_amount, card_number, terminal_id)";
System.out.println(loadQuery);
Statement stmt = connection.createStatement();
stmt.execute(loadQuery);
System.out.println("Data import success");
}
catch (Exception e)
{
e.printStackTrace();
}
However the incoming files vary in names and their content(columns are different) so i want to be filter the files by name into different LOAD DATA LOCAL INFILE commands so that the data can be inserted correctly.
I was thinking along the lines of using a simple if-else statement since i decreed the filenames as String but i realised if-else statements take the FULL filename as a condition, and this would not work in the case i want to filter by partial filename. Like so: if(newPathfinal == “apple" {}
I want all files containing the word "apple" to be imported into a certain table.
For example, let's say i want to import files with name starting from "java" like java_1 into mySQL table java1. And files named "apple" like apple_1 into apple1. java_2 and apple_2 proceed to be imported and i need to correspondingly filter these into the right LOAD DATA LOCAL INFILE statements to insert their data into the right tables.
Is there any way i can filter by partial filename? I read up on jfilechooser and fileFilter but these seem to focus on the file extensions which is not my area of focus since the files are all in the same .csv format.
Many thanks once again!
Upvotes: 2
Views: 159
Reputation: 4592
if
statements only "take the FULL filename as a condition" if you program them that way. Using methods available from the File and String classes you can do much more.
First, File.getName() will get you the name part of file path, separate from the directory:
File file = new File("C:/Users/User/Desktop/Test/java_12345.txt");
System.out.println(file.getName()); // prints "java12345.txt"
Now you can go to work on the file name with methods from String
. For your examples, startsWith() would be ideal:
if (file.getName().startsWith("apple")) {
// handle "apple" files
}
else if (file.getName().startsWith("java")) {
// handle "java" files
}
There's also endsWith in case you'd like to filter out specific extensions as well:
if (file.getName().startsWith("apple") && file.getName().endsWith(".txt") {
// handle "apple*.txt" files
}
else if (file.getName().startsWith("java")) {
// handle "java" files
}
And that's just the easy stuff. There are all sorts of ways you can examine and test String
s that might come in handy either now or in the future. You can use substring() to peek inside the name, or even matches() for really tricky filtering.
Upvotes: 2
Reputation: 1410
It sounds like you want to use the startsWith
method.
if (newPathFinal.startsWith("java"))
{
// do java stuff
}
else if (newPathFinal.startsWith("apple"))
{
// do apple stuff
}
Upvotes: 2