Example usage for java.io FileWriter FileWriter

List of usage examples for java.io FileWriter FileWriter

Introduction

In this page you can find the example usage for java.io FileWriter FileWriter.

Prototype

public FileWriter(File file, Charset charset) throws IOException 

Source Link

Document

Constructs a FileWriter given the File to write and java.nio.charset.Charset charset .

Usage

From source file:com.dianping.wed.cache.redis.util.TestDataUtil.java

public static void main(String[] args) {
    // ????/*from   ww  w. j a  va2 s.co m*/

    //&method=incr&parameterTypes=com.dianping.wed.cache.redis.dto.WeddingRedisKeyDTO&parameters={"category":"justtest","params":["1","2"]}
    List<String> opList = new ArrayList<String>(opAndKey.keySet());
    for (int i = 0; i < 1000; i++) {
        int index = RandomUtils.nextInt(opList.size());
        String op = opList.get(index);
        String key = opAndKey.get(op);
        StringBuilder result = new StringBuilder();
        int params = buildTestUrl(result, op, key);

        String fileName = "/Users/Bob/Desktop/data/data-" + params + ".csv";
        try {
            //kuka.txt
            //kuka.txt
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(result.toString());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println("done");
}

From source file:com.cisco.dbds.utils.report.CustomReport.java

/**
 * The main method.//from  w w  w .  ja  v a  2s  .c  o  m
 *
 * @param args the arguments
 * @throws FileNotFoundException the file not found exception
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws ParseException the parse exception
 * @throws AddressException the address exception
 */
public static void main(String[] args)
        throws FileNotFoundException, IOException, ParseException, AddressException {

    // reportparsejson();
    String msg = reportparsejson();
    sendmail(msg);
    String eol = System.getProperty("line.separator");
    //msg=msg.replaceAll("</tr>", eol+"</tr>");
    msg = msg.replaceAll("<tr", eol + "<tr");
    msg = msg.replaceAll("</table>", eol + "</table>");
    msg = msg.replaceAll("<table", eol + "<table");
    new FileOutputStream(htmlfname).close();
    htmlfile = new PrintWriter(new BufferedWriter(new FileWriter(htmlfname, true)));

    htmlfile.print(msg);
    htmlfile.close();

    // String mailContent = "Hi All<br>"+
    // "Build Tag id: jenkins-CANEAS-Nightly-Completed-190 has been completed and the run reports  found in the link: http://10.78.216.52:8080/job/CANEAS-Nightly-Completed/190/cucumber-html-reports/"
    // + "<br>"
    // +
    //
    // "EDCS link for failure analysis Reasons: http://wwwin-eng.cisco.com/cgi-bin/edcs/edcs_info?3677074"
    // + "<br>"
    // +
    // "(will be updated soon for the Build: jenkins-CANEAS-Nightly-Completed-190 )"
    // + "<br><br>" + "Regards" + "<br>" + "SIT Automation TEAM"
    // + "<br>";
    // sendmail(mailContent);
}

From source file:com.cisco.dbds.utils.tims.TIMS.java

/**
 * The main method.//  w w w  .  ja v  a  2 s .c o m
 *
 * @param args the arguments
 * @throws Exception the exception
 */
public static void main(String args[]) throws Exception {

    ConfigFileHandler.loadConfigFile("src/it/resources");
    new FileOutputStream(fname).close();
    outlog = new PrintWriter(new BufferedWriter(new FileWriter(fname, true)));
    //updatetimsresult("Ttv9629533c", "passed");

    //       initializeTIMSParameters(
    //         System.getProperty("tims.userID").trim(), 
    //         System.getProperty("tims.projectID").trim(), 
    //         System.getProperty("tims.configID").trim(),
    //         System.getProperty("tims.token").trim(),
    //         System.getProperty("tims.sw").trim(),
    //         System.getProperty("tims.platform").trim(),
    //         System.getProperty("tims.browsertype").trim(),
    //         System.getProperty("tims.browserversion").trim()
    //    );
    //      
    //      postTIMSsearch("Ttv9629533c");

    updateHTMLtoTIMSresults();
    //      ArrayList<String> re1 = new ArrayList<String>(); 
    //      re1 = readhtmlfilePass();
    //      System.out.println(re1.size()+" bvvv "+re1);

    //createtimsresult("Tl4352381c", "failed","sample111555");
}

From source file:com.sat.spvgt.utils.tims.TIMS.java

/**
 * The main method.//from   w w  w.  ja  va  2s  . c  o  m
 * 
 * @param args
 *            the arguments
 * @throws Exception
 *             the exception
 */
public static void main(String args[]) throws Exception {

    ConfigFileHandlerManager miscConfigFileHandler = new ConfigFileHandlerManager();
    miscConfigFileHandler.loadConfigFileBasedOnPath("src/it/resources");
    new FileOutputStream(fname).close();
    outlog = new PrintWriter(new BufferedWriter(new FileWriter(fname, true)));
    // updatetimsresult("Ttv9629533c", "passed");

    // initializeTIMSParameters(
    // System.getProperty("tims.userID").trim(),
    // System.getProperty("tims.projectID").trim(),
    // System.getProperty("tims.configID").trim(),
    // System.getProperty("tims.token").trim(),
    // System.getProperty("tims.sw").trim(),
    // System.getProperty("tims.platform").trim(),
    // System.getProperty("tims.browsertype").trim(),
    // System.getProperty("tims.browserversion").trim()
    // );
    //
    // postTIMSsearch("Ttv9629533c");

    updateHTMLtoTIMSresults();
    // ArrayList<String> re1 = new ArrayList<String>();
    // re1 = readhtmlfilePass();
    // System.out.println(re1.size()+" bvvv "+re1);

    // createtimsresult("Tl4352381c", "failed","sample111555");
}

From source file:net.semanticmetadata.lire.solr.AddImages.java

public static void main(String[] args) throws IOException, InterruptedException {
    BitSampling.readHashFunctions();//from w w w  .  j av a 2 s  .c  o  m
    LinkedList<Thread> threads = new LinkedList<Thread>();
    for (int j = 10; j < 21; j++) {
        final int tz = j;
        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    List<File> files = FileUtils
                            .getAllImageFiles(new File("D:\\DataSets\\WIPO-US\\jpg_us_trim\\" + tz), true);
                    int count = 0;
                    BufferedWriter br = new BufferedWriter(new FileWriter("add-us-" + tz + ".xml", false));
                    br.write("<add>\n");
                    for (Iterator<File> iterator = files.iterator(); iterator.hasNext();) {
                        File file = iterator.next();
                        br.write(createAddDoc(file).toString());
                        count++;
                        //                            if (count % 1000 == 0) System.out.print('.');
                    }
                    br.write("</add>\n");
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
                }
            }
        };
        t.start();
        threads.add(t);
    }
    for (Iterator<Thread> iterator = threads.iterator(); iterator.hasNext();) {
        Thread next = iterator.next();
        next.join();
    }
}

