Example usage for java.nio.file StandardOpenOption APPEND

List of usage examples for java.nio.file StandardOpenOption APPEND

Introduction

In this page you can find the example usage for java.nio.file StandardOpenOption APPEND.

Prototype

StandardOpenOption APPEND

To view the source code for java.nio.file StandardOpenOption APPEND.

Click Source Link

Document

If the file is opened for #WRITE access then bytes will be written to the end of the file rather than the beginning.

Usage

From source file:org.clipsmonitor.monitor2015.RescueGenMap.java

/**
 * Salva sul json passato in input le informazioni relative all'agente 
 * da utilizzare per ricaricare la scena
 * @param json //from w w  w  .  j av  a  2s.c  om
 */

public boolean SaveJsonRobotParams(File json) {
    try {
        JSONObject Info = new JSONObject();
        Info.put("robot_x", agentposition[0]);
        Info.put("robot_y", agentposition[1]);
        Info.put("robot_x_default", agentposition[0]);
        Info.put("robot_y_default", agentposition[1]);
        Info.put("robot_direction", direction);
        Info.put("robot_loaded", loaded);
        Files.write(Paths.get(json.getAbsolutePath()), Info.toString(2).getBytes(), StandardOpenOption.APPEND);
        return true;
    } catch (JSONException ex) {
        AppendLogMessage(ex.getMessage(), "error");
        return false;

    } catch (NumberFormatException ex) {
        AppendLogMessage(ex.getMessage(), "error");
        return false;
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
        return false;
    }
}

From source file:com.liferay.blade.cli.command.CreateCommandTest.java

@Test
public void testCreateWorkspaceCommaDelimitedModulesDirGradleProject() throws Exception {
    File workspace = new File(_rootDir, "workspace");

    _makeWorkspace(workspace);/* www.j  av a  2s.c  o  m*/

    File gradleProperties = new File(workspace, "gradle.properties");

    Assert.assertTrue(gradleProperties.exists());

    String configLine = System.lineSeparator() + "liferay.workspace.modules.dir=modules,foo,bar";

    Files.write(gradleProperties.toPath(), configLine.getBytes(), StandardOpenOption.APPEND);

    String[] args = { "create", "-t", "rest", "--base", workspace.getAbsolutePath(), "resttest" };

    TestUtil.runBlade(workspace, _extensionsDir, args);

    String fooBar = workspace.getAbsolutePath() + "/modules,foo,bar";

    File fooBarDir = new File(fooBar);

    Assert.assertFalse("directory named '" + fooBarDir.getName() + "' should not exist, but it does.",
            fooBarDir.exists());
}

From source file:org.wildfly.security.tool.FileSystemRealmCommand.java

/**
 * Creates the script/commands the user must run for Elytron to recognize
 * and use the new filesystem-realm//from  www .j ava 2 s .co  m
 */
