Example usage for java.io BufferedReader readLine

List of usage examples for java.io BufferedReader readLine

Introduction

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

Prototype

public String readLine() throws IOException 

Source Link

Document

Reads a line of text.

Usage

From source file:RemoveHTMLReader.java

/** The test program: read a text file, strip HTML, print to console */
public static void main(String[] args) {
    try {/*from   w w  w  .j a  va2 s.c om*/
        if (args.length != 1)
            throw new IllegalArgumentException("Wrong number of args");
        // Create a stream to read from the file and strip tags from it
        BufferedReader in = new BufferedReader(new RemoveHTMLReader(new FileReader(args[0])));
        // Read line by line, printing lines to the console
        String line;
        while ((line = in.readLine()) != null)
            System.out.println(line);
        in.close(); // Close the stream.
    } catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java RemoveHTMLReader$Test" + " <filename>");
    }
}

From source file:luceneindexer.Main.java

/**
 * @param args the command line arguments
 *//*from w w  w.  j  a  v  a  2  s  . co m*/
public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Hello world");
    //Open the file of JSON for reading
    FileOpener fOpener = new FileOpener("parks.json");

    //Create the object for writing
    LuceneWriter luceneWriter = new LuceneWriter("indexDir");

    //This is from Jackson which allows for binding the JSON to the Park.java class
    ObjectMapper objectMapper = new ObjectMapper();

    try {

        //first see if we can open a directory for writing
        if (luceneWriter.openIndex()) {
            //get a buffered reader handle to the file
            BufferedReader breader = new BufferedReader(fOpener.getFileForReading());
            String value = null;
            //loop through the file line by line and parse 
            while ((value = breader.readLine()) != null) {
                Park park = objectMapper.readValue(value, Park.class);

                //now submit each park to the lucene writer to add to the index
                luceneWriter.addPark(park);

            }

        } else {
            System.out.println("We had a problem opening the directory for writing");
        }

    } catch (Exception e) {
        System.out.println("Threw exception " + e.getClass() + " :: " + e.getMessage());
    } finally {
        //close out the index and release the lock on the file
        luceneWriter.finish();
    }

}

From source file:de.bitocean.dspm.examples.Vertex.java

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

    Reader reader = new FileReader(GRAPHML_ACTIVITY_DEPENDENCY_FILE);

    BufferedReader br = new BufferedReader(reader);
    while (br.ready()) {
        System.out.println(br.readLine());
    }//from   w  w w  . java 2 s  .  c o m

    Transformer<NodeMetadata, Vertex> vtrans = new Transformer<NodeMetadata, Vertex>() {
        public Vertex transform(NodeMetadata nmd) {
            Vertex v = new Vertex();
            v.id = nmd.getId();
            v.name = nmd.getProperty("name");
            v.expression = nmd.getProperty("expression");
            return v;
        }
    };

    Transformer<EdgeMetadata, Edge> etrans = new Transformer<EdgeMetadata, Edge>() {
        public Edge transform(EdgeMetadata emd) {
            System.out.println(emd.toString());
            Edge e = new Edge();
            try {
                e.source = emd.getSource();
            } catch (Exception ex) {

            }
            e.target = emd.getTarget();
            e.weight = Double.parseDouble(emd.getProperty("weight"));
            return e;
        }
    };

    Transformer<HyperEdgeMetadata, Edge> hetrans = new Transformer<HyperEdgeMetadata, Edge>() {
        public Edge transform(HyperEdgeMetadata emd) {
            Edge e = new Edge();
            try {
                e.source = emd.getProperty("source");
            } catch (Exception ex) {

            }
            e.target = emd.getProperty("target");
            e.weight = Double.parseDouble(emd.getProperty("weight"));
            return e;
        }
    };
    Transformer<GraphMetadata, DirectedSparseGraph<Vertex, Edge>> gtrans = new Transformer<GraphMetadata, DirectedSparseGraph<Vertex, Edge>>() {
        public DirectedSparseGraph<Vertex, Edge> transform(GraphMetadata gmd) {
            return new DirectedSparseGraph<Vertex, Edge>();
        }
    };

    GraphMLReader2<DirectedSparseGraph<Vertex, Edge>, Vertex, Edge> gmlr =
            //new GraphMLReader2<UndirectedSparseGraph<Vertex,Edge>, Vertex, Edge>(fileReader, graphTransformer, vertexTransformer, edgeTransformer, hyperEdgeTransformer)
            new GraphMLReader2<DirectedSparseGraph<Vertex, Edge>, Vertex, Edge>(reader, gtrans, vtrans, etrans,
                    hetrans);

    DirectedSparseGraph<Vertex, Edge> g = gmlr.readGraph();

    System.out.println("Number of vetex: " + g.getVertexCount());
    System.out.println("Number of edges: " + g.getEdgeCount());

    System.out.println("==========================");
    System.out.println("Vertices");
    System.out.println("==========================");
    for (Vertex v : g.getVertices()) {
        System.out.println(v);
    }

    System.out.println("==========================");
    System.out.println("Edges");
    System.out.println("==========================");
    for (Edge e : g.getEdges()) {
        System.out.println(e);
    }

}

