从文本文件中创建java中的字符串

时间:2015-07-07 20:26:15

标签: java string

我有一个文本文件,其中有一个用户ID列表,如下所示;

798574
890859
984058
484849
etc...

如何将此文本文件读入Java,然后创建一个单独的字符串,将每个ID包装在引号中并用逗号分隔它们?

'798574','890859','984058','484849',.....

我试过这个,但我觉得它根本没用;

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

2 个答案:

答案 0 :(得分:1)

如果您不想使用任何FileReader或BufferedReader,最简单的方法是将文本转储到Java程序中。

取决于您使用的操作系统。如果您使用的是Windows。

c:\>Java myProgram < file.txt

您现在可以扫描它,就像扫描正常的字符串输入一样。

Scanner scn = new Scanner(System.in);
String str = "";
while(scn.hasNext()){
    str += "\'" + scn.nextLine() + "\'" + ",";
}

至于其他阅读方法,已在SO中广泛讨论: Reading a plain text file in Java

答案 1 :(得分:0)

重构代码:

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