private void createWildFlyScript() throws Exception {
    for (Descriptor descriptor : descriptors) {
        String usersFile = descriptor.getUsersFile();
        if (descriptor.getUsersFile() == null || descriptor.getRolesFile() == null
                || descriptor.getOutputLocation() == null) {
            continue;
        }
        String fileSystemRealmName = descriptor.getFileSystemRealmName();
        if (fileSystemRealmName == null || fileSystemRealmName.isEmpty()) {
            warningHandler(String.format(
                    "No name provided for filesystem-realm, using default filesystem-realm name for %s.",
                    usersFile));
            descriptor.setFileSystemRealmName(DEFAULT_FILESYSTEM_REALM_NAME);
            fileSystemRealmName = DEFAULT_FILESYSTEM_REALM_NAME;
        }
        String outputLocation = descriptor.getOutputLocation();
        String securityDomainName = descriptor.getSecurityDomainName();

        String createScriptCheck = "";
        if (Paths.get(String.format("%s.sh", fileSystemRealmName)).toFile().exists()) {
            createScriptCheck = prompt(false, null, false, ElytronToolMessages.msg
                    .shouldFileBeOverwritten(String.format("%s.sh", fileSystemRealmName)));
        }
        String fullOutputPath;
        if (outputLocation.startsWith(".")) {
            fullOutputPath = Paths.get(outputLocation.substring(2, outputLocation.length())).toAbsolutePath()
                    .toString();
        } else {
            fullOutputPath = Paths.get(outputLocation).toAbsolutePath().toString();
        }

        if (summaryMode) {
            summaryString.append(String.format("Configured script for WildFly named %s.sh at %s.",
                    fileSystemRealmName, fullOutputPath));
            summaryString.append(System.getProperty("line.separator"));
            summaryString.append("The script is using the following names:");
            summaryString.append(System.getProperty("line.separator"));
            summaryString.append(String.format("Name of filesystem-realm: %s", fileSystemRealmName));
            summaryString.append(System.getProperty("line.separator"));
        }

        if (securityDomainName != null && !securityDomainName.isEmpty()) {
            if (summaryMode) {
                summaryString.append(String.format("Name of security-domain: %s", securityDomainName));
                summaryString.append(System.getProperty("line.separator"));
            }
        } else {
            warningHandler(String.format(
                    "No name provided for security-domain, using default security-domain name for %s.",
                    usersFile));
            securityDomainName = DEFAULT_SECURITY_DOMAIN_NAME;
        }

        List<String> scriptLines = Arrays.asList(
                String.format("/subsystem=elytron/filesystem-realm=%s:add(path=%s)", fileSystemRealmName,
                        fullOutputPath),
                String.format(
                        "/subsystem=elytron/security-domain=%1$s:add(realms=[{realm=%2$s}],default-realm=%2$s,permission-mapper=default-permission-mapper)",
                        securityDomainName, fileSystemRealmName));

        if (!createScriptCheck.equals("y") && !createScriptCheck.equals("yes")) {
            Files.write(Paths.get(String.format("%s/%s.sh", outputLocation, fileSystemRealmName)), scriptLines,
                    StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
        } else {
            Files.write(Paths.get(String.format("%s/%s.sh", outputLocation, fileSystemRealmName)), scriptLines,
                    StandardOpenOption.APPEND);
        }
    }
}

From source file:com.streamsets.pipeline.stage.origin.logtail.TestFileTailSource.java

@Test
public void testOffsetLagMetric() throws Exception {
    final File testDataDir = new File("target", UUID.randomUUID().toString());

    Assert.assertTrue(testDataDir.mkdirs());

    final File file = new File(testDataDir, "file.txt-1");

    Files.write(file.toPath(), Arrays.asList("A", "B", "C"), StandardCharsets.UTF_8);

    FileInfo fileInfo = new FileInfo();
    fileInfo.fileFullPath = testDataDir.getAbsolutePath() + "/file.txt-1";
    fileInfo.fileRollMode = FileRollMode.ALPHABETICAL;
    fileInfo.firstFile = "";
    fileInfo.patternForToken = ".*";

    FileTailConfigBean conf = new FileTailConfigBean();
    conf.dataFormat = DataFormat.TEXT;/*from w  w w  . ja  v  a  2s.c o  m*/
    conf.multiLineMainPattern = "";
    conf.batchSize = 25;
    conf.maxWaitTimeSecs = 1;
    conf.fileInfos = Arrays.asList(fileInfo);
    conf.postProcessing = PostProcessingOptions.NONE;
    conf.dataFormatConfig.textMaxLineLen = 1024;

    FileTailSource source = PowerMockito.spy(new FileTailSource(conf, SCAN_INTERVAL));

    //Intercept getOffsetsLag private method which calculates the offsetLag
    //in the files and write some data to file so there is an offset lag.
    PowerMockito.replace(MemberMatcher.method(FileTailSource.class, "calculateOffsetLagMetric", Map.class))
            .with(new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    //This will add 6 more (D, E, F) to the file and move the file size to 12 bytes
                    //but we would have read just 6 bytes (A, B, C).
                    Files.write(file.toPath(), Arrays.asList("D", "E", "F"), StandardCharsets.UTF_8,
                            StandardOpenOption.APPEND);
                    //call the real getOffsetsLag private method
                    return method.invoke(proxy, args);
                }
            });

    SourceRunner runner = createRunner(source);
    try {
        runner.runInit();

        StageRunner.Output output = runner.runProduce(null, 10);

        // Make sure there are only three records.
        Assert.assertEquals(3, output.getRecords().get("lane").size());

        Map<String, Counter> offsetLag = (Map<String, Counter>) Whitebox.getInternalState(source,
                "offsetLagMetric");
        Assert.assertEquals(6L, offsetLag.get(file.getAbsolutePath() + "||" + ".*").getCount());
    } finally {
        runner.runDestroy();
    }
}

From source file:io.minio.MinioClient.java

