这就是我在做什么。请帮我从我的资源文件夹中访问我的文本文件。然后我正在从该文本文件中搜索输入的字符串????但我无法做到这一点......
AssetManager re = getResources().getAssets();
InputStream is = null;
try {
is = re.open("search.txt");
if(is != null)
new IconDownloader().execute("Test");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//---------
//read from file
File file = new File("search.txt");
try
{
// File creating
if(file.exists())
{
new IconDownloader().execute("Test");
System.out.println("file exist");
//new IconDownloader().execute("Test");
}
else
{
file.createNewFile();
}
String word = tx3; int val = 0;
//System.out.println("Enter the word to be searched for");
//Scanner input = new Scanner(System.in);
//word = input.next();
Scanner file1 = new Scanner(new File("search.txt"));
int v1=0;
while(file1.hasNextLine()){
if(word.equals(file1.nextLine().trim()))
{
v1=1;
// new IconDownloader().execute("Test");
// System.out.println("Word exist");
break;
}
}
if(v1 == 0)
System.out.println("Word does not exist");
}
catch(IOException e)
{
System.out.print(e);
e.printStackTrace();
}
//-------------------------
//您也可以将答案发送至shahan_butt22@ucp.edu.pk谢谢。
答案 0 :(得分:0)
您创建输入流来自资产管理器,但从不使用它。文件文件=新文件(" Search.txt"),它将在默认的app目录中查找文件!您还需要发布您拥有的实际错误以及IconDownloader.execute()的功能。此程序此时打开正确文件的输入流,在默认目录中创建一个txt文件,然后搜索新创建的文件! 尝试使用这段代码来阅读文本文件
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String read = "";
while((read = reader.readLine()) !=null)
{
//Do whatever you want to do to the line by checking string read
}