有个anguage.txt 里面记录着用户的帐号和密码 格式是: aaa 111
bbb 222
ccc 333如何才能把里面的帐号和密码 分别读出 然后在用户登入框里里进行匹配验证,成功的话则登入成功,不成功的话则跳出登入失败~! 希望大家能帮帮忙 谢谢啦
bbb 222
ccc 333如何才能把里面的帐号和密码 分别读出 然后在用户登入框里里进行匹配验证,成功的话则登入成功,不成功的话则跳出登入失败~! 希望大家能帮帮忙 谢谢啦
props.load(new FileInputStream("filename.properties"));
String Id= props.getProperty("userId");
String Password= props.getProperty("passWord");
filename.properties格式为
userId=root;
password=111111;
如果LZ有多个用户的话,建议用DOM4J解决
public static void main(String[] args)
{
File file = new File("d:/anguage.txt");
if (file.exists()==false){
System.out.println("cannot find file");
return;
}
try
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String lineText;
lineText = br.readLine();
while(lineText != null){
printUserAndPwd(lineText);
lineText = br.readLine();
}
} catch ( FileNotFoundException e )
{
e.printStackTrace();
} catch ( IOException e )
{
e.printStackTrace();
}
}
public static void printUserAndPwd(String text){
if (text == null)
return;
if ("".equals(text))
return;
String[] idAndPwd = text.split(" ");
if (idAndPwd == null || idAndPwd.length != 2)
{
System.out.println("文本格式错误");
return;
}
else{
System.out.print("UserId = " + idAndPwd[0]);
System.out.print("\t");
System.out.println("Password = " + idAndPwd[1]);
}
}
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class ReadTxt1 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("D:\\sql.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String s;
StringBuffer sb = new StringBuffer();
while((s = br.readLine())!=null){
sb.append(s + "\n");
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
当然把文件是.xml格式的,dom4j会非常方便的
public class ReadWithScanner
{
// PRIVATE //
private static File fFile;
//private boolean breakFlag = false; public ReadWithScanner(String aFileName)
{
fFile = new File(aFileName);
} public final void processLineByLine() throws FileNotFoundException
{
Scanner scanner = new Scanner(fFile);
if(fFile.exists())
{
log("exsit file");
}
try
{
while (scanner.hasNextLine())
{
processLine(scanner.nextLine());
}
} finally
{
// ensure the underlying stream is always closed
scanner.close();
}
}
protected void processLine(String aLine)
{
// use a second Scanner to parse the content of each line
Scanner scanner = new Scanner(aLine);
scanner.useDelimiter("=");
if(scanner.hasNext())
{
log("sadfasfasfasdfa");
try
{
String name = scanner.next();
String value = scanner.next();
log("Name is : " + quote(name.trim()) + ", and Value is : "
+ quote(value.trim())); }
catch(NoSuchElementException nsee)
{
log("This line contains not this sign");
}
}
// else
// {
// log("Empty or invalid line. Unable to process.");
// }
// (no need for finally here, since String is source)
scanner.close();
}
private static void log(Object aObject)
{
System.out.println(String.valueOf(aObject));
}
public static void main(String... aArgs)
{
ReadWithScanner parser = new ReadWithScanner("C:\\test\\test.txt");
try
{
FileUtil.getFileNumber(fFile);
}
catch (UtilException e1)
{ e1.printStackTrace();
}
try
{
parser.processLineByLine();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
log("Done.");
}}
杂不用xml
那个多好啊
你想怎么搞就怎么搞
何乐而不讳啊