Hello guys,

First of all I'm not really good with Java. To be honest I'd rather call my Java skills bad. But here comes my problem.
I want to download a CSV file from a specific URL and save it at the TEMP directory. Then i want to read this file and extract specific data from this file. So far so good. I can download the file and save it. And i can read the whole file. But now I want for example all values from the 4th column.

Here's my code. As you can see I want to create a new document in the database for every entry and parse data from the CSV file this Notes document.

Thanks in advance.

import lotus.domino.*;
import java.io.*;
import java.util.StringTokenizer;
import java.io.BufferedReader;

public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();

          // (Your code goes here)
          
          // Hardcoded URL
          url = "http://www.hm-treasury.gov.uk/d/sanctionsconlist.csv";
          
          
          // Download and save the file
          java.io.BufferedInputStream in = new java.io.BufferedInputStream(new           
          java.net.URL(url).openStream());
          java.io.FileOutputStream fos = new java.io.FileOutputStream("C:/Windows/Temp/yncompliance.csv");
          java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(fos,1024);
          byte[] data = new byte[1024];
          while(in.read(data,0,1024)>=0)
          {
          bout.write(data);
          }
          bout.close();          
          in.close();
          
          
          // Read the file           
          String fileName="C:/Windows/Temp/yncompliance.csv";
          BufferedReader br = new BufferedReader( new FileReader(fileName));
          
          //Variables
          String fileLine = "";
          StringTokenizer st = null;          
          int lineNumber = 0;
          int row = 0;
          int tokenNumber = 0;          
          
			//Read every line of the CSV File
			while((fileLine = br.readLine()) != null)	{
				
				Document pDoc = db.createDocument();
				pDoc.appendItemValue("Form", "Person");				
        	  	        lineNumber++;        	  	
        	  	        st = new StringTokenizer(fileLine, ",", true);
        	  	
				while(st.hasMoreTokens()){
				tokenNumber++;			
				System.out.println("Line # " + lineNumber +
						", Tokennumber : "+tokenNumber+
						", "+ st.nextToken());				
				}				
								
			if (pDoc.save())
			        System.out.println("Document has been saved");
			else
			        System.out.println("Unable to save document");				
				tokenNumber = 0;
				row++;
			}          
          
      } catch(Exception e) {
          e.printStackTrace();
       }
   }
}

Recommended Answers

All 2 Replies

As long as there are no "quoted" fields in the csv, then simply read it line by line (using a BufferedReader wrapped around a FileReader and BufferedReaders readLine method) and use split to split the line to a String[] and take index 3.

Edit: Also, if you don't need to retain the file, then use a BufferedReader, wrapped around an InputStreamReader, wrapped around the inputStream from the HttpURLConnection and parse it directly from the web.

Ok seems like it was a rather simple problem.
Anyway Thanks for your help. I've changed my code and it works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.