Example usage for java.nio.file Files readAllLines

List of usage examples for java.nio.file Files readAllLines

Introduction

In this page you can find the example usage for java.nio.file Files readAllLines.

Prototype

public static List<String> readAllLines(Path path) throws IOException 

Source Link

Document

Read all lines from a file.

Usage

From source file:org.beryx.viewreka.fxapp.Viewreka.java

private static boolean isProbablyBinary(File file) {
    try {/*from w w w . j a v  a 2s  .co m*/
        List<String> lines = Files.readAllLines(Paths.get(file.getAbsolutePath()));
        if (!lines.isEmpty()) {
            long binCharCount = 0;
            long binLineCount = 0;
            for (String line : lines) {
                if (CONTROL_PATTERN.matcher(line).matches())
                    return true;
                if (line.chars().filter(ch -> !Character.isDefined(ch)).findFirst().isPresent())
                    return true;
                long count = line.chars().filter(ch -> (ch > 127)).count();
                if (count > 0) {
                    binCharCount += count;
                    binLineCount++;
                }
            }
            if (binCharCount == 0 || binLineCount == 0)
                return false;
            long length = file.length();
            if (length < 100) {
                if (binCharCount > 10)
                    return true;
            } else {
                if (100.0 * binCharCount / length > 10)
                    return true;
                if (100.0 * binLineCount / lines.size() > 50)
                    return true;
            }
        }
    } catch (Throwable t) {
        log.error("An error occurred while analyzing the file", t);
        return true;
    }
    return false;
}

From source file:ca.osmcanada.osvuploadr.JPMain.java

private void jbUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbUploadActionPerformed
    String path = ca.osmcanada.osvuploadr.JFMain.class.getProtectionDomain().getCodeSource().getLocation()
            .getPath();//from  w  ww  .  ja  va  2s  .c  o m
    String decodedPath = "";
    try {
        decodedPath = new File(URLDecoder.decode(path, "UTF-8")).getParentFile().getPath();
    } catch (Exception ex) {
        Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, "decodePath", ex);
    }
    File id = new File(decodedPath + "/access_token.txt");
    String accessToken = "";
    System.out.println("id_file exists:" + id.exists());
    if (!id.exists()) {
        try {
            String[] buttons = { new String(r.getString("automatically").getBytes(), "UTF-8"),
                    new String(r.getString("manually").getBytes(), "UTF-8"),
                    new String(r.getString("cancel").getBytes(), "UTF-8") };
            int rc = JOptionPane.showOptionDialog(null,
                    new String(r.getString("login_to_osm").getBytes(), "UTF-8"),
                    new String(r.getString("confirmation").getBytes(), "UTF-8"),
                    JOptionPane.INFORMATION_MESSAGE, 0, null, buttons, buttons[0]);
            String token = "";
            System.out.println("GetOSMUser");
            switch (rc) {
            case 0:
                String usr = "";
                String psw = "";
                JTextField tf = new JTextField();
                JPasswordField pf = new JPasswordField();
                rc = JOptionPane.showConfirmDialog(null, tf,
                        new String(r.getString("email_osm_usr").getBytes(), "UTF-8"),
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
                if (rc == JOptionPane.OK_OPTION) {
                    usr = tf.getText();
                } else {
                    return;
                }

                rc = JOptionPane.showConfirmDialog(null, pf,
                        new String(r.getString("enter_password").getBytes(), "UTF-8"),
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
                if (rc == JOptionPane.OK_OPTION) {
                    psw = new String(pf.getPassword());
                } else {
                    return;
                }
                token = GetOSMUser(usr, psw);
                break;
            case 1:
                token = GetOSMUser();
                break;
            case 2:
                return;

            }
            Path targetPath = Paths.get("./access_token.txt");
            byte[] bytes = token.split("\\|")[0].getBytes(StandardCharsets.UTF_8);
            Files.write(targetPath, bytes, StandardOpenOption.CREATE);
            accessToken = token.split("\\|")[0];
            String accessSecret = token.split("\\|")[1];
            SendAuthTokens(accessToken, accessSecret);
        } catch (Exception ex) {
            Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, "GetOSMUser", ex);
        }
    } else {
        try {
            List<String> token = Files.readAllLines(Paths.get(id.getPath()));
            if (token.size() > 0) {
                accessToken = token.get(0); //read first line
            }
        } catch (Exception ex) {
            Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, "readAllLines", ex);
        }
    }
    System.out.println("Access Token obtained from file or OSM:" + accessToken);

    //Start processing list
    for (String item : listDir.getItems()) {
        System.out.println("Processing folder:" + item);
        Process(item, accessToken);
    }
    //um = new UploadManager(listDir.getItems());
    //um.start();

}