From source file:net.morphbank.webclient.ProcessFiles.java

/**
 * @param args//from w  ww. j  a v  a2 s  .  c  o m
 *            args[0] directory including terminal '/' 
 *            args[1] prefix of request files 
 *            args[2] number of digits in file index value
 *            args[3] number of files 
 *            args[4] index of first file (default 0) 
 *            args[5] prefix of service 
 *            args[6] prefix of response files (default args[1] + "Resp")
 */
public static void main(String[] args) {
    ProcessFiles fileProcessor = new ProcessFiles();
    // restTest.processRequest(URL, UPLOAD_FILE);

    String zeros = "0000000";

    if (args.length < 4) {
        System.out.println("Too few parameters");
    } else {
        try {
            // get parameters
            String reqDir = args[0];
            String reqPrefix = args[1];
            int numDigits = Integer.valueOf(args[2]);
            if (numDigits > zeros.length())
                numDigits = zeros.length();
            NumberFormat intFormat = new DecimalFormat(zeros.substring(0, numDigits));
            int numFiles = Integer.valueOf(args[3]);
            int firstFile = 0;

            BufferedReader fileIn = new BufferedReader(new FileReader(FILE_IN_PATH));
            String line = fileIn.readLine();
            fileIn.close();
            firstFile = Integer.valueOf(line);
            // firstFile = 189;
            // numFiles = 1;
            int lastFile = firstFile + numFiles - 1;
            String url = URL;
            String respPrefix = reqPrefix + "Resp";
            if (args.length > 5)
                respPrefix = args[5];
            if (args.length > 6)
                url = args[6];

            // process files
            for (int i = firstFile; i <= lastFile; i++) {
                String xmlOutputFile = null;
                String requestFile = reqDir + reqPrefix + intFormat.format(i) + ".xml";
                System.out.println("Processing request file " + requestFile);
                String responseFile = reqDir + respPrefix + intFormat.format(i) + ".xml";
                System.out.println("Response file " + responseFile);
                // restTest.processRequest(URL, UPLOAD_FILE);
                fileProcessor.processRequest(url, requestFile, responseFile);
                Writer fileOut = new FileWriter(FILE_IN_PATH, false);
                fileOut.append(Integer.toString(i + 1));
                fileOut.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:BookRank.java

/** Grab the sales rank off the web page and log it. */
public static void main(String[] args) throws Exception {

    Properties p = new Properties();
    String title = p.getProperty("title", "NO TITLE IN PROPERTIES");
    // The url must have the "isbn=" at the very end, or otherwise
    // be amenable to being string-catted to, like the default.
    String url = p.getProperty("url", "http://test.ing/test.cgi?isbn=");
    // The 10-digit ISBN for the book.
    String isbn = p.getProperty("isbn", "0000000000");
    // The RE pattern (MUST have ONE capture group for the number)
    String pattern = p.getProperty("pattern", "Rank: (\\d+)");

    // Looking for something like this in the input:
    //    <b>QuickBookShop.web Sales Rank: </b>
    //    26,252/*from   ww  w.  ja v a 2 s  .co m*/
    //    </font><br>

    Pattern r = Pattern.compile(pattern);

    // Open the URL and get a Reader from it.
    BufferedReader is = new BufferedReader(new InputStreamReader(new URL(url + isbn).openStream()));
    // Read the URL looking for the rank information, as
    // a single long string, so can match RE across multi-lines.
    String input = "input from console";
    // System.out.println(input);

    // If found, append to sales data file.
    Matcher m = r.matcher(input);
    if (m.find()) {
        PrintWriter pw = new PrintWriter(new FileWriter(DATA_FILE, true));
        String date = // `date +'%m %d %H %M %S %Y'`;
                new SimpleDateFormat("MM dd hh mm ss yyyy ").format(new Date());
        // Paren 1 is the digits (and maybe ','s) that matched; remove comma
        Matcher noComma = Pattern.compile(",").matcher(m.group(1));
        pw.println(date + noComma.replaceAll(""));
        pw.close();
    } else {
        System.err.println("WARNING: pattern `" + pattern + "' did not match in `" + url + isbn + "'!");
    }

    // Whether current data found or not, draw the graph, using
    // external plotting program against all historical data.
    // Could use gnuplot, R, any other math/graph program.
    // Better yet: use one of the Java plotting APIs.

    String gnuplot_cmd = "set term png\n" + "set output \"" + GRAPH_FILE + "\"\n" + "set xdata time\n"
            + "set ylabel \"Book sales rank\"\n" + "set bmargin 3\n" + "set logscale y\n"
            + "set yrange [1:60000] reverse\n" + "set timefmt \"%m %d %H %M %S %Y\"\n" + "plot \"" + DATA_FILE
            + "\" using 1:7 title \"" + title + "\" with lines\n";

    Process proc = Runtime.getRuntime().exec("/usr/local/bin/gnuplot");
    PrintWriter gp = new PrintWriter(proc.getOutputStream());
    gp.print(gnuplot_cmd);
    gp.close();
}

From source file:com.yhfudev.SimulatorForSelfStabilizing.java

public static void main(String[] args) {
    // command line lib: apache CLI http://commons.apache.org/proper/commons-cli/
    // command line arguments:
    //  -- input file
    //  -- output line to a csv file
    //  -- algorithm: Ding's linear or randomized

    // single thread parsing ...
    Options options = new Options();
    options.addOption("h", false, "print this message");
    //heuristic//from   w w  w . j  av a 2s .c o  m
    options.addOption("u", false, "(rand) heuristic on");
    options.addOption("y", true, "show the graph with specified delay (ms)");
    options.addOption("i", true, "the input file name");
    options.addOption("o", true, "the results is save to a attachable output cvs file");
    options.addOption("l", true, "the graph activities trace log file name");
    options.addOption("s", true, "save the graph to a file");
    options.addOption("a", true, "the algorithm name, ding or rand");
    // options specified to generator
    options.addOption("g", true,
            "the graph generator algorithm name: fan1l, fan2l, rand, doro, flower, watt, lobster");
    options.addOption("n", true, "the number of nodes");
    options.addOption("d", true, "(rand) the node degree (max)");
    options.addOption("f", false, "(rand) if the degree value is fix or not");
    options.addOption("p", true, "(watt) the probability of beta");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        e.printStackTrace();
        return;
    }
    if (cmd.hasOption("h")) {
        showHelp(options);
        return;
    }

    int delay_time = 0;
    if (cmd.hasOption("y")) {
        delay_time = Integer.parseInt(cmd.getOptionValue("y"));
    }

    String sFileName = null;
    sFileName = null;
    FileWriter writer = null;
    if (cmd.hasOption("o")) {
        sFileName = cmd.getOptionValue("o");
    }
    if ((null != sFileName) && (!"".equals(sFileName))) {
        try {
            writer = new FileWriter(sFileName, true); // true: append
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error: unable to open the output file " + sFileName);
            return;
        }
    }
    FileWriter wrGraph = null;
    sFileName = null;
    if (cmd.hasOption("s")) {
        sFileName = cmd.getOptionValue("s");
    }
    if ((null != sFileName) && (!"".equals(sFileName))) {
        try {
            wrGraph = new FileWriter(sFileName, true); // true: append
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error: unable to open the saveGraph file " + sFileName);
            return;
        }
    }

    sFileName = null;
    if (cmd.hasOption("i")) {
        sFileName = cmd.getOptionValue("i");
    }
    String genname = null;
    if (cmd.hasOption("g")) {
        genname = cmd.getOptionValue("g");
    }
    if ((null == genname) && (null == sFileName)) {
        System.out.println("Error: not specify the input file or graph generator");
        showHelp(options);
        return;
    }
    if ((null != genname) && (null != sFileName)) {
        System.out.println("Error: do not specify the input file and graph generator at the same time");
        showHelp(options);
        return;
    }

    if (delay_time > 0) {
        // create and display a graph
        System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
    }

    Graph graph = new SingleGraph("test");
    //graph.setNullAttributesAreErrors(true); // to throw an exception instead of returning null (in getAttribute()).
    if (delay_time > 0) {
        graph.addAttribute("ui.quality");
        graph.addAttribute("ui.antialias");
        graph.addAttribute("ui.stylesheet", "url(data/selfstab-mwcds.css);");
        graph.display();
    }

    // save the trace to file
    FileSinkDGS dgs = null;
    if (cmd.hasOption("l")) {
        dgs = new FileSinkDGS();
        graph.addSink(dgs);
        try {
            dgs.begin(cmd.getOptionValue("l"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Generator generator = null;
    if (null != sFileName) {
        System.out.println("DEBUG: the input file=" + sFileName);
        FileSource source = new FileSourceDGS();
        source.addSink(graph);
        int count_edge_error = 0;
        try {
            //source.begin("data/selfstab-mwcds.dgs"); // Ding's paper example
            //source.begin("data/selfstab-ds.dgs");    // DS example
            //source.begin("data/selfstab-doro-1002.dgs"); // DorogovtsevMendes
            //source.begin("data/selfstab-rand-p10-10002.dgs"); // random connected graph with degree = 10% nodes
            //source.begin("data/selfstab-rand-f5-34.dgs"); // random connected graph with degree = 5
            source.begin(sFileName);
            while (true) {
                try {
                    if (false == source.nextEvents()) {
                        break;
                    }
                } catch (EdgeRejectedException e) {
                    // ignore
                    count_edge_error++;
                    System.out.println("DEBUG: adding edge error: " + e.toString());
                }
                if (delay_time > 0) {
                    delay(delay_time);
                }
            }
            source.end();
            //} catch (InterruptedException e) {
            //    e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("DEBUG: END read from source. # of edges ignored=" + count_edge_error);
    } else {
        // assert (genname != null);

        // graph generator
        //generator = new ChvatalGenerator(); // fix size
        //generator = new FullGenerator(); // full connected, 2 steps,1 node in dominate set
        //generator = new GridGenerator(); // only one result
        //generator = new HypercubeGenerator(); // one result
        //generator = new IncompleteGridGenerator(); // error
        //generator = new PetersenGraphGenerator(); // fix size
        //generator = new PointsOfInterestGenerator(); // error
        //generator = new RandomEuclideanGenerator(); // linear algo endless loop
        //generator = new RandomFixedDegreeDynamicGraphGenerator(); //
        //generator = new RandomGenerator(); //
        //generator = new URLGenerator("http://www.cnbeta.com"); //
        //generator = new WikipediaGenerator("Antarctica"); // no end

        //generator = new DorogovtsevMendesGenerator(); // ok
        //generator = new FlowerSnarkGenerator(); // ok
        //generator = new WattsStrogatzGenerator(maxSteps, 30, 0.5); // small world, ok
        //generator = new LobsterGenerator(); // tree like, ok

        int i;
        int n = 12; // the number of nodes
        if (cmd.hasOption("n")) {
            n = Integer.parseInt(cmd.getOptionValue("n"));
        }
        int d = 3; // the degree of nodes
        if (cmd.hasOption("d")) {
            d = Integer.parseInt(cmd.getOptionValue("d"));
        }
        boolean isFix = false;
        if (cmd.hasOption("f")) {
            isFix = true;
        }
        if ("".equals(genname)) {
            System.out.println("Error: not set generator name");
            return;
        } else if ("fan1l".equals(genname)) {
            generator = new FanGenerator();
        } else if ("fan2l".equals(genname)) {
            generator = new Fan2lGenerator(graph, d);
        } else if ("doro".equals(genname)) {
            generator = new DorogovtsevMendesGenerator();
        } else if ("flower".equals(genname)) {
            generator = new FlowerSnarkGenerator();
        } else if ("lobster".equals(genname)) {
            generator = new LobsterGenerator();
        } else if ("rand".equals(genname)) {
            generator = new ConnectionGenerator(graph, d, false, isFix);
        } else if ("watt".equals(genname)) {
            // WattsStrogatzGenerator(n,k,beta)
            // a ring of n nodes
            // each node is connected to its k nearest neighbours, k must be even
            // n >> k >> log(n) >> 1
            // beta being a probability it must be between 0 and 1.
            int k;
            double beta = 0.5;
            if (cmd.hasOption("p")) {
                beta = Double.parseDouble(cmd.getOptionValue("p"));
            }
            k = (n / 20) * 2;
            if (k < 2) {
                k = 2;
            }
            if (n < 2 * 6) {
                n = 2 * 6;
            }
            generator = new WattsStrogatzGenerator(n, k, beta);
        }
        /*int listf5[][] = {
        {12, 5},
        {34, 5},
        {102, 5},
        {318, 5},
        {1002, 5},
        {3164, 5},
        {10002, 5},
        };
        int listp3[][] = {
        {12, 2},
        {34, 2},
        {102, 3},
        {318, 9},
        {1002, 30},
        {3164, 90},
        {10002, 300},
        };
        int listp10[][] = {
        {12, 2},
        {34, 3},
        {102, 10},
        {318, 32},
        {1002, 100},
        {3164, 316},
        {10002, 1000},
        };
        i = 6;
        maxSteps = listf5[i][0];
        int degree = listf5[i][1];
        generator = new ConnectionGenerator(graph, degree, false, true);
        */
        generator.addSink(graph);
        generator.begin();
        for (i = 1; i < n; i++) {
            generator.nextEvents();
        }
        generator.end();
        delay(500);
    }

    if (cmd.hasOption("a")) {
        SinkAlgorithm algorithm = null;
        String algo = "rand";
        algo = cmd.getOptionValue("a");
        if ("ding".equals(algo)) {
            algorithm = new SelfStabilizingMWCDSLinear();
        } else if ("ds".equals(algo)) {
            algorithm = new SelfStabilizingDSLinear();
        } else {
            algorithm = new SelfStabilizingMWCDSRandom();
        }
        algorithm.init(graph);
        algorithm.setSource("0");
        if (delay_time > 0) {
            algorithm.setAnimationDelay(delay_time);
        }
        if (cmd.hasOption("u")) {
            algorithm.heuristicOn(true);
        } else {
            algorithm.heuristicOn(false);
        }
        algorithm.compute();

        GraphVerificator verificator = new MWCDSGraphVerificator();
        if (verificator.verify(graph)) {
            System.out.println("DEBUG: PASS MWCDSGraphVerificator verficiation.");
        } else {
            System.out.println("DEBUG: FAILED MWCDSGraphVerificator verficiation!");
        }

        if (null != writer) {
            AlgorithmResult result = algorithm.getResult();
            result.SaveTo(writer);
            try {
                writer.flush();
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        algorithm.terminate();
    }

    if (null != generator) {
        generator.removeSink(graph);
    }
    if (dgs != null) {
        graph.removeSink(dgs);
        try {
            dgs.end();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (null != wrGraph) {
        try {
            saveGraph(graph, wrGraph);
            wrGraph.flush();
            wrGraph.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:it.sayservice.platform.smartplanner.utils.LegGenerator.java

public static void main(String[] args) throws IOException {

    Mongo m = new Mongo("localhost"); // default port 27017
    DB db = m.getDB("smart-planner-15x");
    DBCollection coll = db.getCollection("stops");

    // read trips.txt(trips,serviceId).
    List<String[]> trips = readFileGetLines("src/main/resources/schedules/17/trips.txt");
    List<String[]> stopTimes = readFileGetLines("src/main/resources/schedules/17/stop_times.txt");
    for (String[] words : trips) {
        try {/*from  w w w. ja  va  2s  . c  o m*/
            String routeId = words[0].trim();
            String serviceId = words[1].trim();
            String tripId = words[2].trim();
            // fetch schedule for trips.
            for (int i = 0; i < stopTimes.size(); i++) {
                // already ordered by occurence.
                String[] scheduleLeg = stopTimes.get(i);
                if (scheduleLeg[0].equalsIgnoreCase(tripId)) {
                    // check if next leg belongs to same trip
                    if (stopTimes.get(i + 1)[0].equalsIgnoreCase(tripId)) {

                        String arrivalT = scheduleLeg[1];
                        String departT = scheduleLeg[2];
                        String sourceId = scheduleLeg[3];
                        String destId = stopTimes.get(i + 1)[3];
                        // get coordinates of stops.
                        /**
                         * make sure that mongo stop collection is
                         * populated. if, not, invoke
                         * http://localhost:7070/smart
                         * -planner/rest/getTransitTimes
                         * /TB_R2_R/1366776000000/1366819200000
                         */
                        Stop source = (Stop) getObjectByField(db, "id", sourceId, coll, Stop.class);
                        Stop destination = (Stop) getObjectByField(db, "id", destId, coll, Stop.class);
                        // System.out.println(tripId + ","
                        // + routeId + ","
                        // + source.getId() + ","
                        // + source.getLatitude() + ","
                        // + source.getLongitude() + ","
                        // + arrivalT + ","
                        // + destination.getId() + ","
                        // + destination.getLatitude() + ","
                        // + destination.getLongitude() + ","
                        // + departT + ","
                        // + serviceId
                        // );
                        String content = tripId + "," + routeId + "," + source.getStopId() + ","
                                + source.getLatitude() + "," + source.getLongitude() + "," + arrivalT + ","
                                + destination.getStopId() + "," + destination.getLatitude() + ","
                                + destination.getLongitude() + "," + departT + "," + "Giornaliero" + "\n";

                        File file = new File("src/main/resources/legs/legs.txt");
                        // single leg file
                        if (!file.exists()) {
                            file.createNewFile();
                        }

                        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
                        BufferedWriter bw = new BufferedWriter(fw);
                        bw.write(content);
                        bw.close();
                        // individual trip leg file.
                        File fileT = new File("src/main/resources/legs/legs_" + routeId + ".txt");
                        FileWriter fwT = new FileWriter(fileT.getAbsoluteFile(), true);
                        BufferedWriter bwT = new BufferedWriter(fwT);
                        bwT.write(content);
                        bwT.close();

                    }
                }
            }

        } catch (Exception e) {
            System.out.println("Error parsing trip: " + words[0] + "," + words[1] + "," + words[2]);
        }
    }
    System.out.println("Done");
}

From source file:com.nextdoor.bender.S3SnsNotifier.java

public static void main(String[] args) throws ParseException, InterruptedException, IOException {
    formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZoneUTC();

    /*/*w  w w . j  a  va2 s .c om*/
     * Parse cli arguments
     */
    Options options = new Options();
    options.addOption(Option.builder().longOpt("bucket").hasArg().required()
            .desc("Name of S3 bucket to list s3 objects from").build());
    options.addOption(Option.builder().longOpt("key-file").hasArg().required()
            .desc("Local file of S3 keys to process").build());
    options.addOption(
            Option.builder().longOpt("sns-arn").hasArg().required().desc("SNS arn to publish to").build());
    options.addOption(Option.builder().longOpt("throttle-ms").hasArg()
            .desc("Amount of ms to wait between publishing to SNS").build());
    options.addOption(Option.builder().longOpt("processed-file").hasArg()
            .desc("Local file to use to store procssed S3 object names").build());
    options.addOption(Option.builder().longOpt("skip-processed").hasArg(false)
            .desc("Whether to skip S3 objects that have been processed").build());
    options.addOption(
            Option.builder().longOpt("dry-run").hasArg(false).desc("If set do not publish to SNS").build());

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

    String bucket = cmd.getOptionValue("bucket");
    String keyFile = cmd.getOptionValue("key-file");
    String snsArn = cmd.getOptionValue("sns-arn");
    String processedFile = cmd.getOptionValue("processed-file", null);
    boolean skipProcessed = cmd.hasOption("skip-processed");
    dryRun = cmd.hasOption("dry-run");
    long throttle = Long.parseLong(cmd.getOptionValue("throttle-ms", "-1"));

    if (processedFile != null) {
        File file = new File(processedFile);

        if (!file.exists()) {
            logger.debug("creating local file to store processed s3 object names: " + processedFile);
            file.createNewFile();
        }
    }

    /*
     * Import S3 keys that have been processed
     */
    if (skipProcessed && processedFile != null) {
        try (BufferedReader br = new BufferedReader(new FileReader(processedFile))) {
            String line;
            while ((line = br.readLine()) != null) {
                alreadyPublished.add(line.trim());
            }
        }
    }

    /*
     * Setup writer for file containing processed S3 keys
     */
    FileWriter fw = null;
    BufferedWriter bw = null;
    if (processedFile != null) {
        fw = new FileWriter(processedFile, true);
        bw = new BufferedWriter(fw);
    }

    /*
     * Create clients
     */
    AmazonS3Client s3Client = new AmazonS3Client();
    AmazonSNSClient snsClient = new AmazonSNSClient();

    /*
     * Get S3 object list
     */
    try (BufferedReader br = new BufferedReader(new FileReader(keyFile))) {
        String line;
        while ((line = br.readLine()) != null) {
            String key = line.trim();

            if (alreadyPublished.contains(key)) {
                logger.info("skipping " + key);
            }

            ObjectMetadata om = s3Client.getObjectMetadata(bucket, key);

            S3EventNotification s3Notification = getS3Notification(key, bucket, om.getContentLength());

            String json = s3Notification.toJson();

            /*
             * Publish to SNS
             */
            if (publish(snsArn, json, snsClient, key) && processedFile != null) {
                bw.write(key + "\n");
                bw.flush();
            }

            if (throttle != -1) {
                Thread.sleep(throttle);
            }

        }
    }

    if (processedFile != null) {
        bw.close();
        fw.close();
    }
}