如何比较整数和文件名?

时间:2016-12-02 04:44:02

标签: java file int equals

我正在尝试制作一个登录/密码程序,询问您是否有帐户,也可以制作帐户 按帐号我的意思是程序会给你一个随机的8位数字 我还有一个FileWriter,它根据你给出的ID创建一个文件。我有一个FileReader,它最终将读取您之前导出到文件中的内容,以便您可以更新它。

我遇到的问题是,当我询问用户他们是否已经拥有帐户时,如果他们说是,则会询问用户他们的用户ID。

我的计划是,当它读取你的UserID时,它会扫描保存我的.java文件的文件夹,并查找与你的UserID同名的.txt文件。例如,如果您创建一个帐户,并且它为您提供的UserID是12345678,它将创建一个名为12345678的文件,然后当您输入UserID时,它将扫描以查看该文件是否存在。

目前发生的问题是打印

  

找不到错误文件(我写的catch字符串)

即使我在文件夹中有该文件 我认为我在比较以查看UserID是否与任何文件名匹配时出现了问题。

"登录"类。

import java.awt.*;
import hsa.Console;
import java.util.Random;
import java.io.*;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class Login
{
static Console c;

static Login player1;

public static void main (String[] args)

{
    player1 = new Login ();
    player1.FileReaderTest (25756326);
    //player1.Userlogin (); //I think it has something to do with this
} // main method


public void Userlogin (File input)
{
    c = new Console ();

    Random rand = new Random ();

    c.println ("Hello do you have an account?");
    String Q1 = c.readLine ();
    Q1 = Q1.toUpperCase ();
    if (Q1.equals ("YES"))
    {
        c.println ("Please input your User ID");
        int login = c.readInt ();
        if (String.valueOf(login).equals (input))//I think it has something to do with this
        {
            try
            {
                FileReader reader = new FileReader (input);
                BufferedReader buf = new BufferedReader (reader);

                String line1 = buf.readLine ();
                String line2 = buf.readLine ();

                buf.close ();
                c.println (line1);
                c.println (line2);

            }
            catch (FileNotFoundException e)
            {
                c.println ("Error File Not Found");

            }
            catch (Exception e)
            {
                c.println ("ERROR");

            }


        }
    }

    else if (Q1.equals ("NO"))
    {
        c.println ("Please enter your name ");
        String name = c.readLine ();
        int UserID = rand.nextInt (99999999);
        c.println ("Your User ID is " + UserID);
        player1.FileCreation (UserID);
        player1.FileReaderTest (UserID);
    }

    while (!Q1.equals ("YES") && !Q1.equals ("NO")) //While Q1 != YES || NO
    {
        c.println ("Please Answer the question with Yes or No");
        c.println ("Hello do you have an account?");
        String Q2 = c.readLine ();
        Q2 = Q2.toUpperCase ();
        if (Q2.equals ("YES"))
        {
            c.println ("Ok lets start");
            break;
        }
        else if (Q2.equals ("NO"))
        {
            c.println ("Please enter your name ");
            String name = c.readLine ();
            int UserID = rand.nextInt (89999999) + 10000000;
            c.println ("Your User ID is " + UserID);
            player1.FileCreation (UserID);
            player1.FileReaderTest (UserID);
            break;
        }

    } //While Q1 != YES || NO



} //Public void Main


public void FileReaderTest (int UserID)
{
    File input = new File (String.valueOf (UserID));
    player1.Userlogin (input);

    try
    {
        FileReader reader = new FileReader (input);
        BufferedReader buf = new BufferedReader (reader);

        String line1 = buf.readLine ();
        String line2 = buf.readLine ();


        buf.close ();
        c.println (line1);
        c.println (line2);

    }
    catch (FileNotFoundException e)
    {
        c.println ("Error File Not Found");

    }
    catch (Exception e)
    {
        c.println ("ERROR");

    }




}


public void FileCreation (int UserID)
{
    try
    {
        Writer writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (String.valueOf (UserID)), "utf-8"));
    }
    catch (IOException ex)
    {
    }
}
} // Login class

3 个答案:

答案 0 :(得分:1)

So, you have a File input, and you want to compare the name of the file, so you instead want

String.valueOf(login).equals (input.getName());

If you are getting an error on

File input = new File (String.valueOf (UserID));

then note: "2.txt", for example, is very different than a file named just "2" (which is worth mentioning because Windows hides file extensions by default)

And if you aren't giving the full path to the file, then that file has to be in your "classpath", which if you don't understand, better to give the full path to the file.

答案 1 :(得分:0)

FileNotFound is not thrown after a comparison but by your FileReader when it is created. FileReader tells you that there is not file named as such. Since you said that you did create the file, there can be a few explanations:

  • Maybe the file you created is in the wrong folder. The default folder for a java program is the project folder. Make sure that you have your file in the right folder. Else you can give the full path as an argument for the FileReader: "/my/full/path/filename.extension".
  • It can also be a problem of extension. If you are using a Windows OS, the extension may be hidden in the file explorer. Right clic on your file and check whether there is such an extension (ex: ".txt")

Eventually you can open the file with a FileWriter under Java, and try to create the file within your program. You will see more easily which file Java is trying to access and it will help you identify your issue.

答案 2 :(得分:0)

Since you have mentioned that you need to create the text file associated with the UserId, I would suggest adding the ".txt" in the FileReaderTest class. Something similar to this :

File input = new File (String.valueOf (UserID)+".txt");

Or more conveniently

File input = new File (Integer.toString(UserID)+".txt");

I think this solves your query.