JAVA:在文件中搜索字符串/关键字,并在ArrayList中添加由搜索到的字符串/关键字组成的行

时间:2014-12-04 04:34:06

标签: java eclipse

我在这里:

String Path = "C:\\Ian\\sample\\install.info"
List<String> BatchList = new ArrayList<String>();
List<String> installList = new ArrayList<String>();
List<String> uninstallList = new ArrayList<String>();
Scanner scanner = new Scanner(Path);
String myLine;
int i  = 0;
while((myLine = scanner.next()) != null)
{
 if(myLine.indexOf("C:\\Batch") == 0 )
 {  
    BatchList.add(myLine);
 }
 if(myLine.indexOf("C:\\install") == 0 )
 {  
    installList.add(myLine);
 }
 if(myLine.indexOf("C:\\uninstall") == 0 )
 {  
    uninstallList.add(myLine);
 }
 i++;
}

以下是 install.info

的内容
[BatchFiles]
C:\Batch\Batch1.jar
C:\Batch\Batch2.jar
C:\Batch\Batch3.jar
C:\Batch\Batch4.jar

[INSTALL]
C:\install\sql1.sql
C:\install\sql2.sql
C:\install\sql3.sql
C:\install\sql4.sql

[UNINSTALL]
C:\uninstall\sql1.sql
C:\uninstall\sql2.sql
C:\uninstall\sql3.sql
C:\uninstall\sql4.sql

我是否有任何想法或方法可以获得由我的关键字组成的行并将其放入ArrayLists?

渴望输出

BatchList 应包含:

C:\install\Batch1.jar
C:\install\Batch2.jar
C:\install\Batch3.jar
C:\install\Batch4.jar

installList 应包含:

C:\install\sql1.sql
C:\install\sql2.sql
C:\install\sql3.sql
C:\install\sql4.sql

uninstallList 应包含::

C:\uninstall\sql1.sql
C:\uninstall\sql2.sql
C:\uninstall\sql3.sql
C:\uninstall\sql4.sql

= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -

更新

    List<String> batchList = new ArrayList<String>();
    List<String> installList = new ArrayList<String>();
    List<String> uninstallList = new ArrayList<String>();

    Scanner scanner = new Scanner(Path);
     String myLine;
     int i = 0;
     while (scanner != null && scanner.hasNext()) {
         if ((myLine = scanner.next()) != null) {
             if (myLine.indexOf("C:\\Batch") == 0) {
            batchList.add(myLine);
        }
        if (myLine.indexOf("C:\\install") == 0) {
            installList.add(myLine);
        }
        if (myLine.indexOf("C:\\uninstall") == 0) {
            uninstallList.add(myLine);
        }
        i++;
    }
}
if (scanner != null) {
    scanner.close(); // Closing scanner
}

String[] BatchPath = batchList.toArray(new String[0]);
String[] SQLinsPath = installList.toArray(new String[0]);
String[] SQLuninsPath = uninstallList.toArray(new String[0]);

for (String k : BatchPath)
{
    System.out.println(k);
}
for (String k : SQLinsPath)
{
    System.out.println(k);
}
for (String k : SQLuninsPath)
{
    System.out.println(k);
}

1 个答案:

答案 0 :(得分:0)

以下代码适合您。

import java.io.File;

public static void main(String[] args) {
    String path = "C:\\Ian\\sample\\install.info"; // Changed variable name from Path to path
    File file = new File(path);                    // Creating file object
    List<String> batchList = new ArrayList<String>(); // Changed variable name from BatchList to batchList
    List<String> installList = new ArrayList<String>();
    List<String> uninstallList = new ArrayList<String>();
    Scanner scanner = null;
    try {
        scanner = new Scanner(file);               // Reading file object
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    String myLine;
    int i = 0;
    while (scanner != null && scanner.hasNext()) {
        if ((myLine = scanner.next()) != null) {
            if (myLine.indexOf("C:\\Batch") == 0) {
                batchList.add(myLine);
            }
            if (myLine.indexOf("C:\\install") == 0) {
                installList.add(myLine);
            }
            if (myLine.indexOf("C:\\uninstall") == 0) {
                uninstallList.add(myLine);
            }
            i++;
        }
    }
    if (scanner != null) {
        scanner.close(); // Closing scanner
    }
    // System.out.println(batchList);
    // System.out.println(installList);
    // System.out.println(uninstallList);

    String[] BatchPath = batchList.toArray(new String[0]);
    String[] SQLinsPath = installList.toArray(new String[0]);
    String[] SQLuninsPath = uninstallList.toArray(new String[0]);

    System.out.println("Batch List : ");
    for (String k : BatchPath) {
        System.out.println(k);
    }
    System.out.println();

    System.out.println("Install List : ");
    for (String k : SQLinsPath) {
        System.out.println(k);
    }
    System.out.println();

    System.out.println("Uninstall List : ");
    for (String k : SQLuninsPath) {
        System.out.println(k);
    }
}

<强>输出:

Batch List : 
C:\Batch\Batch1.jar
C:\Batch\Batch2.jar
C:\Batch\Batch3.jar
C:\Batch\Batch4.jar

Install List : 
C:\install\sql1.sql
C:\install\sql2.sql
C:\install\sql3.sql
C:\install\sql4.sql

Uninstall List : 
C:\uninstall\sql1.sql
C:\uninstall\sql2.sql
C:\uninstall\sql3.sql
C:\uninstall\sql4.sql