java code

i have a java code that gives me a text file with filenames in this format x_someno.csv as output.I need to modify the code so that the text file contains only the x part and then compare this text file with a csv file in another folder that has x 's in the first column.If the two x's do not much then the x's for which there is no entry in the text file must be added to this text file.

here is the code for generating the text file.Please help me in updating this with the above requirement.
 
package Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
 
 
public class FileLister {
      public static int date;
      private String folder = "C:/Intel"; //The folder which needs to be operated. 
      long  duration=0;
      public static void main(String args[]){
         FileLister flst = new FileLister();   
          try{ 
    	  flst.duration = Integer.parseInt(args[0]); 
    	  //convert to millisec assuming input was in mins;
    	  flst.duration = flst.duration*1000*60;
          }catch(Exception e)
          {
        	System.out.println("invalid duration");
        	System.exit(0);
          }
                Calendar calendar = Calendar.getInstance();
            long curTime = calendar.getTimeInMillis();
            ArrayList<String> lst=flst.getFileList(curTime,flst.duration);
            System.out.println("This is the list of files:"+lst.toString());
            try 
            {
            File outputFile = new File("C://output.txt");
           	FileWriter fout = new FileWriter(outputFile);
            for (String s:lst)
            {
            fout.write(s);
            fout.write("\n");
            }
            fout.close();
            }
            catch (IOException e1) 
            {
            System.out.println("Error in getting create time");
            }
 
            
 
}
     public ArrayList<String> getFileList(long curTime, long duration)
     {
    ArrayList<String> lst=new ArrayList<String>();; 
    try{ File fol = new File(folder);
    if(fol.isDirectory())
    {
    File[] files=fol.listFiles();	
    for(File f:files)
    {
    	if(f.isFile()&&f.getName().startsWith("10"))
    	{
    	long lastmd = f.lastModified();
    	long diff = curTime-lastmd;
    	if(diff<=duration)
    		lst.add(f.getName());
       	}
    }
    
    }
    else{
    return null;	 
    }}
    catch(Exception e)
    {
    	return null;
    }
    
    return lst;	
       }

Open in new window

supportproAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

supportproAuthor Commented:
then for the x's in the above updated text file the csv file already present  must contain a new column and must be updated as "yes".
0
supportproAuthor Commented:
please help me to get this quickly.
0
a_bCommented:
the x seems to be hardcoded into the program as 10. Don't you take have to make this variable.
-- if(f.isFile()&&f.getName().startsWith("10"))

"I need to modify the code so that the text file contains only the x "
--make modifications to the list as follows - lst.add(x);

Let me know if this in the correct direction and then we can move on from there.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

a_bCommented:
The parsing for the other file is quite easy to manage using the StringTokenizer.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
supportproAuthor Commented:
actually that x part must definitely be a number like ip address.you please modify the code such that in the text file generated above  only the files with ipaddresses ie that x must be returned and then this x must be compared with x's in the csv file.
0
supportproAuthor Commented:
please modify the code so that the final output is the updated csv file with extra column.
0
a_bCommented:
Please read my question again, the "x" is hardcoded into the program. Is it supposed to be that way? Do i recieve the value of x as any input from somewhere?
0
anilallewarCommented:
Here it is...I have put the "x" as a variable that can be passed as command line argument 2
package com.anil.java.strings;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
 
public class FileLister {
	public static int date;
	private String folder = "C:/Intel"; // The folder which needs to be
										// operated.
	long duration = 0;
 
