Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


How to write in existing file from list using Java by removing duplicate data?


I want to write in file that exists. My data is in the form of java list. Here is a sample of data :

snmp,192.168.20.1,cloud,
snmp,192.168.20.2,cloud,
I want to add line snmp,192.168.20.1,cloud123 in the file. It should update existing file content i.e.(snmp,192.168.20.1,cloud) by new contents given.

And if provided contents different from contents of file then append it to file?

Here is my workaround---

String tempFile = RunMTNew.instdir + "/var/";
File tempFileName = new File(tempFile+"hosts.tempFile");
try{
if(!tempFileName.exists()) {
tempFileName.createNewFile();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}
catch ( IOException ioe){
ioe.printStackTrace();
System.out.println("Exception occured while creating temp file");
}
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
PrintWriter tempoutfile= null;
 while((line = bufferedReader.readLine()) != null) {
     System.out.println("line before if "+ line);
     if(line!=null || (line = line.trim()) != "" ){
     System.out.println("line at start of while" + line);
     String[] lineFromFile = line.split(",");
 try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)));
    ListIterator atwlist = arraytowrite.listIterator();
    String lineToWriteInFile = "";

while (atwlist.hasNext()) {
ArrayList atwlistline = (ArrayList) atwlist.next();
 System.out.println("array" + atwlistline);
 String lineToAdd = atwlistline.toString();
 lineToWriteIfNotFound = lineToAdd;
 System.out.println("After converting to string line is" + lineToAdd);
 System.out.println("lineFromFile contents are "+ lineFromFile[1]);
 if(lineToAdd.contains(lineFromFile[1])){
 lineToWriteInFile = lineToAdd;
 }
 else{
 lineToWriteInFile = line;
 }
}
 try{
 tempoutfile = new PrintWriter(new BufferedWriter(new FileWriter(tempFileName, true)));
 System.out.println("writing in file" +lineToWriteInFile);
tempoutfile.write(lineToWriteInFile);
tempoutfile.write("\n");
 }catch(IOException ioe){
     ioe.printStackTrace();
 System.out.println("Exception occured while writing in tempFile");
 }

tempoutfile.close();
} catch (IOException e) {
e.printStackTrace();
    System.out.println("Exception occured in outer try block");
}
 }//end of if
 }// end of while

 try{
 FileReader tempfileReader = new FileReader(tempFile+"hosts.tempFile");

 BufferedReader tempBufferedReader = new BufferedReader(tempfileReader);
 FileWriter fosFinal = new FileWriter(filename);
 PrintWriter outFinal = new PrintWriter(fosFinal);
 while((line = tempBufferedReader.readLine()) != null) {
     System.out.println("line from tempfile to write in main " + line);
    outFinal.write(line);
 }
 }catch(IOException ie){
     ie.printStackTrace();
     System.out.println("Exception occured while reading from temp and write into main file");
 }

asked Jun 18, 2015 in Core java by rajesh
0 votes
100 views



Related Hot Questions



Government Jobs Opening


...