I have a text file that has a list of User IDs looking like this;
798574
890859
984058
484849
etc...
How can I read this text file into Java and then create a single string that wraps each ID in quotes and separates them by a comma like this?
'798574','890859','984058','484849',.....
I tried this, but I feel like it is not efficient at all;
public class SixtyK {
public String getNumbers() {
FileInputStream myfile = null;
BufferedReader reader = null;
try {
myfile = new FileInputStream("myfile.txt");
reader = new BufferedReader(new InputStreamReader(myfile));
String my_quote = "\\'";
String my_sep = ",";
String line = reader.readLine();
String new_line = "";
new_line += my_quote;
new_line += line;
new_line += my_quote;
new_line += my_sep;
while(line != null){
line = reader.readLine();
new_line += my_quote;
new_line += line;
new_line += my_quote;
new_line += my_sep;
}
System.out.println(new_line);
return new_line;
} catch (FileNotFoundException ex) {
Logger.getLogger(SixtyK.class.getName()).log(Level.SEVERE, null, ex);
return "Error";
} catch (IOException ex) {
Logger.getLogger(SixtyK.class.getName()).log(Level.SEVERE, null, ex);
return "Error";
} finally {
try {
reader.close();
myfile.close();
return "finally caught";
} catch (IOException ex) {
Logger.getLogger(SixtyK.class.getName()).log(Level.SEVERE, null, ex);
return "error in finally";
}
}
}