	public static void main(String args[]) {
		FileLister flst = new FileLister();
		try {
			flst.duration = Integer.parseInt(args[0]);
			// convert to millisec assuming input was in mins;
			flst.duration = flst.duration * 1000 * 60;
		} catch (Exception e) {
			System.out.println("invalid duration");
			System.exit(0);
		}
		Calendar calendar = Calendar.getInstance();
		long curTime = calendar.getTimeInMillis();
		ArrayList<String> lst = flst.getFileList(curTime, flst.duration, args[1]);
		System.out.println("This is the list of files:" + lst.toString());
		try {
			File outputFile = new File("C://output.txt");
			FileWriter fout = new FileWriter(outputFile);
			String[] list = null;
			for (String s : lst) {
				list=s.split("_");
				fout.write(list[0]);
				fout.write("\n");
			}
			fout.close();
		} catch (IOException e1) {
			System.out.println("Error in getting create time");
		}
		
		ArrayList<String> data = new ArrayList<String>();
		
		//Read the output excel and compare against the file
		try{
			File file = new File("C://output.txt");
			BufferedReader fin = new BufferedReader(new FileReader(file));
			String s;
            while((s = fin.readLine())!=null){
                System.out.println(s);
                data.add(s);
            }
            System.out.println(data);
            //fin=null;
            file = new File("C://Intel/output.csv");
            fin = new BufferedReader(new FileReader(file));
            BufferedWriter fout = new BufferedWriter(new FileWriter(new File("C://output1.csv")));
            //fout.write("Checking");
            String[] list=null;
            while((s = fin.readLine())!=null){
                System.out.println(s);
                list=s.split(",");
                if (!data.contains(list[0])){
                	System.out.println("Not present: " + list[0]);
                	fout.write(list[0] + "," + "Yes");
                	fout.write("\n");
                }else{
                	System.out.println("Present: " + list[0]);
                	fout.write(list[0] + "," + "No");
                	fout.write("\n");
                }
            }
            fout.close();
            
         }catch(IOException ie){
            	System.out.println("Error while reading and writing file");
         }
 
	}
 
