首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BufferedWriter不打印

BufferedWriter不打印
EN

Stack Overflow用户
提问于 2016-09-11 13:29:11
回答 1查看 84关注 0票数 0

我试图组织用户名和密码到插槽和假设用户名不是电子邮件,我把它作为一个密码。我必须把这些文件,但它不是打印。不过,它是在阅读。能帮我个忙吗?提前谢谢。

代码语言:javascript
复制
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) { }

    }

}

}

我还应该指出,我没有任何例外,也没有任何错误可以显示。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-11 13:50:32

我用一个文件data.txt测试了您的代码

代码语言:javascript
复制
foo:bar
blarg:bletch
blahonga:baar

更改小的代码

代码语言:javascript
复制
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) {
            }

        }

    }
}

结果是两个文件:

username.txt

代码语言:javascript
复制
foo
blarg
blahonga

password.txt

代码语言:javascript
复制
bar
bletch
baar
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39436608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档