From source file:math2605.gn_exp.java

/**
 * @param args the command line arguments
 *///  www  .  java  2s.co  m
public static void main(String[] args) {
    //get file name
    System.out.println("Please enter a file name:");
    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();
    List<String[]> pairs = new ArrayList<>();
    //get coordinate pairs and add to arraylist
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = br.readLine()) != null) {
            String[] pair = line.split(",");
            pairs.add(pair);
        }
        br.close();
    } catch (Exception e) {
    }

    System.out.println("Please enter the value of a:");
    double a = scanner.nextInt();
    System.out.println("Please enter the value of b:");
    double b = scanner.nextInt();
    System.out.println("Please enter the value of c:");
    double c = scanner.nextInt();
    //init B, vector with 3 coordinates
    RealMatrix B = new Array2DRowRealMatrix(3, 1);
    B.setEntry(0, 0, a);
    B.setEntry(1, 0, b);
    B.setEntry(2, 0, c);

    System.out.println("Please enter the number of iteration for the Gauss-newton:");
    //init N, number of iterations
    int N = scanner.nextInt();

    //init r, vector of residuals
    RealMatrix r = new Array2DRowRealMatrix();
    setR(pairs, a, b, c, r);

    //init J, Jacobian of r
    RealMatrix J = new Array2DRowRealMatrix();
    setJ(pairs, a, b, c, r, J);

    System.out.println("J");
    System.out.println(J);
    System.out.println("r");
    System.out.println(r);

    RealMatrix sub = findQR(J, r);

    for (int i = N; i > 0; i--) {
        B = B.subtract(sub);
        double B0 = B.getEntry(0, 0);
        double B1 = B.getEntry(1, 0);
        double B2 = B.getEntry(2, 0);
        //CHANGE ABC TO USE B0, B1, B2
        setR(pairs, B0, B1, B2, r);
        setJ(pairs, B0, B1, B2, r, J);
    }
    System.out.println("B");
    System.out.println(B.toString());

}

From source file:math2605.gn_qua.java

/**
 * @param args the command line arguments
 *//*from   w  w  w .  j a v  a 2  s  .co  m*/
public static void main(String[] args) {
    //get file name
    System.out.println("Please enter a file name:");
    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();
    List<String[]> pairs = new ArrayList<>();
    //get coordinate pairs and add to arraylist
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = br.readLine()) != null) {
            String[] pair = line.split(",");
            pairs.add(pair);
        }
        br.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    System.out.println("Please enter the value of a:");
    double a = scanner.nextInt();
    System.out.println("Please enter the value of b:");
    double b = scanner.nextInt();
    System.out.println("Please enter the value of c:");
    double c = scanner.nextInt();
    //init B, vector with 3 coordinates
    RealMatrix B = new Array2DRowRealMatrix(3, 1);
    B.setEntry(0, 0, a);
    B.setEntry(1, 0, b);
    B.setEntry(2, 0, c);

    System.out.println("Please enter the number of iteration for the Gauss-newton:");
    //init N, number of iterations
    int N = scanner.nextInt();

    //init r, vector of residuals
    RealMatrix r = new Array2DRowRealMatrix(pairs.size(), 1);
    setR(pairs, a, b, c, r);

    //init J, Jacobian of r
    RealMatrix J = new Array2DRowRealMatrix(pairs.size(), 3);
    setJ(pairs, a, b, c, r, J);

    System.out.println("J");
    System.out.println(J);
    System.out.println("r");
    System.out.println(r);

    RealMatrix sub = findQR(J, r);

    for (int i = N; i > 0; i--) {
        B = B.subtract(sub);
        double B0 = B.getEntry(0, 0);
        double B1 = B.getEntry(1, 0);
        double B2 = B.getEntry(2, 0);
        //CHANGE ABC TO USE B0, B1, B2
        setR(pairs, B0, B1, B2, r);
        setJ(pairs, B0, B1, B2, r, J);
    }
    System.out.println("B");
    System.out.println(B.toString());
}

