我正在阅读文件:
name1 wordx wordy passw1
name2 wordx wordy passw2
name3 wordx wordy passw3
name(i)wordx wordy PASSW(i)
x
x字
x字
也就是说
X
词语
目前我可以逐行打印:
第1行:name1 wordx wordy passw1
第2行:name2 wordx wordy passw2
我计划访问:
users [0] = name1
用户[1] = name2
用户[2] = name3
..
passws [0] = passw1
passws [1] = passw2
passws [2] = passw3
..
我的代码是:
public static void main(String args[]) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("C:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (inFile.hasNextLine()) {
list.add(inFile.nextLine()+",");
}
String listString = "";
for (String s : list) {
listString += s + "\t";
}
String[] parts = listString.split(",");
System.out.println("Line1: "+ parts[0]);
}
如何获得以下输出:
用户是name1,密码是passw1
用户名为32,密码为passw32
提前致谢。
答案 0 :(得分:0)
这样的事情会:
public static void main(String args[]) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = null;
try {
inFile = new Scanner(new File("C:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (inFile.hasNextLine()) {
list.add(inFile.nextLine());
}
int line = 0;
String[] parts = list.get(line).split(" ");
String username = parts[0];
String pass = parts[3];
System.out.println("Line" + (line + 1) + ": " + "User is " + username +" and password is " + pass);
}
编辑:如果你想迭代所有行,只需将最后一行放在一个循环中:
for (int line = 0; line < list.size(); line++) {
String[] parts = list.get(line).split(" ");
String username = parts[0];
String pass = parts[3];
System.out.println("Line" + (line + 1) + ": " + "User is " + username +" and password is " + pass);
}
答案 1 :(得分:0)
首先要做的是,将此循环添加到代码的末尾:
for(int i = 0; i <= parts.length(); i++){
System.out.println("parts["+i+"] :" + parts[i] );
}
只会使用,
显示拆分结果。
然后调整您的代码,您可能希望使用另一个正则表达式来split()
您的行,例如空格。
String[] parts = listString.split(" ");
有关split()
方法检查this。
答案 2 :(得分:0)
我建议您使用java.util.Map
标准API,该标准API允许您通过密钥存储对象并读取每个对象。 (在您的情况下,字符串对象由字符串键索引)。例如:
让我们假设这个空地图:
Map<String, String> map=new HashMap<String,String>();
如果你存储:
map.put("month", "january");
map.put("day", "sunday");
您可以预期map.get("month")
将返回“january”,map.get("day")
将返回“sunday”,map.get(any-other-string)
将返回null
。
回到你的案例:首先,你必须创建并填充地图:
private Map<String, String> toMap(Scanner scanner)
{
Map<String, String> map=new HashMap<String, String>();
while (scanner.hasNextLine())
{
String line=scanner.nextLine();
String[] parts=line.split(" ");
// Validation: Process only lines with 4 tokens or more:
if (parts.length>=4)
{
map.put(parts[0], parts[parts.length-1]);
}
}
return map;
}
然后,阅读地图:
private void listMap(Map<String,String> map)
{
for (String name : map.keySet())
{
String pass=map.get(name);
System.out.println(...);
}
}
您必须在班级中同时包含这两项内容,并使用main
方法调用它们。
答案 3 :(得分:0)
如果你想得到那个输出,那么这应该可以解决问题:
public static void main(String args[]) throws FileNotFoundException, IOException {
Scanner inFile = null;
try {
inFile = new Scanner(new File("F:\\file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Map<String, String> userAndPassMap = new LinkedHashMap<>();
while (inFile.hasNextLine()) {
String nextLine = inFile.nextLine();
String[] userAndPass = nextLine.split(" ");
userAndPassMap.put(userAndPass[0], userAndPass[1]);
}
for (Map.Entry<String, String> entry : userAndPassMap.entrySet()) {
System.out.println("User is:" + entry.getKey() + " and password is:" + entry.getValue());
}
}
通过在地图中存储,您可以直接将每个用户名与其密码相关联。如果需要将它们保存到单独的数组中,则可以在while循环中执行此操作:
List<String> users = new LinkedList<>(),passwords = new LinkedList<>();
while (inFile.hasNextLine()) {
String nextLine = inFile.nextLine();
String[] userAndPass = nextLine.split(" ");
users.add(userAndPass[0]);
passwords.add(userAndPass[1]);
}
然后将它们转换为数组
users.toArray()
答案 4 :(得分:0)
如果需要对读取行进行任意索引编制,请使用ArrayList:
首先,定义一个javabean用户:
public class User
{
private String name;
private String password;
// ... add full constructor, getters and setters.
}
然后,您必须创建并填充列表:
private ArrayList<User> toList(Scanner scanner)
{
List<User> list=new ArrayList<User>();
while (scanner.hasNextLine())
{
String line=scanner.nextLine();
String[] parts=line.split(" ");
// Validation: Process only lines with 4 tokens or more:
if (parts.length>=4)
{
list.add(new User(parts[0], parts[parts.length-1]));
}
}
return list;
}