Jump to content
Sign in to follow this  

Java program to search for a string array in a file

Recommended Posts

Hello all,


This is a program I wrote in java to check the client files for for gem descriptions.


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class StringArraySearch {
    public static void main(String[] args) throws IOException{
        //Creation of File Descriptor for input file
        File readFile = new File("src/stonehint.txt");

        //Intialize the word Array
        String[] fileWords=null;

        //Creation of File Reader object
        FileReader fr = new FileReader(readFile);

        //Creation of BufferedReader object
        BufferedReader br = new BufferedReader(fr);
        String stringCheck;
        String[] input={
                "ItemHint_LieYanS", "ItemHint_ZhiYanS", "ItemHint_HuoYaoS", "ItemHint_MaNaoS", "ItemHint_HanYu",
                "ItemHint_YueZhiX", "ItemHint_ShuiLingS", "ItemHint_ShengGuangS", "ItemHint_FengLingS", "ItemHint_YingYanS",
                "ItemHint_YanVitS", "ItemHint_YanStrS", "ItemHint_LongZhiTong", "ItemHint_LongZhiHun", "ItemHint_LongZhiXin",
                "ItemHint_GaNaZhiShen", "ItemHint_HuangYu", "ItemHint_ChiYu", "ItemHint_QingYu", "ItemHint_XTLingGuang",
                "ItemHint_LKBiZhong", "ItemHint_BBDuoShan", "ItemHint_FFDiYu", "ItemHint_XKQiangHua", "ItemHint_SShuiyao",
                "ItemHint_SSbusi", "ItemHint_SSguangmang", "ItemHint_SSningju", "ItemHint_SSxuanwu", "ItemHint_HLS",
                "ItemHint_HYS", "ItemHint_HJS", "ItemHint_BLS", "ItemHint_BYS", "ItemHint_BJS", "ItemHint_CLS",
                "ItemHint_CYS", "ItemHint_CJS", "ItemHint_ZLS", "ItemHint_ZYS", "ItemHint_ZJS", "ItemHint_QLS",
                "ItemHint_QYS", "ItemHint_QJS", "ItemHint_GGR", "ItemHint_GGS", "ItemHint_GGOS", "ItemHint_GGC", "ItemHint_GGW",
                "ItemHint_JinYanS", "ItemHint_MuYanS", "ItemHint_ShuiYanS", "ItemHint_HuoYanS", "ItemHint_TuYanS"

        //Variable to check increments
        int count=0;

        //Reading Content from the file
        while((stringCheck = br.readLine())!=null){
            //Split the word using space
            fileWords = stringCheck.split(" ");
            for (String word : fileWords){
                for (int i = 0; i < input.length; i++){
                    //Search for the given word
                    if (word.equals(input[i])){
                        System.out.println(input[i] + " is present in the file");
        System.out.println("There are "+count+" gem descriptions in the file");
    }//close main method

How useful is it?


Not very lol.

  • Like 1

Share this post

Link to post
Share on other sites

What exactly are you trying to do?

Looks like a standard search for a keyword, you could possibly try using Regexp for a performance improvement if that's something you think needs improvement.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...