Results 1 to 3 of 3
  1. #1
    Hyeri is offline Member
    Join Date
    Dec 2016
    Posts
    1
    Rep Power
    0

    Default Write command deletes everything in csv file

    I want to extract URLs from the text and store them in a separate column in the same file. I'm using opencsv to read a csv file in java, so far reading works fine and it does extract urls from the text however the problem is I don't know how to write in the csv file. Here is my code in java

    Java Code:
    package parser;
    
    import com.opencsv.CSVReader;
    import com.opencsv.CSVWriter;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.ArrayList;
    
    public class Parser {
    
        /**
         * @param args the command line arguments
         */
        public static List<String> extractUrls(String text)
    {
        List<String> containedUrls = new ArrayList<String>();
        String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
        Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
        Matcher urlMatcher = pattern.matcher(text);
    
        while (urlMatcher.find())
        {
            containedUrls.add(text.substring(urlMatcher.start(0),
                    urlMatcher.end(0)));
        }
        System.out.print(containedUrls);
        return containedUrls;
    
    }
    
        public static void main(String[] args) throws IOException {
            String csvFilename = "check.csv";
            CSVReader csvReader = null; 
    
            CSVWriter writer = null;
            try {
                csvReader = new CSVReader(new FileReader(csvFilename));
                writer = new CSVWriter(new FileWriter(csvFilename));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
            }
            String[] row = null;
            try {
                while((row = csvReader.readNext()) != null) {
    
                        Parser extract = new Parser();
                         extract.extractUrls(row[2]);   //the third column contains the text and urls
    
    
    //                     writer.writeNext(row);
    //                     writer.close();
                }       } catch (IOException ex) {
                Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    
            try {
                csvReader.close();
            } catch (IOException ex) {
                Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    
    
    
    
        }
    
    }
    Here is what the result should look like Results

    https://i.stack.imgur.com/nVuLF.png

    when I un-comment the above write lines it deletes EVERYTHING in my csv file instead of appending new data to a new column

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Write command deletes everything in csv file

    Is there an API doc for the classes and methods in com.opencsv package that explains how to use the classes and methods?

    write-command-deletes-everything
    Look at the API doc for the FileWriter class. It has a constructor to use if you want to append data to an existing file.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Write command deletes everything in csv file

    It looks good. One error though: don't close the writer in the loop.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 0
    Last Post: 06-04-2013, 07:18 PM
  2. Replies: 3
    Last Post: 07-07-2012, 07:44 AM
  3. Replies: 0
    Last Post: 05-07-2012, 10:10 PM
  4. write command to trigger com port
    By jachah in forum New To Java
    Replies: 1
    Last Post: 03-22-2012, 07:56 AM
  5. Replies: 0
    Last Post: 12-08-2010, 07:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •