BufferedWriter不打印

时间:2016-09-11 13:29:11

标签: java

我正在尝试将用户名和密码组织到插槽中并假设用户名不是电子邮件,我将其视为密码。我必须将这些文件放入文件中,但不打印。它正在阅读。我可以得到一些帮助吗?提前谢谢。

public class Core {

public static void main(String[] args) {

    FileReader in = null;
    BufferedReader br = null;

    BufferedWriter uOut = null;
    BufferedWriter pOut = null;

    try {

        in = new FileReader("src/input.txt");
        br = new BufferedReader(in);

        uOut = new BufferedWriter(new FileWriter("src/username.txt"));

        pOut = new BufferedWriter(new FileWriter("src/password.txt"));

        String line = br.readLine();
        while(line != null) {

            boolean migrated = true;

            if(line.contains(":")) {

                String[] split = line.split(":");
                String user = split[0];
                String pass = split[1];

                if(!(user.contains("@") && user.contains(".com"))) {
                    migrated = false;
                }

                if(migrated) {
                    uOut.write(user, 0, user.length());
                    uOut.newLine();

                    pOut.write(pass, 0, pass.length());
                    pOut.newLine();
                } else {
                    pOut.write(user, 0, user.length());
                    pOut.newLine();
                    pOut.write(pass, 0, pass.length());
                    pOut.newLine();
                }

                line = br.readLine();
                continue;
            }

            if(!(line.contains("@") && line.contains(".com"))) {
                pOut.write(line, 0, line.length());
                pOut.newLine();

                line = br.readLine();
                continue;
            } else {
                uOut.write(line, 0, line.length());
                uOut.newLine();

                line = br.readLine();
                continue;
            }

        }

    } catch(Exception ex) {

        ex.printStackTrace();

    } finally {

        if(br != null) try { br.close(); } catch(Exception ex) { }

        if(uOut != null) try { uOut.close(); } catch(Exception ex) { }

        if(pOut != null) try { pOut.close(); } catch(Exception ex) { }

    }

}

}

我还应该提一下,我没有任何例外,也没有错误显示。

1 个答案:

答案 0 :(得分:0)

我使用文件data.txt

测试了您的代码
foo:bar
blarg:bletch
blahonga:baar

代码稍有变化

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Main {

    public static void main(String[] args) {

        FileReader in = null;
        BufferedReader br = null;

        BufferedWriter uOut = null;
        BufferedWriter pOut = null;

        try {

            in = new FileReader("data.txt");
            br = new BufferedReader(in);

            uOut = new BufferedWriter(new FileWriter("username.txt"));

            pOut = new BufferedWriter(new FileWriter("password.txt"));

            String line = br.readLine();
            while (line != null) {

                boolean migrated = true;

                if (line.contains(":")) {

                    String[] split = line.split(":");
                    String user = split[0];
                    String pass = split[1];
                    if (user.contains("@") && user.contains(".com")) {
                        migrated = false;
                    }

                    if (migrated) {
                        uOut.write(user, 0, user.length());
                        uOut.newLine();

                        pOut.write(pass, 0, pass.length());
                        pOut.newLine();
                    } else {
                        pOut.write(user, 0, user.length());
                        pOut.newLine();
                        pOut.write(pass, 0, pass.length());
                        pOut.newLine();
                    }

                    line = br.readLine();
                    continue;
                }

                if (!(line.contains("@") && line.contains(".com"))) {
                    pOut.write(line, 0, line.length());
                    pOut.newLine();

                    line = br.readLine();
                    continue;
                } else {
                    uOut.write(line, 0, line.length());
                    uOut.newLine();

                    line = br.readLine();
                    continue;
                }

            }

        } catch (Exception ex) {

            ex.printStackTrace();

        } finally {

            if (br != null) try {
                br.close();
            } catch (Exception ex) {
            }

            if (uOut != null) try {
                uOut.close();
            } catch (Exception ex) {
            }

            if (pOut != null) try {
                pOut.close();
            } catch (Exception ex) {
            }

        }

    }
}

结果是2个文件:

<强> username.txt

foo
blarg
blahonga

<强> password.txt

bar
bletch
baar