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";
}
}
}
-
3Hey buddy, Welcome to Stack Overflow! Please show us some code that you've tried. We are not a code-writing service, but we can help if you run into problems while writing your program. Happy coding!Olivier Poulin– Olivier Poulin2015年07月07日 20:28:36 +00:00Commented Jul 7, 2015 at 20:28
-
Basics on io: docs.oracle.com/javase/tutorial/essential/io I should hope you can figure out how to add quotes and commas on your own :)Orch– Orch2015年07月07日 20:32:10 +00:00Commented Jul 7, 2015 at 20:32
-
Please, use StringBuilder and append in order to reduce the Strings created and save lots of memory!melli-182– melli-1822015年07月07日 20:32:27 +00:00Commented Jul 7, 2015 at 20:32
2 Answers 2
The simplest way if you do not want to use any FileReader or BufferedReader is to dump the text into your Java program.
Depending on the Operating System you are using. If you are using Windows.
c:\>Java myProgram < file.txt
You can now scan it like scanning for normal String input.
Scanner scn = new Scanner(System.in);
String str = "";
while(scn.hasNext()){
str += "\'" + scn.nextLine() + "\'" + ",";
}
As for other reading methods, it has been discussed widely in SO: Reading a plain text file in Java
Refactoring your code:
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 = "";
StringBuilder sb = new StringBuilder();
sb.append(new_line)
.append(my_quote)
.append(line)
.append(my_quote)
.append(my_sep);
while(line != null){
line = reader.readLine();
sb.append(my_quote)
.append(line)
.append(my_quote)
.append(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";
}
}
}