From source file:gov.anl.cue.arcane.engine.matrix.MatrixModel.java

/**
 * Export solid write scad edge identifiers.
 *
 * @param graph the graph//from w ww  .  ja va 2  s . c o  m
 * @param edgeCount the edge count
 * @param writer the writer
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void exportSolidWriteSCADEdgeIdentifiers(Graph<String, String> graph, int edgeCount, PrintWriter writer)
        throws IOException {

    // Read the boilerplate SCAD segment.
    List<String> c = Files.readAllLines(
            Paths.get(Util.INPUT_DIR + "//" + Util.INPUT_SCAD_DIR + "//" + MatrixModel.SCAD_INPUT_FILE_C));
    // Read the boilerplate SCAD segments.
    List<String> d = Files.readAllLines(
            Paths.get(Util.INPUT_DIR + "//" + Util.INPUT_SCAD_DIR + "//" + MatrixModel.SCAD_INPUT_FILE_D));

    // Write out a boilerplate SCAD segment.
    for (String line : c)
        writer.println(line);

    // Write out the edge identifiers.
    for (int edgeCounter = 1; edgeCounter <= edgeCount; edgeCounter++) {
        Pair<String> pair = graph.getEndpoints("" + edgeCounter);
        writer.print("[" + edgeCounter + ", " + pair.getFirst() + ", " + pair.getSecond() + "]");
        if (edgeCounter < edgeCount) {
            writer.println(", ");
        } else {
            writer.println("");
        }
    }
    for (String line : d)
        writer.println(line);

}

From source file:gov.anl.cue.arcane.engine.matrix.MatrixModel.java

/**
 * Export solid write scad node identifiers.
 *
 * @param writer the writer//from  ww  w.ja  va 2 s . c om
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void exportSolidWriteSCADNodeIdentifiers(PrintWriter writer) throws IOException {

    // Read the boilerplate SCAD segment.
    List<String> b = Files.readAllLines(
            Paths.get(Util.INPUT_DIR + "//" + Util.INPUT_SCAD_DIR + "//" + MatrixModel.SCAD_INPUT_FILE_B));

    // Write out a boilerplate SCAD segment.
    for (String line : b)
        writer.println(line);

    // Write out the node identifiers.
    for (int source = 0; source < this.nodeCount(); source++) {
        writer.print("[" + source + ", " + source + "]");
        if (source < (this.nodeCount() - 1)) {
            writer.println(", ");
        } else {
            writer.println("");
        }
    }

}

From source file:gov.anl.cue.arcane.engine.matrix.MatrixModel.java

/**
 * Export solid write scad node coordinates.
 *
 * @param layout the layout//from   w  w w  . j  av a2s .  co m
 * @param minimum the minimum
 * @param maximum the maximum
 * @param writer the writer
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void exportSolidWriteSCADNodeCoordinates(CircleLayout<String, String> layout, double minimum,
        double maximum, PrintWriter writer) throws IOException {

    // Read the boilerplate SCAD segment.
    List<String> a = Files.readAllLines(
            Paths.get(Util.INPUT_DIR + "//" + Util.INPUT_SCAD_DIR + "//" + MatrixModel.SCAD_INPUT_FILE_A));

    // Write out a boilerplate SCAD segment.
    for (String line : a)
        writer.println(line);

    // Write out the node coordinates.
    for (int source = 0; source < this.nodeCount(); source++) {
        writer.print("[" + layout.getX("" + source) + ", " + layout.getY("" + source) + ", "
                + this.getMatrixEngine().getRandomNumberFromTo(minimum, maximum) + "]");
        if (source < (this.nodeCount() - 1)) {
            writer.println(", ");
        } else {
            writer.println("");
        }
    }

}