当前位置: 技术问答>java相关
很急,请各位帮忙想想这个程序该怎么写,万分感谢!
来源: 互联网 发布时间:2015-09-02
本文导语: 有这样一个文件login.txt如下: #name password liu liu123 li li123 wang wang12 ... ... 我想读出这个文件的第一行是姓名这个字符串,跟屏幕上已输入的名字去比较,如果不一...
有这样一个文件login.txt如下:
#name password
liu liu123
li li123
wang wang12
... ...
我想读出这个文件的第一行是姓名这个字符串,跟屏幕上已输入的名字去比较,如果不一样,读下一行的名字,也就是去遍历,整个文件都找不到,也报错就行了;如果名字一样,再读出这行名字后面的密码,再和从屏幕上输入的密码去比较,一样则ok,不一样则报错。
我是java新手,请大家帮忙!
#name password
liu liu123
li li123
wang wang12
... ...
我想读出这个文件的第一行是姓名这个字符串,跟屏幕上已输入的名字去比较,如果不一样,读下一行的名字,也就是去遍历,整个文件都找不到,也报错就行了;如果名字一样,再读出这行名字后面的密码,再和从屏幕上输入的密码去比较,一样则ok,不一样则报错。
我是java新手,请大家帮忙!
|
import java.io.*;
public class test
{
public static void main(String[] args) throws IOException
{
System.out.print("Please input your name : ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String name = br.readLine();
FileReader filereader = new FileReader("log.txt");
BufferedReader linereader = new BufferedReader(filereader);
linereader.readLine();
while ( true )
{
String str = linereader.readLine();
if ( str == null )
{
System.out.println("The name doesn't exist");
break;
}
if ( (name + " ").equals(str.substring( 0 , (name.length() + 1) ) ) )
{
System.out.print("Please input your password : ");
String password = br.readLine();
if ( password.equals(str.substring(11)) )
{
System.out.println("Name and password are correct");
break;
}
else
{
System.out.println("Password is incorrect");
break;
}
}
}
}
}
注 :把你的log.txt和这个test.java文件放在同一个目录下
public class test
{
public static void main(String[] args) throws IOException
{
System.out.print("Please input your name : ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String name = br.readLine();
FileReader filereader = new FileReader("log.txt");
BufferedReader linereader = new BufferedReader(filereader);
linereader.readLine();
while ( true )
{
String str = linereader.readLine();
if ( str == null )
{
System.out.println("The name doesn't exist");
break;
}
if ( (name + " ").equals(str.substring( 0 , (name.length() + 1) ) ) )
{
System.out.print("Please input your password : ");
String password = br.readLine();
if ( password.equals(str.substring(11)) )
{
System.out.println("Name and password are correct");
break;
}
else
{
System.out.println("Password is incorrect");
break;
}
}
}
}
}
注 :把你的log.txt和这个test.java文件放在同一个目录下
|
import java.io.*;
public class Pass
{
public static void main(String[] args){
try{
boolean isRight = false;
FileReader fr = new FileReader("login.txt");
BufferedReader br = new BufferedReader(fr);
String name = "wang"; //用输入的姓名来替换
String password = "wang12"; //用输入的密码来替换
String s = "";
while((s = br.readLine()) != null){
int f = s.indexOf(" ");
int l = s.lastIndexOf(" ");
String n = s.substring(0,f);
String p = s.substring(l + 1);
if(n.equals(name))
if(p.equals(password)){
System.out.println("right user");
isRight = true;
break;
}
}
if(!isRight) System.out.println("wrong user");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public class Pass
{
public static void main(String[] args){
try{
boolean isRight = false;
FileReader fr = new FileReader("login.txt");
BufferedReader br = new BufferedReader(fr);
String name = "wang"; //用输入的姓名来替换
String password = "wang12"; //用输入的密码来替换
String s = "";
while((s = br.readLine()) != null){
int f = s.indexOf(" ");
int l = s.lastIndexOf(" ");
String n = s.substring(0,f);
String p = s.substring(l + 1);
if(n.equals(name))
if(p.equals(password)){
System.out.println("right user");
isRight = true;
break;
}
}
if(!isRight) System.out.println("wrong user");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
|
public ReadFile
{
public static void main(String arg[])
{
final int fsize =2550;
byte buf[] = new byte[fsize];
try
{
FileInputStream filein = new FileInputStream(new File(filename));
int d = filein.read(buf,0,fsize);
try{
if( d==-1)
return;
String s = new String(buf,0,d);
StringTokenizer st = new StringTokenizer(s,tokenformat);// tokenformat ","," ","-"...
while(st.hasMoreToken())
{
String ss = st.nextToken();
if (ss.indexOf("password")>=0))
// this is the password field
...
}
}catch(Exception e){}
}
{
public static void main(String arg[])
{
final int fsize =2550;
byte buf[] = new byte[fsize];
try
{
FileInputStream filein = new FileInputStream(new File(filename));
int d = filein.read(buf,0,fsize);
try{
if( d==-1)
return;
String s = new String(buf,0,d);
StringTokenizer st = new StringTokenizer(s,tokenformat);// tokenformat ","," ","-"...
while(st.hasMoreToken())
{
String ss = st.nextToken();
if (ss.indexOf("password")>=0))
// this is the password field
...
}
}catch(Exception e){}
}
|
import java.io.FileInputStream;
import java.io.PrintStream;
import java.util.Properties;
public class GFConfig
{
public GFConfig()
{
}
public static String getProperty(String s)
{
return getProperty(s, null);
}
public static String getProperty(String s, String s1)
{
return getProperty(s, s1, null);
}
public static String getProperty(String s, String s1, String s2)
{
if(config == null)
readConfig();
if(config != null)
{
if(s1 == null)
return config.getProperty(s, s2);
else
return config.getProperty(s + "." + s1, s2);
} else
{
return s2;
}
}
private static void readConfig()
{
if(config != null)
return;
try
{
String s = System.getProperty("file.separator") + "test" + System.getProperty("file.separator") + "jar" + System.getProperty("file.separator") + "config.properties";
FileInputStream fileinputstream = new FileInputStream(s);
if(fileinputstream != null)
{
config = new Properties();
config.load(fileinputstream);
} else
{
System.out.println("The file is not open !n");
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
private static Properties config = null;
}
把这个文件打包,可以通过GFConfig.getProperty()读出name值,具体怎么控制,你可以在这个文件和jsp里边在操作。
import java.io.PrintStream;
import java.util.Properties;
public class GFConfig
{
public GFConfig()
{
}
public static String getProperty(String s)
{
return getProperty(s, null);
}
public static String getProperty(String s, String s1)
{
return getProperty(s, s1, null);
}
public static String getProperty(String s, String s1, String s2)
{
if(config == null)
readConfig();
if(config != null)
{
if(s1 == null)
return config.getProperty(s, s2);
else
return config.getProperty(s + "." + s1, s2);
} else
{
return s2;
}
}
private static void readConfig()
{
if(config != null)
return;
try
{
String s = System.getProperty("file.separator") + "test" + System.getProperty("file.separator") + "jar" + System.getProperty("file.separator") + "config.properties";
FileInputStream fileinputstream = new FileInputStream(s);
if(fileinputstream != null)
{
config = new Properties();
config.load(fileinputstream);
} else
{
System.out.println("The file is not open !n");
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
private static Properties config = null;
}
把这个文件打包,可以通过GFConfig.getProperty()读出name值,具体怎么控制,你可以在这个文件和jsp里边在操作。