From source file:spatialluceneindexer.Main.java

/**
 * @param args the command line arguments
 *///from w  w  w . ja va 2 s .  co  m
public static void main(String[] args) {

    System.out.println("Starting to create the index");

    //Open the file of JSON for reading
    FileOpener fOpener = new FileOpener("parks.json");

    //Create the object for writing
    LuceneWriter luceneWriter = new LuceneWriter("indexDir");

    //This is from Jackson which allows for binding the JSON to the Park.java class
    ObjectMapper objectMapper = new ObjectMapper();

    try {

        //first see if we can open a directory for writing
        if (luceneWriter.openIndex()) {
            //get a buffered reader handle to the file
            BufferedReader breader = new BufferedReader(fOpener.getFileForReading());
            String value = null;
            //loop through the file line by line and parse 
            while ((value = breader.readLine()) != null) {
                Park park = objectMapper.readValue(value, Park.class);

                //now submit each park to the lucene writer to add to the index
                luceneWriter.addPark(park);

            }

        } else {
            System.out.println("We had a problem opening the directory for writing");
        }

    } catch (Exception e) {
        System.out.println("Threw exception " + e.getClass() + " :: " + e.getMessage());
    } finally {
        luceneWriter.finish();
    }

    System.out.println("Finished created the index");
}

From source file:httpclient.UploadAction.java

public static void main(String[] args) throws FileNotFoundException {
    File targetFile1 = new File("F:\\2.jpg");
    // File targetFile2 = new File("F:\\1.jpg");
    FileInputStream fis1 = new FileInputStream(targetFile1);
    // FileInputStream fis2 = new FileInputStream(targetFile2);
    // String targetURL =
    // "http://static.fangbiandian.com.cn/round_server/upload/uploadFile.do";
    String targetURL = "http://www.fangbiandian.com.cn/round_server/user/updateUserInfo.do";
    HttpPost filePost = new HttpPost(targetURL);
    try {/*from   ww w . j  av a  2  s. c o m*/
        // ?????
        HttpClient client = new DefaultHttpClient();
        // FormBodyPart fbp1 = new FormBodyPart("file1", new
        // FileBody(targetFile1));
        // FormBodyPart fbp2 = new FormBodyPart("file2", new
        // FileBody(targetFile2));
        // FormBodyPart fbp3 = new FormBodyPart("file3", new
        // FileBody(targetFile3));
        // List<FormBodyPart> picList = new ArrayList<FormBodyPart>();
        // picList.add(fbp1);
        // picList.add(fbp2);
        // picList.add(fbp3);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("userId", "65478A5CD8D20C3807EE16CF22AF8A17");
        Map<String, Object> map = new HashMap<String, Object>();
        String jsonStr = JSON.toJSONString(paramMap);
        map.put("cid", 321);
        map.put("request", jsonStr);
        String jsonString = JSON.toJSONString(map);
        MultipartEntity multiEntity = new MultipartEntity();
        Charset charset = Charset.forName("UTF-8");
        multiEntity.addPart("request", new StringBody(jsonString, charset));
        multiEntity.addPart("photo", new InputStreamBody(fis1, "2.jpg"));
        // multiEntity.addPart("licenseUrl", new InputStreamBody(fis2,
        // "1.jpg"));
        filePost.setEntity(multiEntity);
        HttpResponse response = client.execute(filePost);
        int code = response.getStatusLine().getStatusCode();
        System.out.println(code);
        if (HttpStatus.SC_OK == code) {
            System.out.println("?");
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    // do something useful
                    BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                    String str = null;
                    while ((str = reader.readLine()) != null) {
                        System.out.println(str);
                    }
                } finally {
                    instream.close();
                }
            }
        } else {
            System.out.println("");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        filePost.releaseConnection();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*from  w  ww.j  a  v  a 2 s. c o m*/
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        FileInputStream fis = new FileInputStream("sometextfile.txt");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement createTable = connection.createStatement();
        createTable.executeUpdate("CREATE TABLE source_code (name char(20), source LONGTEXT)");
        String ins = "INSERT INTO source_code VALUES(?,?)";
        PreparedStatement statement = connection.prepareStatement(ins);

        statement.setString(1, "TryInputStream2");
        statement.setAsciiStream(2, fis, fis.available());

        int rowsUpdated = statement.executeUpdate();
        System.out.println("Rows affected: " + rowsUpdated);
        Statement getCode = connection.createStatement();
        ResultSet theCode = getCode.executeQuery("SELECT name,source FROM source_code");
        BufferedReader reader = null;
        String input = null;

        while (theCode.next()) {
            reader = new BufferedReader(new InputStreamReader(theCode.getAsciiStream(2)));
            while ((input = reader.readLine()) != null) {
                System.out.println(input);
            }
        }
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket socket = new Socket("localhost", 12900);
    System.out.println("Started client  socket at " + socket.getLocalSocketAddress());
    BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

    String promptMsg = "Please enter a  message  (Bye  to quit):";
    String outMsg = null;//  w  ww  .  j av  a  2s  . c  o  m

    System.out.print(promptMsg);
    while ((outMsg = consoleReader.readLine()) != null) {
        if (outMsg.equalsIgnoreCase("bye")) {
            break;
        }
        // Add a new line to the message to the server,
        // because the server reads one line at a time.
        socketWriter.write(outMsg);
        socketWriter.write("\n");
        socketWriter.flush();

        // Read and display the message from the server
        String inMsg = socketReader.readLine();
        System.out.println("Server: " + inMsg);
        System.out.println(); // Print a blank line
        System.out.print(promptMsg);
    }
    socket.close();
}

