Why its not working with simple string? i know we can use either list or map or other such type but what i am missing with the string?
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
public class Solution {
public static void main(String[] args) throws IOException,FileNotFoundException{
String line = "";
String newString = "";
String oldContent = "";
String oldString = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String fileName = br.readLine();
File originalFile = new File(fileName);
br.close();
FileInputStream is = new FileInputStream(fileName);
//ArrayList<String> list = new ArrayList<>();
br = new BufferedReader(new InputStreamReader(is));
if(args.length != 0 ){
if(args[0].equals("-u")){
line = br.readLine();
while (line != null) {
//String[] lineArray = line.split(" ");
if((line.substring(0,8).trim()).equals(args[1])){
oldString = line;
//}else{
oldContent = oldContent + line + System.lineSeparator();
}
line = br.readLine();
}
StringBuilder sb = new StringBuilder();
//sb.append(String.format("%-8s",args[1]));
sb.append(String.format("%-8s%-30s%-8s%-4s",args[1],args[2],args[3],args[4]));
sb.append("\n");
newString = sb.toString();
newString = oldContent.replace(oldString,newString);
//FileWriter writer = new FileWriter(fileName);
FileOutputStream fileOut = new FileOutputStream(fileName);
fileOut.write(newString.getBytes());
fileOut.close();
//is.close();
br.close();
}
}else if(args[0].equals("-d")){
String s = "";
String stringAfterDeletion = "";
FileOutputStream os = new FileOutputStream(fileName);
if ((s = br.readLine()) != null) {
//String[] lineArray = s.split(" ");
if(!(s.substring(0,8).trim()).equals(args[1])){
//bw.write(s + System.getProperty("line.separator"));
stringAfterDeletion = stringAfterDeletion + s + System.lineSeparator();
}
}
os.write(stringAfterDeletion.getBytes());
os.close();
is.close();
br.close();
}
}
}