/**
 * Gets object's data in the given bucket and stores it to given file name.
 *
 * </p><b>Example:</b><br>
 * <pre>{@code minioClient.getObject("my-bucketname", "my-objectname", "photo.jpg"); }</pre>
 *
 * @param bucketName  Bucket name.//w w w .ja  v  a2  s . c o m
 * @param objectName  Object name in the bucket.
 * @param fileName    file name.
 *
 * @throws InvalidBucketNameException  upon invalid bucket name is given
 * @throws NoResponseException         upon no response from server
 * @throws IOException                 upon connection error
 * @throws XmlPullParserException      upon parsing response xml
 * @throws ErrorResponseException      upon unsuccessful execution
 * @throws InternalException           upon internal library error
 */
public void getObject(String bucketName, String objectName, String fileName)
        throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
        InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
        InternalException, InvalidArgumentException {
    Path filePath = Paths.get(fileName);
    boolean fileExists = Files.exists(filePath);

    if (fileExists && !Files.isRegularFile(filePath)) {
        throw new InvalidArgumentException(fileName + ": not a regular file");
    }

    ObjectStat objectStat = statObject(bucketName, objectName);
    long length = objectStat.length();
    String etag = objectStat.etag();

    String tempFileName = fileName + "." + etag + ".part.minio";
    Path tempFilePath = Paths.get(tempFileName);
    boolean tempFileExists = Files.exists(tempFilePath);

    if (tempFileExists && !Files.isRegularFile(tempFilePath)) {
        throw new IOException(tempFileName + ": not a regular file");
    }

    long tempFileSize = 0;
    if (tempFileExists) {
        tempFileSize = Files.size(tempFilePath);
        if (tempFileSize > length) {
            Files.delete(tempFilePath);
            tempFileExists = false;
            tempFileSize = 0;
        }
    }

    if (fileExists) {
        long fileSize = Files.size(filePath);
        if (fileSize == length) {
            // already downloaded. nothing to do
            return;
        } else if (fileSize > length) {
            throw new InvalidArgumentException(
                    "'" + fileName + "': object size " + length + " is smaller than file size " + fileSize);
        } else if (!tempFileExists) {
            // before resuming the download, copy filename to tempfilename
            Files.copy(filePath, tempFilePath);
            tempFileSize = fileSize;
            tempFileExists = true;
        }
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = getObject(bucketName, objectName, tempFileSize);
        os = Files.newOutputStream(tempFilePath, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        long bytesWritten = ByteStreams.copy(is, os);
        is.close();
        os.close();

        if (bytesWritten != length - tempFileSize) {
            throw new IOException(tempFileName + ": unexpected data written.  expected = "
                    + (length - tempFileSize) + ", written = " + bytesWritten);
        }

        Files.move(tempFilePath, filePath, StandardCopyOption.REPLACE_EXISTING);
    } finally {
        if (is != null) {
            is.close();
        }
        if (os != null) {
            os.close();
        }
    }
}

From source file:org.apache.hadoop.hive.ql.QTestUtil.java

public static void setupMetaStoreTableColumnStatsFor30TBTPCDSWorkload(HiveConf conf) {
    Connection conn = null;//from  w  w  w.  j a  v a2s .com
    ArrayList<Statement> statements = new ArrayList<Statement>(); // list of Statements, PreparedStatements

    try {
        Properties props = new Properties(); // connection properties
        props.put("user", conf.get("javax.jdo.option.ConnectionUserName"));
        props.put("password", conf.get("javax.jdo.option.ConnectionPassword"));
        conn = DriverManager.getConnection(conf.get("javax.jdo.option.ConnectionURL"), props);
        ResultSet rs = null;
        Statement s = conn.createStatement();

        if (LOG.isDebugEnabled()) {
            LOG.debug("Connected to metastore database ");
        }

        String mdbPath = AbstractCliConfig.HIVE_ROOT + "/data/files/tpcds-perf/metastore_export/";

        // Setup the table column stats
        BufferedReader br = new BufferedReader(new FileReader(new File(
                AbstractCliConfig.HIVE_ROOT + "/metastore/scripts/upgrade/derby/022-HIVE-11107.derby.sql")));
        String command;

        s.execute("DROP TABLE APP.TABLE_PARAMS");
        s.execute("DROP TABLE APP.TAB_COL_STATS");
        // Create the column stats table
        while ((command = br.readLine()) != null) {
            if (!command.endsWith(";")) {
                continue;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Going to run command : " + command);
            }
            try {
                PreparedStatement psCommand = conn.prepareStatement(command.substring(0, command.length() - 1));
                statements.add(psCommand);
                psCommand.execute();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("successfully completed " + command);
                }
            } catch (SQLException e) {
                LOG.info("Got SQL Exception " + e.getMessage());
            }
        }
        br.close();

        java.nio.file.Path tabColStatsCsv = FileSystems.getDefault().getPath(mdbPath, "csv",
                "TAB_COL_STATS.txt.bz2");
        java.nio.file.Path tabParamsCsv = FileSystems.getDefault().getPath(mdbPath, "csv",
                "TABLE_PARAMS.txt.bz2");

        // Set up the foreign key constraints properly in the TAB_COL_STATS data
        String tmpBaseDir = System.getProperty(TEST_TMP_DIR_PROPERTY);
        java.nio.file.Path tmpFileLoc1 = FileSystems.getDefault().getPath(tmpBaseDir, "TAB_COL_STATS.txt");
        java.nio.file.Path tmpFileLoc2 = FileSystems.getDefault().getPath(tmpBaseDir, "TABLE_PARAMS.txt");

        class MyComp implements Comparator<String> {
            @Override
            public int compare(String str1, String str2) {
                if (str2.length() != str1.length()) {
                    return str2.length() - str1.length();
                }
                return str1.compareTo(str2);
            }
        }

        final SortedMap<String, Integer> tableNameToID = new TreeMap<String, Integer>(new MyComp());

        rs = s.executeQuery("SELECT * FROM APP.TBLS");
        while (rs.next()) {
            String tblName = rs.getString("TBL_NAME");
            Integer tblId = rs.getInt("TBL_ID");
            tableNameToID.put(tblName, tblId);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Resultset : " + tblName + " | " + tblId);
            }
        }

        final Map<String, Map<String, String>> data = new HashMap<>();
        rs = s.executeQuery("select TBLS.TBL_NAME, a.COLUMN_NAME, a.TYPE_NAME from  "
                + "(select COLUMN_NAME, TYPE_NAME, SDS.SD_ID from APP.COLUMNS_V2 join APP.SDS on SDS.CD_ID = COLUMNS_V2.CD_ID) a"
                + " join APP.TBLS on  TBLS.SD_ID = a.SD_ID");
        while (rs.next()) {
            String tblName = rs.getString(1);
            String colName = rs.getString(2);
            String typeName = rs.getString(3);
            Map<String, String> cols = data.get(tblName);
            if (null == cols) {
                cols = new HashMap<>();
            }
            cols.put(colName, typeName);
            data.put(tblName, cols);
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new BZip2CompressorInputStream(Files.newInputStream(tabColStatsCsv, StandardOpenOption.READ))));

        Stream<String> replaced = reader.lines().parallel().map(str -> {
            String[] splits = str.split(",");
            String tblName = splits[0];
            String colName = splits[1];
            Integer tblID = tableNameToID.get(tblName);
            StringBuilder sb = new StringBuilder(
                    "default@" + tblName + "@" + colName + "@" + data.get(tblName).get(colName) + "@");
            for (int i = 2; i < splits.length; i++) {
                sb.append(splits[i] + "@");
            }
            // Add tbl_id and empty bitvector
            return sb.append(tblID).append("@").toString();
        });

        Files.write(tmpFileLoc1, (Iterable<String>) replaced::iterator);
        replaced.close();
        reader.close();

        BufferedReader reader2 = new BufferedReader(new InputStreamReader(
                new BZip2CompressorInputStream(Files.newInputStream(tabParamsCsv, StandardOpenOption.READ))));
        final Map<String, String> colStats = new ConcurrentHashMap<>();
        Stream<String> replacedStream = reader2.lines().parallel().map(str -> {
            String[] splits = str.split("_@");
            String tblName = splits[0];
            Integer tblId = tableNameToID.get(tblName);
            Map<String, String> cols = data.get(tblName);
            StringBuilder sb = new StringBuilder();
            sb.append("{\"COLUMN_STATS\":{");
            for (String colName : cols.keySet()) {
                sb.append("\"" + colName + "\":\"true\",");
            }
            sb.append("},\"BASIC_STATS\":\"true\"}");
            colStats.put(tblId.toString(), sb.toString());

            return tblId.toString() + "@" + splits[1];
        });

        Files.write(tmpFileLoc2, (Iterable<String>) replacedStream::iterator);
        Files.write(tmpFileLoc2,
                (Iterable<String>) colStats.entrySet().stream()
                        .map(map -> map.getKey() + "@COLUMN_STATS_ACCURATE@" + map.getValue())::iterator,
                StandardOpenOption.APPEND);

        replacedStream.close();
        reader2.close();
        // Load the column stats and table params with 30 TB scale
        String importStatement1 = "CALL SYSCS_UTIL.SYSCS_IMPORT_TABLE(null, '" + "TAB_COL_STATS" + "', '"
                + tmpFileLoc1.toAbsolutePath().toString() + "', '@', null, 'UTF-8', 1)";
        String importStatement2 = "CALL SYSCS_UTIL.SYSCS_IMPORT_TABLE(null, '" + "TABLE_PARAMS" + "', '"
                + tmpFileLoc2.toAbsolutePath().toString() + "', '@', null, 'UTF-8', 1)";
        try {
            PreparedStatement psImport1 = conn.prepareStatement(importStatement1);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Going to execute : " + importStatement1);
            }
            statements.add(psImport1);
            psImport1.execute();
            if (LOG.isDebugEnabled()) {
                LOG.debug("successfully completed " + importStatement1);
            }
            PreparedStatement psImport2 = conn.prepareStatement(importStatement2);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Going to execute : " + importStatement2);
            }
            statements.add(psImport2);
            psImport2.execute();
            if (LOG.isDebugEnabled()) {
                LOG.debug("successfully completed " + importStatement2);
            }
        } catch (SQLException e) {
            LOG.info("Got SQL Exception  " + e.getMessage());
        }
    } catch (FileNotFoundException e1) {
        LOG.info("Got File not found Exception " + e1.getMessage());
    } catch (IOException e1) {
        LOG.info("Got IOException " + e1.getMessage());
    } catch (SQLException e1) {
        LOG.info("Got SQLException " + e1.getMessage());
    } finally {
        // Statements and PreparedStatements
        int i = 0;
        while (!statements.isEmpty()) {
            // PreparedStatement extend Statement
            Statement st = statements.remove(i);
            try {
                if (st != null) {
                    st.close();
                    st = null;
                }
            } catch (SQLException sqle) {
            }
        }

        //Connection
        try {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (SQLException sqle) {
        }
    }
}