	public ArrayList<String> getFileList(long curTime, long duration, String startString) {
		ArrayList<String> lst = new ArrayList<String>();
		;
		try {
			File fol = new File(folder);
			if (fol.isDirectory()) {
				File[] files = fol.listFiles();
				for (File f : files) {
					if (f.isFile() && f.getName().startsWith(startString)) {
						long lastmd = f.lastModified();
						long diff = curTime - lastmd;
						if (diff <= duration)
							lst.add(f.getName());
					}
				}
 
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
 
		return lst;
	}
}

Open in new window

0
a_bCommented:
This function will fetch the first column from the csv as a list

  public ArrayList<String> getListFrom CSVFile(String filePath)
    {
        File file = new File(filePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
       
        ArrayList<String>csvlist = new ArrayList<String>();

        try
        {
          fis = new FileInputStream(file);

          // Here BufferedInputStream is added for fast reading.
          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);

          // dis.available() returns 0 if the file does not have more lines.
          while (dis.available() != 0)
          {
            String line = dis.readLine();
            StringTokenizer st = new StringTokenizer(line, ",");
            csvlist.add(st.nextToken());
          }
         

          // dispose all the resources after using them.
          fis.close();
          bis.close();
          dis.close();
        }
        catch(Exception e)
        {
           
        }
        return csvlist;
}
0
supportproAuthor Commented:
that 10 you need to remove and modify .ACTUALLY THE ABOVE CODE IS FOR SEARCHING for files in a folder that has been modified x minutes back.There are lot of csv files in that folder.In that i need the csv files that are of the type  some ipaddress_someno.csv
and the ipaddress say x alone must be put in a text file.so now the text file contains x's returned from above.the value of x 's are different in the text file.now this text file must be compared with another csv file in another folder such that the value of x present in the first column must be found in text file.If it is not present in text file this x's for which there is no entry must also be added.lastly  in the csv file for which there are x 's in the text file a new column must be created corresponding to that x and "no " must be updated.
0
supportproAuthor Commented:
that 10 you need to remove and modify .ACTUALLY THE ABOVE CODE IS FOR SEARCHING for files in a folder that has been modified n minutes back.There are lot of csv files in that folder.In that i need the csv files that are of the type  some ipaddress_someno.csv
and the ipaddress say x alone must be put in a text file.so now the text file contains x's returned from above.the value of x 's are different in the text file.now this text file must be compared with another csv file in another folder such that the value of x present in the first column must be found in text file.If it is not present in text file this x's for which there is no entry must also be added.lastly  in the csv file for which there are x 's in the text file a new column must be created corresponding to that x and "no " must be updated.
0
a_bCommented:
"If it is not present in text file this x's for which there is no entry must also be added.lastly  in the csv file for which there are x 's in the text file a new column must be created corresponding to that x and "no " must be updated."

I am not sure. Once you have a text file that contains the list if th ips, now you take the csv where the first column conatins ips. If the ips in the csv are not found in the text file, you add that ip into the text file?  Please clarify.
0
supportproAuthor Commented:
could you please give me the full code once again
as i am not able to follow the blocks.please give the entire thing as a single file
0
supportproAuthor Commented:
yes we need to add them to the text file and from the text file we modify the csv file with new column inserted.
0
supportproAuthor Commented:
this csv file is the file which we used for comparison.it is this file that must be updated with the text file that contained the searched ip plus the ip in the csv file not in the text file output.
0
supportproAuthor Commented:
is the problem statement clear?please give as single file.
0
supportproAuthor Commented:
ie want code as single file full code.please
0
anilallewarCommented:
Here is the code file..I will try to address your concerns soon.
FileLister.txt
0
anilallewarCommented:
If you want to add the single ip as x, then you can give the IP as argument 2 on command line and the program will work. However you could work through 1 IP at a time with this program. Let me know if you have some other requirement.
0
supportproAuthor Commented:
for better understanding the folder to be checked is c:/cscopx.in this there are files like 10.345.678.98_12344.csv
23.456.78.9_2235.csv
tempdvb.csv
fswgtfsw.csv
like these many files.of these i need the ip address of the files modified n minutes back in a text file say ip.txt.it should contain only the ipaddress 10.345.678.98 if it modified n minutes back.now there is one more file in c;/bin/iplist.csv.this contains many columns the first being the ipaddress column for different devices.we need to check for every device here whether there is a entry in the text file.if it is not there the ipaddress must be added to text file ip.txt.now finally in c:/bin/iplist.csv for these devices in ip.txt a new column must be added in iplist.csv and a "no" entry must be put.
0
supportproAuthor Commented:
please give it as quickly as possible.it is a urgent requirement.give the whole code as single file.
0
supportproAuthor Commented:
hope you understood the problem requirement now.
0
anilallewarCommented:
You can run the program with 2 argument <No of minutes since modified> <IP address>
E.g.
$java FileLister 10 172.31.14.45
0
supportproAuthor Commented:
i wont give any ip everything comes from the file.that n minutes will be hard coded in the program itself.it is fixed.
0
supportproAuthor Commented:
for better understanding the folder to be checked is c:/cscopx.in this there are files like 10.345.678.98_12344.csv
23.456.78.9_2235.csv
143.576.678.34_dtr.csv
223.3634.47.347_set.csv
tempdvb.csv
fswgtfsw.csv
like these many files.of these i need the ip address of the files modified n minutes back in a text file say ip.txt.it should contain only the ipaddress 10.345.678.98,23.456.78.9 etc  if it modified n minutes back.now there is one more file in c;/bin/iplist.csv.this contains many columns the first being the ipaddress column for different devices.we need to check for every device here whether there is a entry in the text file.if it is not there the ipaddress must be added to text file ip.txt.now finally in c:/bin/iplist.csv for these devices in ip.txt a new column must be added in iplist.csv and a "no" entry must be put.please send it as early as possible.
0
a_bCommented:
You donot have to give each ip address. You can use regular expressions to match the files with ip addresses. Here is the modified code -  

public ArrayList<String> getFileList (long curTime, long duration)
    {
        ArrayList<String> lst = new ArrayList<String>();;
        try
        {
            File fol = new File(folder);
            if (fol.isDirectory())
            {
                File[] files = fol.listFiles();
                for (File f : files)
                {
                    if (f.isFile() && f.getName().matches(".\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"))
                    {
                        long lastmd = f.lastModified();
                        long diff = curTime - lastmd;
                        if (diff <= duration)
                            {
                                String temp = f.getName().substring(0, f.getName().indexOf('_'));
                                lst.add(temp);
                               
                            }
                    }
                }

            }
            else
            {
                return null;
            }
        }
        catch (Exception e)
        {
            return null;
        }

        return lst;
    }
0
supportproAuthor Commented:
can you please send it fully from the beginning as a single program as not able to follow in blocks.
0
a_bCommented:
Here is the code. Please change the path files in the code by yourself.
I am sure you can make some effort to test it out and smoothen the bugs, if not please let me know.



import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;
 
 
public class FileLister
{
    public static int date;
    private String folder = "C:\\Intel"; // The folder which needs to be operated.
    long duration = 0;
 
 
    public static void main (String args[])
    {
        FileLister flst = new FileLister();
        try
        {
            flst.duration = Integer.parseInt("20000000");
            // convert to millisec assuming input was in mins;
            flst.duration = flst.duration * 1000 * 60;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("invalid duration");
            System.exit(0);
 
        }
        Calendar calendar = Calendar.getInstance();
        long curTime = calendar.getTimeInMillis();
        ArrayList<String> lst = flst.getFileList(curTime, flst.duration);
        System.out.println("This is the list of files:" + lst.toString());
        try
        {
            File outputFile = new File("C://output.txt");
            FileWriter fout = new FileWriter(outputFile);
            for (String s : lst)
            {
                fout.write(s);
                fout.write("\n");
            }
            
            ArrayList<String> arrayList = flst.getListFromCSVFile("C:\\Temp");
            for (String s : arrayList)
            {
                if(!lst.contains(s))
                {
                    fout.write(s);
                    fout.write("\n");
                }
            }
            fout.close();
        }
        catch (IOException e1)
        {
            System.out.println("Error in getting create time");
        }
 
 
    }
 
 
    public ArrayList<String> getFileList (long curTime, long duration)
    {
        ArrayList<String> lst = new ArrayList<String>();;
        try
        {
            File fol = new File(folder);
            if (fol.isDirectory())
            {
                File[] files = fol.listFiles();
                for (File f : files)
                {
                    if (f.isFile() && f.getName().matches(".\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"))
                    {
                        long lastmd = f.lastModified();
                        long diff = curTime - lastmd;
                        if (diff <= duration)
                            {
                                String temp = f.getName().substring(0, f.getName().indexOf('_')); 
                                lst.add(temp);
                                
                            }
                    }
                }
 
            }
            else
            {
                return null;
            }
        }
        catch (Exception e)
        {
            return null;
        }
 
        return lst;
    }
    
    public ArrayList<String> getListFromCSVFile(String filePath)
    {
        File file = new File(filePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        
        ArrayList<String>csvlist = new ArrayList<String>();
 
        try 
        {
          fis = new FileInputStream(file);
 
          // Here BufferedInputStream is added for fast reading.
          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);
 
          // dis.available() returns 0 if the file does not have more lines.
          while (dis.available() != 0) 
          {
            String line = dis.readLine();
            StringTokenizer st = new StringTokenizer(line, ",");
            csvlist.add(st.nextToken());
          }
          
 
          // dispose all the resources after using them.
          fis.close();
          bis.close();
          dis.close();
          
        }
        catch(Exception e)
        {
            
        }
        return csvlist;
    }
    
}

Open in new window

0
anilallewarCommented:
Here it is...Please let me know if you require some more modifications.
FileLister.txt
0
supportproAuthor Commented:
i have run the program by giving c:\\gsudata instead of c:\\intel this is the folder to be searched for the files and in the function getlistfrom csv file instead of c:\\temp i gave c:\\ip.csv.but i dont get the updated file in ip.csv.is this correct or anyother parameters or input must be changed?in the place of 20000000 gave value as 1 for 1 minute.please help
0
a_bCommented:
what do you get in your output file?
0
supportproAuthor Commented:
i get nothing.the file is unchanged.no column is added.in the cmd prompt got the output
i ran as java FileLister
the output i got is
this is the list of files:[]
0
anilallewarCommented:
Let me know if you have tried the program I gave..the format would be

java FileLister 10
The current parameters is input folder - C:/Intel
Input csv file - C://Intel/output.csv
0
a_bCommented:
Let me check the regular expression.
0
anilallewarCommented:
This is the output I got

This is the list of files:[12.31.14.79_3.csv, 172.31.14.79_1.csv, 21.31.14.79_2.csv, output.csv]
12.31.14.79
172.31.14.79
21.31.14.79
[12.31.14.79, 172.31.14.79, 21.31.14.79]
172.31.14.79,1
Present: 172.31.14.79
172.31.14.79,2
Present: 172.31.14.79
172.31.14.79,3
Present: 172.31.14.79
x3,4
Not present: x3
0
a_bCommented:
@anilallewar: The problem with your regular expression is that it will pick even incorrect ip addresses
0
supportproAuthor Commented:
still not getting the solution.the csv file remains unchanged.do i need to do something else?
0
anilallewarCommented:
@a_b - if the file names are as per @supportpro those should be in this format. I agree that in the strictest sense the below expression is better

String Pattern="[\\d{1.3}]+.[\\d{1.3}]+.[\\d{1.3}]+.[\\d{1.3}]+_[\\w]*.*";
0
anilallewarCommented:
With my program, a new oputput file "outputfile.csv" gets generated on C: drive. You can move it or rename it as required.
0
supportproAuthor Commented:
the filenames i have are in this format 10.64.90.151_20090521.csv
i want 10.64.90.151
should i make any change?
0
anilallewarCommented:
not with my program...
0
supportproAuthor Commented:
but not getting the solution
0
anilallewarCommented:
Here is my unput CSV
12.31.14.79,1
21.31.14.79,2
172.31.14.79,3
x3,4

Here is the program output
This is the list of files:[12.31.14.79_3.csv, 172.31.14.79_1.csv, 21.31.14.79_2.csv, output.csv]
12.31.14.79
172.31.14.79
21.31.14.79
[12.31.14.79, 172.31.14.79, 21.31.14.79]
12.31.14.79,1
Present: 12.31.14.79
21.31.14.79,2
Present: 21.31.14.79
172.31.14.79,3
Present: 172.31.14.79
x3,4
Not present: x3

And I am attaching the output file
outputfile.txt
0
anilallewarCommented:
first is input csv
0
supportproAuthor Commented:

this is the output i got.
C:\Program Files\Java\jdk1.5.0_10\bin>javac FileLister.java
Note: FileLister.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

C:\Program Files\Java\jdk1.5.0_10\bin>java FileLister 10
This is the list of files:[]
0
anilallewarCommented:
Can you chcek the last modified timestamp of files in folder...Increase the duration from 10 to 1000 in Java invocation and check.
0
supportproAuthor Commented:
10.64.91.45      NA      UnSupported
10.76.245.236      NA      UnReachable
10.64.91.171      NA      UnReachable
10.76.252.202      NA      UnReachable
10.64.91.82      NA      UnReachable
10.76.93.118      NA      UnReachable
172.20.119.107      NA      UnReachable
10.76.91.97      NA      UnReachable
10.76.91.163      NA      UnReachable
10.77.241.28      NA      UnReachable
this is my input csv file.here the first column needs to be taken.

0
anilallewarCommented:
Since the file was supposed to be CSV, the separator been put is ,. Can you open the file in textpad and change it to below
10.64.91.45,NA,UnSupported
10.76.245.236,NA,UnReachable
10.64.91.171,NA,UnReachable
10.76.252.202,NA,UnReachable
10.64.91.82,NA,UnReachable
10.76.93.118,NA,UnReachable
172.20.119.107,NA,UnReachable
10.76.91.97,NA,UnReachable
10.76.91.163,NA,UnReachable
10.77.241.28,NA,UnReachable
0
supportproAuthor Commented:
i think the problem is with the type of file.on right clicking the csv file it shows me it opens with excel.even though i copied in notepad it is saved as excel sheet format.
0
supportproAuthor Commented:
is that the issue?
0
anilallewarCommented:
Jsut right click on the file and select "Open with" notepad.

Alternately create a new txt file and paste the contents. Save it as the "csv" file.
0
supportproAuthor Commented:
did it.but still not getting the output.
0
anilallewarCommented:
can you upload the input csv and some sample files? I will run against my program.
0
supportproAuthor Commented:
please rename the files as csv.these are the two input files  and the ip3 is the csv file.
10.64.90.151-20090519.xls
10.64.90.151-20090520.xls
ip3.xls
0
anilallewarCommented:
Tested using the samples provided. Please see the input and output files attached. Change the extension from .txt to .csv

Input file - output.txt
Output file - outputfile.txt
outputfile.txt
output.txt
0
anilallewarCommented:
Sorry these was with my samples..will run with your samples.
0
anilallewarCommented:
Got it @supportpro. There were 2 problems
1. The problems was with the file format. You had earlier given the file format as <ip address>_someno.csv whereas the files you have are in format <ip address>-someno.csv.
2. The input csv file had "" enclosed  contents. The leading " is taken as the input and that is why it cannot be matched. When you open the file in any text editor replace all " with empty space.

I am attaching the input file and generated output file along with the program for your reference. Rename the .xls file to .csv before opening.

ip3.xls
outputfile.xls
FileLister.txt
0
supportproAuthor Commented:
please help me in modifing the above code so that the text file containing the files modified n minutes back be compared with the csv file having the filename as some <ipaddress_GroupMembership-output.csv> in the same folder.In this csv file every entry in the first column must be compared with the corresponding entry in the  third column and if the value in the third column is equal to  "monitored" then the first column entry must be compared with the text file entries.If this entry is not present in text file,this new entry must be added in the text file.Lastly all the entries in the text file must be updated to the csv file with a new column added for the corresponding entries and the value in that column for those entries must be "no".
  import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;
 
 
public class FileLister
{
    public static int date;
    private String folder = "C:/intel"; // The folder which needs to be operated.
    long duration = 0;
 
 
    public static void main (String args[])
    {
        FileLister flst = new FileLister();
        try
        {
            flst.duration = Integer.parseInt("20000000");
            // convert to millisec assuming input was in mins;
            flst.duration = flst.duration * 1000 * 60;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("invalid duration");
            System.exit(0);
 
        }
        Calendar calendar = Calendar.getInstance();
        long curTime = calendar.getTimeInMillis();
        ArrayList<String> lst = flst.getFileList(curTime, flst.duration);
        System.out.println("This is the list of files:" + lst.toString());
        try
        {
            File outputFile = new File("C://output.txt");
            FileWriter fout = new FileWriter(outputFile);
            for (String s : lst)
            {
                fout.write(s);
                fout.write("\n");
            }
            
            ArrayList<String> arrayList = flst.getListFromCSVFile("C://intel/ip3.csv");
            for (String s : arrayList)
            {
                if(!lst.contains(s))
                {
                    fout.write(s);
                    fout.write("\n");
                }
            }
            fout.close();
        }
        catch (IOException e1)
        {
            System.out.println("Error in getting create time");
        }
 
 
    }
 
 
    public ArrayList<String> getFileList (long curTime, long duration)
    {
        ArrayList<String> lst = new ArrayList<String>();;
        try
        {
            File fol = new File(folder);
            if (fol.isDirectory())
            {
                File[] files = fol.listFiles();
                for (File f : files)
                {
                    if (f.isFile() && f.getName().matches(".\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"))
                    {
                        long lastmd = f.lastModified();
                        long diff = curTime - lastmd;
                        if (diff <= duration)
                            {
                                String temp = f.getName().substring(0, f.getName().indexOf('_')); 
                                lst.add(temp);
                                
                            }
                    }
                }
 
            }
            else
            {
                return null;
            }
        }
        catch (Exception e)
        {
            return null;
        }
 
        return lst;
    }
    
    public ArrayList<String> getListFromCSVFile(String filePath)
    {
        File file = new File(filePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        
        ArrayList<String>csvlist = new ArrayList<String>();
 
        try 
        {
          fis = new FileInputStream(file);
 
          // Here BufferedInputStream is added for fast reading.
          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);
 
          // dis.available() returns 0 if the file does not have more lines.
          while (dis.available() != 0) 
          {
            String line = dis.readLine();
            StringTokenizer st = new StringTokenizer(line, ",");
            csvlist.add(st.nextToken());
          }
          
 
          // dispose all the resources after using them.
          fis.close();
          bis.close();
          dis.close();
          
        }
        catch(Exception e)
        {
            
        }
        return csvlist;
    }
    
}

Open in new window

0
supportproAuthor Commented:
the code is import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;
 
 
public class FileLister
{
    public static int date;
    private String folder = "C:"+File.separator+"Intel"; // The folder which needs to be operated.
    long duration = 0;
 
 
    public static void main (String args[])
    {
        FileLister flst = new FileLister();
        try
        {
            flst.duration = Integer.parseInt(args[0]);
            // convert to millisec assuming input was in mins;
            flst.duration = flst.duration * 1000 * 60;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("invalid duration");
            System.exit(0);
 
        }
        Calendar calendar = Calendar.getInstance();
        long curTime = calendar.getTimeInMillis();
        ArrayList<String> lst = flst.getFileList(curTime, flst.duration);
        System.out.println("This is the list of files:" + lst.toString());
        try
        {
            File outputFile = new File("C://output.txt");
            FileWriter fout = new FileWriter(outputFile);
            for (String s : lst)
            {
                fout.write(s);
                fout.write("\n");
            }
           
            ArrayList<String> arrayList = flst.getListFromCSVFile("C:"+File.separator+"Intel"+File.separator+"ip4.csv");
 

            for (String s : arrayList)
            {
                   
                 
                if(!lst.contains(s))
                {
                    fout.write(s);
                    fout.write("\n");
 
                }
            }
            fout.close();
        }
        catch (IOException e1)
        {
            System.out.println("Error in getting create time");
        }
 
 
    }
 
 
    public ArrayList<String> getFileList (long curTime, long duration)
    {
        ArrayList<String> lst = new ArrayList<String>();;
        try
        {
            File fol = new File(folder);
            if (fol.isDirectory())
            {
                File[] files = fol.listFiles();
                for(File f:files)
    {
          if(f.isFile())
          {
          long lastmd = f.lastModified();
          long diff = curTime-lastmd;
          if(diff>=duration)
                lst.add(f.getName());
             }
    }
   
    }
           
            else
            {
                return null;
            }
        }
        catch (Exception e)
        {
            return null;
        }
 
        return lst;
    }
   
    public ArrayList<String> getListFromCSVFile(String filePath)
    {
        File file = new File(filePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
       
        ArrayList<String>csvlist = new ArrayList<String>();

        try
        {
          fis = new FileInputStream(file);
 
          // Here BufferedInputStream is added for fast reading.
          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);
 
          // dis.available() returns 0 if the file does not have more lines.
          while (dis.available() != 0)
          {

 
            String line = dis.readLine();
            StringTokenizer st = new StringTokenizer(line, ",");
            csvlist.add(st.nextToken());
          }
         
 
          // dispose all the resources after using them.
          fis.close();
          bis.close();
          dis.close();
         
        }
        catch(Exception e)
        {
           
        }
        return csvlist;
    }
   
}
 
0
anilallewarCommented:
Can you upload the sample CSV input file and the expected output file? This would help me making the modifications at the earliest and give you correct code.
0
supportproAuthor Commented:
answer helped
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.