我试图组织用户名和密码到插槽和假设用户名不是电子邮件,我把它作为一个密码。我必须把这些文件,但它不是打印。不过,它是在阅读。能帮我个忙吗?提前谢谢。
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) { }
}
}}
我还应该指出,我没有任何例外,也没有任何错误可以显示。
发布于 2016-09-11 13:50:32
我用一个文件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) {
}
}
}
}结果是两个文件:
username.txt
foo
blarg
blahongapassword.txt
bar
bletch
baarhttps://stackoverflow.com/questions/39436608
复制相似问题