[英]Java: How can I split multiple sentences in a txt file to an array and then search for individual words in the array?
我在這里停留在一個項目上,我們得到了一個包含多個句子的txt文件,其中包含用戶名,哈希密碼和用戶角色。 我設法使用split函數將句子拆分為單個數組,但是現在我迷失了如何在每個句子中搜索匹配的用戶名/密碼。 任何幫助都非常感謝。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.MessageDigest;
import java.util.Scanner;
public class Execute {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Scanner scnr = new Scanner(System.in);
FileInputStream fileByteStream = null;
String username = "";
String password = "";
Authentication user = new Authentication();
// Ask for user input
System.out.println("Enter your username");
username = scnr.nextLine();
System.out.println("Enter your password");
password = scnr.nextLine();
// Convert the password string to an MD5 hash
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
// Set the username and password to what the user entered
user.setUsername(username);
user.setPassword(sb.toString());
// open the credentials file
try {
fileByteStream = new FileInputStream("src/credentials.txt");
} catch (FileNotFoundException ex) {
System.out.println("File not found");
}
Scanner inFS = new Scanner(fileByteStream);
// inFS.useDelimiter("");
int lineNumber = 1;
while (inFS.hasNextLine()) {
String fileLine = inFS.nextLine(); // declares string "fileLine" to
// the next line in the file
String[] fileLineArray = fileLine.split("; "); // turns fileLine into
// an array name
// fileLineArray
lineNumber++; // increase line number by 1 each progression
if (fileLineArray[0].matches(user.getUsername())) {
System.out.println("Success");
} else {
System.out.println("Failed");
}
}
}
}
我不知道是否有幫助,但這是另一個課程。
public class Authentication {
private String username;
private String password;
public Authentication(){
setUsername("");
setPassword("");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
這也是憑證文件
griffin.keyes 108de81c31bf9c622f76876b74e9285f zookeeper;
rosario.dawson 3e34baa4ee2ff767af8c120a496742b5 admin;
bernie.gorilla a584efafa8f9ea7fe5cf18442f32b07b veterinarian;
donald.monkey 17b1b7d8a706696ed220bc414f729ad3 zookeeper;
jerome.grizzlybear 3adea92111e6307f8f2aae4721e77900 veterinarian;
bruce.grizzlybear 0d107d09f5bbe40cade3de5c71e9e9b7 admin;
if (fileLineArray[0].contains(user.getUsername())) {
System.out.println("Success");
} else {
System.out.println("Failed");
}
嘗試使用包含,因為
fileLineArray [0]是griffin.keyes 108de81c31bf9c622f76876b74e9285f zookeeper
或嘗試進行不同的拆分並在文件中按行搜索
采用
readLine()
並在文件中使用令牌進行更好的拆分
griffin.keyes-108de81c31bf9c622f76876b74e9285f-zookeeper
String[] fileLineArray = fileLine.split("-");
fileLineArray[0] will be griffin.keyes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.