chattrat423
chattrat423

Reputation: 603

Create string in java from text file

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";
        }
    }
}

Upvotes: 1

Views: 67

Answers (2)

melli-182
melli-182

Reputation: 1234

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";
        }
    }
}

Upvotes: 0

user3437460
user3437460

Reputation: 17454

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

Upvotes: 1

Related Questions