1

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";
 }
 }
}
asked Jul 7, 2015 at 20:26
3
  • 3
    Hey 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! Commented 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 :) Commented Jul 7, 2015 at 20:32
  • Please, use StringBuilder and append in order to reduce the Strings created and save lots of memory! Commented Jul 7, 2015 at 20:32

2 Answers 2

1

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

answered Jul 7, 2015 at 20:34
0

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";
 }
 }
}
answered Jul 7, 2015 at 20:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.