When you find the pathicular line, write it at the paticulat position in the text document

I am trying to do the following after finding this line Min:30

in a text document that I want to write after that line stop mon-fri

in a new text documnet, or if possible in the same text document, then delete the empty new line.

I appreciate any help.

Plain:

4
stop mon-fri
dominant 05:02 05:32 06:02 06:32 07:02 07:32 08:02 17:02 17:32 18:02 18:32 19:02 19:32
death Radbruch-Platz 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
operator 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
Min:30
apologized 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
cooperate 05:16 05:46 06:21 06:51 07:21 07:51 08:21 17:21 17:51 18:21 18:51 19:21 19:51
4
stop mon-fri
government computers 04:32 05:07 05:37 06:03 06:33 07:03 07:26 08:03 18:33 19:03 19:33
WASHINGTON 04:34 05:09 05:39 06:05 06:35 07:05 07:28 08:05 18:35 19:05 19:35
suspected 04:38 05:13 05:43 06:11 06:41 07:11 07:34 08:11 18:41 19:11 19:41
Min:15
Chinese 04:43 05:18 05:48 06:15 06:45 07:15 07:24 07:38 08:15 18:45 19:15 19:45
hackers  04:50 05:25 05:55 06:25 06:55 07:25 07:34 07:48 08:25 18:55 19:25 19:55

      

The result should look like this

4
stop mon-fri
Min:30
dominant 05:02 05:32 06:02 06:32 07:02 07:32 08:02 17:02 17:32 18:02 18:32 19:02 19:32
death Radbruch-Platz 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
operator 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
apologized 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
cooperate 05:16 05:46 06:21 06:51 07:21 07:51 08:21 17:21 17:51 18:21 18:51 19:21 19:51
4
stop mon-fri
Min:15
government computers 04:32 05:07 05:37 06:03 06:33 07:03 07:26 08:03 18:33 19:03 19:33
WASHINGTON 04:34 05:09 05:39 06:05 06:35 07:05 07:28 08:05 18:35 19:05 19:35
suspected 04:38 05:13 05:43 06:11 06:41 07:11 07:34 08:11 18:41 19:11 19:41
Chinese 04:43 05:18 05:48 06:15 06:45 07:15 07:24 07:38 08:15 18:45 19:15 19:45
hackers  04:50 05:25 05:55 06:25 06:55 07:25 07:34 07:48 08:25 18:55 19:25 19:55

      

Code:

private static void refill_time_table(String path, String key) {
    int five = 5;
    File file = new File(path + File.separator + key);

    StringBuilder sb = new StringBuilder(key);
    sb.deleteCharAt(5);
    String resultString = sb.toString();

    String str = Integer.toString(five);
    String newName = resultString.substring(0, 5) + str
            + resultString.substring(5, resultString.length());
    tempArray.add(newName);
    try (PrintWriter writer = new PrintWriter(path + File.separator
            + newName);

            Scanner scanner = new Scanner(file)) {
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (!line.startsWith("Min:")) {

                writer.println(line);
                //writer.flush();
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

      

+3


source to share


2 answers


Add all rows to the collection, keep track of the indices for these tokens and swap list items:

    List<String> l = new ArrayList<String>();

    try {

        Scanner sc = new Scanner(new File("D:\\temp\\dd.txt"));

        String TokenA = "stop mon-fri", TokenB = "Min";
        int indexA = 0, indexB = 0;

        while (sc.hasNextLine()) {
            String temp = sc.nextLine();
            l.add(temp);

            if (temp.equals(TokenA))
                indexA = l.size();
            else if (temp.contains(TokenB)) {
                indexB = l.size() - 1;

                while (indexB > indexA)
                    Collections.swap(l, indexB, --indexB);
            }
        }

        sc.close();

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

      



// Add code here - to print ArrayList as is file

+2


source


You should look into using it Queue

for string refactoring. Read line by line in Queue

. Stop when you find stop mon-fri

and search for the next one Min:

. Copy it and then delete it from the original to continue feeding lines to Queue

.



UPDATE: only the first case should be removed in the source Min:

. You should also add content Queue

to the new file every time you read . stop mon-fri

0


source







All Articles