From source file:me.mast3rplan.phantombot.PhantomBot.java

public void loadGameList(DataStore dataStore) {
    try {//w w  w .ja  v a2  s.  c  om
        if (new File("./conf/game_list.txt").exists()) {
            long lastModified = new File("./conf/game_list.txt").lastModified();
            long dbLastModified = dataStore.GetLong("settings", "", "gameListModified");
            if (lastModified > dbLastModified) {
                print("Processing New Game List File");

                print("Loading into database...");
                String data = FileUtils.readFileToString(new File("./conf/game_list.txt"));
                String[] lines = data.replaceAll("\\r", "").split("\\n");
                dataStore.setbatch("gamelist", lines, lines);

                print("Creating cache file for Control Panel...");

                if (!new File("./web/panel/js").exists())
                    new File("./web/panel/js").mkdirs();

                String string = "// PhantomBot Control Panel Game List\r\n// Generated by PhantomBot Core\r\n\r\n$.gamesList = [";
                Files.write(Paths.get("./web/panel/js/games.js"), string.getBytes(StandardCharsets.UTF_8),
                        StandardOpenOption.CREATE, StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING);

                for (String line : lines) {
                    string = "'" + line.replace("'", "") + "', \r\n";
                    Files.write(Paths.get("./web/panel/js/games.js"), string.getBytes(StandardCharsets.UTF_8),
                            StandardOpenOption.APPEND);
                    string = "";
                }

                string += "];";
                Files.write(Paths.get("./web/panel/js/games.js"), string.getBytes(StandardCharsets.UTF_8),
                        StandardOpenOption.APPEND);
                print("Completed Processing.");

                dataStore.SetLong("settings", "", "gameListModified", lastModified);
            }
        }
    } catch (IOException ex) {
        com.gmt2001.Console.err.printStackTrace(ex);
    }
}

From source file:org.neo4j.io.pagecache.PageCacheTest.java

@Test(timeout = SEMI_LONG_TIMEOUT_MILLIS)
public void mustIgnoreCertainOpenOptions() throws Exception {
    getPageCache(fs, maxPages, pageCachePageSize, PageCacheTracer.NULL);
    try (PagedFile pf = pageCache.map(file("a"), filePageSize, StandardOpenOption.READ,
            StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.SPARSE);
            PageCursor cursor = pf.io(0, PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());/*from w  w  w .  java  2s.  c om*/
    }
}