-2

Here is my use case , I'm reading my input file file.in as an object then converting it as a String

0000001HL26110650059147    TEST- TEST                                                                                                               
0000002HL34110FON202212217835294783529                       20221221123000                 650059147                                                                                                                                
0000003HL34120FON20221221783529BLE50000395     13626142                                                                         
0000004HL34120FON20221221783529BLE50000395     13626143                                                                         

Here is my positions (0-7 : it's a sequence ) , (11-14 : it's my section) and the important one is (47-67 : it's the Oldvalue that i want to change in one condition if section is equals to 120)

For that ,i have to take the Oldvalue from postion 47-67 and check in database using simple query

select NewValue from myTable where Oldvalue = 'Oldvalue'

Then put the NewValue in the place of the Oldvalue in my String

So my output would be :

0000001HL26110650059147    TEST- TEST                                                                                                               
0000002HL34110FON202212217835294783529                       20221221123000                 650059147                                                                                                                                
0000003HL34120FON20221221783529BLE50000395     NewValue1  
0000004HL34120FON20221221783529BLE50000395     NewValue2 

Note that the other sections will remain unchanged just the section equals 120 and there is multiple lines where section equals 120

Any idea how to do this please

Here is my attemp but could not continue to find solution ,

 try {
        File file = new File("C:/Users/20230309.in");
        Scanner scanner = new Scanner(file);
        List<String> lines = new ArrayList<>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            lines.add(line);
        }
        scanner.close();
        System.out.println(lines);
           
for (String element : lines) {


 if (element.length()>11 && "120".equals(element.trim().substring(11, 14))) 
{
 String oldValue = element.substring(47, 67);  

}
}       
    
    } catch (FileNotFoundException e) {
        System.out.println("File not found.");
        e.printStackTrace();
    }
    


   
Java BEE
  • 67
  • 4
  • 1
    You didn't state the problem you're facing. Reading from a file and substring operations are trivial matters, and there are thousands of tutorials for both. What have you tried so far? – Abhijit Sarkar Mar 13 '23 at 10:01

1 Answers1

0

Read the file in to list of strings. For each row (entry in the list of strings) check if this row (the string) is to be changed. If it is then replace the old value in this string with new value with String.replace(oldValue, newValue). Rewrite the whole file based on your now modified list of strings.

This assumes that the file is small enough to be read into memory and that no two processes or threads accesses the file at the same time. Otherwise you will have race conditions and you would have to implement some kind of locking.

Tarmo
  • 3,851
  • 2
  • 24
  • 41
  • There's no need to read the whole file at once, line-by-line would work just fine for OP. – Abhijit Sarkar Mar 13 '23 at 10:04
  • do you have some code samples on how to iterate each line , do i have do split the rows using '\n' ?? how to do that – Java BEE Mar 13 '23 at 10:13
  • @AbhijitSarkar , how could i read the file line by line then change the old values with the new ones from DB ? – Java BEE Mar 13 '23 at 10:14
  • @AbhijitSarkar this is true, but it might not be reasonable to read and edit the same file at the same time. That's why I suggested to read the whole file at once. Of course on could write to a different file and at the end delete the old file and replace it with then new one. Different ways, same end result, each with it's pros and cons. As for how to read a file in Java line by line: https://stackoverflow.com/questions/5868369/how-can-i-read-a-large-text-file-line-by-line-using-java – Tarmo Mar 13 '23 at 10:44
  • @Tarmo, i have updated my question but could not continue your suggestion , could you help to make it work as expected please ? – Java BEE Mar 13 '23 at 11:05
  • i'm getting the old Values see my code in question , how could i proceed to to do the rest of the staff – Java BEE Mar 13 '23 at 11:14
  • Replace a part of a string like this https://stackoverflow.com/questions/16702357/how-to-replace-a-substring-of-a-string and write back to a file like this https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it – Tarmo Mar 13 '23 at 11:16