gpt4 book ai didi

java - 在Android中比较txt文件和输入字符串

转载 作者:行者123 更新时间:2023-12-01 13:50:18 26 4
gpt4 key购买 nike

当我在编辑文本中输入字符串并单击按钮时,我创建了一个 txt 文件并将其保存在位于 data/data/com.xxx/files/mynote/txt 的文件资源管理器中。

我想比较txt文件内容和输入字符串。在我的 txt 文件中,仅存储最小长度 5 到 6。我该怎么做呢?我已经尝试过,但它无法正确比较 - 当我输入与我保存在 txt 文件中的相同字符串(例如“tazin”)时,它会转到其他 block 部分。

这是我的代码。

  btnSubmitCode=(Button)findViewById(R.id.button_Submit);
btnSubmitCode.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

if(editText_Code.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(),"Please Enter Code", Toast.LENGTH_SHORT).show();
}
else
{
String strCode = editText_Code.getText().toString().trim();
create_File(strCode);
readFile();
}

String temp;
String searchString = "tazin";

try {
File file=new File("/data/data/com.xxxxx/files/mynote.txt/");

BufferedReader in = new BufferedReader(new FileReader(file));
while (in.readLine() != null)
{
temp = in.readLine();
System.out.println("String from txt file ="+temp);
if(searchString.equals(temp))
{
System.out.println("word is match");
in.close();
return;
}
else
{
System.out.println("word is not match");
}
}
in.close();

}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});




//Write File
private void create_File(String strtext)
{
FileOutputStream fos = null;
try
{
fos = openFileOutput("mynote.txt", MODE_PRIVATE);
fos.write(strtext.getBytes());
Toast.makeText(getApplicationContext(), "File created succesfully",
Toast.LENGTH_SHORT).show();

}
catch(FileNotFoundException fexception)
{
fexception.printStackTrace();

}
catch(IOException ioexception)
{
ioexception.printStackTrace();
}

finally
{
if (fos != null)
{
try
{
// drain the stream
fos.flush();
fos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


//Read File

private void readFile()
{


FileInputStream fis;

try
{
fis = openFileInput("mynote.txt");
byte[] reader = new byte[fis.available()];
while (fis.read(reader) != -1)
{

}


textView_ReadCode.setText(new String(reader));
strReadFile = new String(reader);
System.out.println("Read File Into String Format " + strReadFile);


Toast.makeText(getApplicationContext(), "File read succesfully",
Toast.LENGTH_SHORT).show();

if (fis != null)
{
fis.close();
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
Log.e("Read File", e.getLocalizedMessage());
}

}

最佳答案

您滥用了 BufferedReader.readLine() 方法。您每次通过 while 循环调用它两次;一次在 while 条件中,一次在循环 block 本身中。因此,当您执行 temp = in.readLine() 然后执行 searchString.equals(temp) 比较时,您已经从文本文件中跳过了一行。像这样构造你的循环:

temp = in.readLine();
while (temp != null)
{
System.out.println("String from txt file =" + temp);

if(searchString.equals(temp))
{
System.out.println("word is match");
in.close();
return;
}
else
{
System.out.println("word is not match");
}

temp = in.readLine();
}

编辑:

根据我们在评论中的聊天,这里是您需要的更新代码:

btnSubmitCode.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
String str = editText_Code.getText().toString();

if(str == null || str.equals(""))
{
Toast.makeText(getApplicationContext(), "Please Enter Code", Toast.LENGTH_SHORT).show();
}
else
{
String strCode = str.trim();
if(checkCode(strCode))
{
System.out.println("word is match");
}
else
{
System.out.println("word is not match");
}
}
}
}
);
...
...

// Returns true if the code has already been used
// Returns false if the code is new
// Creates mynote.txt if it doesn't exist
// Appends code if it is new
private boolean checkCode(String code)
{
String temp;
File file = new File(getFilesDir() + "mynote.txt");

if(file.exists())
{
try
{
BufferedReader in = new BufferedReader(new FileReader(file));
temp = in.readLine();
while (temp != null)
{
if(code.equals(temp))
{
// Match was found
// Clean up and return true
in.close();
return true;
}

temp = in.readLine();
}
in.close();
}
catch (FileNotFoundException e)
{

}
catch (IOException e)
{

}

}

// Match was not found or File doesn't exist
// Append code to mynote.txt and return false
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
out.newLine();
out.write(code);
out.close();
}
catch (IOException e)
{

}

return false;
}

关于java - 在Android中比较txt文件和输入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20015541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com