From source file:cc.twittertools.util.VerifySubcollection.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("source collection directory")
            .create(COLLECTION_OPTION));
    options.addOption(//ww w  . j av a 2 s. co  m
            OptionBuilder.withArgName("file").hasArg().withDescription("list of tweetids").create(ID_OPTION));

    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }

    if (!cmdline.hasOption(COLLECTION_OPTION) || !cmdline.hasOption(ID_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(ExtractSubcollection.class.getName(), options);
        System.exit(-1);
    }

    String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION);

    LongOpenHashSet tweetids = new LongOpenHashSet();
    File tweetidsFile = new File(cmdline.getOptionValue(ID_OPTION));
    if (!tweetidsFile.exists()) {
        System.err.println("Error: " + tweetidsFile + " does not exist!");
        System.exit(-1);
    }
    LOG.info("Reading tweetids from " + tweetidsFile);

    FileInputStream fin = new FileInputStream(tweetidsFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(fin));

    String s;
    while ((s = br.readLine()) != null) {
        tweetids.add(Long.parseLong(s));
    }
    br.close();
    fin.close();
    LOG.info("Read " + tweetids.size() + " tweetids.");

    File file = new File(collectionPath);
    if (!file.exists()) {
        System.err.println("Error: " + file + " does not exist!");
        System.exit(-1);
    }

    LongOpenHashSet seen = new LongOpenHashSet();
    TreeMap<Long, String> tweets = Maps.newTreeMap();

    PrintStream out = new PrintStream(System.out, true, "UTF-8");
    StatusStream stream = new JsonStatusCorpusReader(file);
    Status status;
    int cnt = 0;
    while ((status = stream.next()) != null) {
        if (!tweetids.contains(status.getId())) {
            LOG.error("tweetid " + status.getId() + " doesn't belong in collection");
            continue;
        }
        if (seen.contains(status.getId())) {
            LOG.error("tweetid " + status.getId() + " already seen!");
            continue;
        }

        tweets.put(status.getId(), status.getJsonObject().toString());
        seen.add(status.getId());
        cnt++;
    }
    LOG.info("total of " + cnt + " tweets in subcollection.");

    for (Map.Entry<Long, String> entry : tweets.entrySet()) {
        out.println(entry.getValue());
    }

    stream.close();
    out.close();
}