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:MainClass.java

public static void main(String args[]) throws Exception {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    ServerSocket ss = ssf.createServerSocket(443);
    while (true) {
        Socket s = ss.accept();// www  . j  ava 2s  .  co m
        PrintStream out = new PrintStream(s.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String info = null;
        String request = null;
        String refer = null;

        while ((info = in.readLine()) != null) {
            if (info.startsWith("GET")) {
                request = info;
            }
            if (info.startsWith("Referer:")) {
                refer = info;
            }
            if (info.equals(""))
                break;
        }
        if (request != null) {
            out.println("HTTP/1.0 200 OK\nMIME_version:1.0\nContent_Type:text/html");
            int sp1 = request.indexOf(' ');
            int sp2 = request.indexOf(' ', sp1 + 1);
            String filename = request.substring(sp1 + 2, sp2);
            if (refer != null) {
                sp1 = refer.indexOf(' ');
                refer = refer.substring(sp1 + 1, refer.length());
                if (!refer.endsWith("/")) {
                    refer = refer + "/";
                }
                filename = refer + filename;
            }
            URL con = new URL(filename);
            InputStream gotoin = con.openStream();
            int n = gotoin.available();
            byte buf[] = new byte[1024];
            out.println("HTTP/1.0 200 OK\nMIME_version:1.0\nContent_Type:text/html");
            out.println("Content_Length:" + n + "\n");
            while ((n = gotoin.read(buf)) >= 0) {
                out.write(buf, 0, n);
            }
            out.close();
            s.close();
            in.close();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from  w  w w.  j a  va2  s. co 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("source")));
            while ((input = reader.readLine()) != null) {
                System.out.println(input);
            }
        }
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:com.reversemind.glia.test.go3.java

public static void main(String... args) throws Exception {

    System.setProperty("http.proxyHost", "10.105.0.217");
    System.setProperty("http.proxyPort", "3128");

    System.setProperty("https.proxyHost", "10.105.0.217");
    System.setProperty("https.proxyPort", "3128");

    HttpHost proxy = new HttpHost("10.105.0.217", 3128);
    DefaultHttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

    HttpGet request = new HttpGet(
            "https://twitter.com/i/profiles/show/splix/timeline/with_replies?include_available_features=1&include_entities=1&max_id=285605679744569344");
    HttpResponse response = client.execute(request);

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

    String sl = "";
    String line = "";
    while ((line = rd.readLine()) != null) {
        LOG.debug(line);//from  w w w  . j  a  va2 s  .  c o  m
        sl += line;
    }

    sl = new String(sl.getBytes(), "UTF-8");
    String sss = sl.replaceAll("\\{1,}", "\\").replaceAll("\\\"", "'").replaceAll("\\"", "'")
            .replaceAll("&gt;", ">").replaceAll("&lt;", "<").replaceAll("&amp;", "&").replaceAll("&apos;", "'")
            .replaceAll("\u003c", "<").replaceAll("\u003e", ">").replaceAll("\n", " ").replaceAll("\\/", "/")
            .replaceAll("\\'", "'");

    String sss2 = sss.replaceAll("\\'", "'");
    LOG.debug(sss);

    save("/opt/_del/go_sl.txt", sl);
    save("/opt/_del/go_sss.txt", sss);
    save("/opt/_del/go_line.txt", line);
    save("/opt/_del/go_sss2.txt", sss2);

    LOG.debug("\n\n\n\n\n");
    LOG.debug(sss);
    LOG.debug("\n\n\n\n\n");
    LOG.debug(URLDecoder.decode(sl, "UTF-8"));
    LOG.debug(URLDecoder.decode("\u0438\u043d\u043e\u0433\u0434\u0430", "UTF-8"));

    LOG.debug(URLDecoder.decode("\n            \u003c/span\u003e\n            \u003cb\u003e\n ", "UTF-8"));

}

From source file:ca.uwaterloo.cpami.mahout.matrix.utils.GramSchmidt.java

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

    //final Configuration conf = new Configuration();
    //final FileSystem fs = FileSystem.get(conf);
    //final SequenceFile.Reader reader = new SequenceFile.Reader(fs,
    //   new Path("R1.dat"), conf);
    //IntWritable key = new IntWritable();
    //VectorWritable vec = new VectorWritable();
    Matrix mat = new SparseMatrix(1500, 100);
    //SparseRealMatrix mat2 = new OpenMapRealMatrix(12419,1500 );
    BufferedReader reader = new BufferedReader(new FileReader("R.3.csv"));
    String line = null;//from w w  w  . j  av  a 2s.c  o m
    while ((line = reader.readLine()) != null) {
        String[] parts = line.split(",");

        mat.set(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Double.parseDouble(parts[2]));
        /*
        Vector v = vec.get();
        int i=0;
        Iterator<Vector.Element> itr = v.iterateNonZero();
        while(itr.hasNext()){
           double elem = itr.next().get();
           if(elem !=0)
              mat2.setEntry(i, key.get(), elem);
           i++;
        }
        */
    }

    //mat = mat.transpose();
    System.out.println(mat.viewColumn(0).isDense());
    System.out.println(mat.viewRow(0).isDense());
    mat = mat.transpose();
    GramSchmidt.orthonormalizeColumns(mat);
    /*
    System.out.println("started QR");
    System.out.println(Runtime.getRuntime().maxMemory());
    System.out.println(Runtime.getRuntime().maxMemory()-Runtime.getRuntime().freeMemory());
    QRDecomposition qr = new QRDecomposition(mat2);
    System.out.println(qr.getQ().getColumnDimension());
    System.out.println(qr.getQ().getRowDimension());
    */
    //mat = mat.transpose();
    //storeSparseColumns(mat);
    //for (int i = 0; i < 10; i++) {
    //   System.out.println(mat.viewRow(i).getNumNondefaultElements());
    //}

}

From source file:com.github.brandtg.stl.StlPlotter.java

public static void main(String[] args) throws Exception {
    List<Double> times = new ArrayList<Double>();
    List<Double> series = new ArrayList<Double>();
    List<Double> trend = new ArrayList<Double>();
    List<Double> seasonal = new ArrayList<Double>();
    List<Double> remainder = new ArrayList<Double>();

    // Read from STDIN
    String line;//from   w  w w.  j ava2s  .  c o m
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(",");
        times.add(Double.valueOf(tokens[0]));
        series.add(Double.valueOf(tokens[1]));
        trend.add(Double.valueOf(tokens[2]));
        seasonal.add(Double.valueOf(tokens[3]));
        remainder.add(Double.valueOf(tokens[4]));
    }

    StlResult res = new StlResult(convert(times), convert(series), convert(trend), convert(seasonal),
            convert(remainder));

    if (args.length == 1) {
        plot(res, new File(args[0]));
    } else {
        plotOnScreen(res, "Seasonal Decomposition");
    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.yahtzee2.YahtzeeClient.java

/**
 * @param args//from   ww  w.ja  va 2 s.com
 * @throws IOException
 */
public static void main(final String[] args) throws IOException {
    final RandomGenerator rng = new MersenneTwister(42);
    final YahtzeeState s0 = new YahtzeeState(rng);
    final YahtzeeSimulator sim = new YahtzeeSimulator(rng, s0);

    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    while (!sim.state().isTerminal()) {
        printState(sim.state());
        final String cmd = reader.readLine();
        final String[] parts = cmd.split(" ");
        if ("keep".equals(parts[0])) {
            final int[] keepers = new int[Hand.Nfaces];
            for (int i = 1; i < parts.length; ++i) {
                final int v = Integer.parseInt(parts[i]);
                keepers[v - 1] += 1;
            }
            sim.takeAction(new JointAction<YahtzeeAction>(new KeepAction(keepers)));
        } else if ("score".equals(parts[0])) {
            final YahtzeeScores category = YahtzeeScores.valueOf(parts[1]);
            sim.takeAction(new JointAction<YahtzeeAction>(new ScoreAction(category)));
        } else if ("undo".equals(parts[0])) {
            sim.untakeLastAction();
        } else {
            System.out.println("!!! Bad command");
        }
    }
    System.out.println("********** Terminal **********");
    printState(sim.state());
}

From source file:URLGet.java

public static void main(String[] args) {
    BufferedReader in = null;
    if (args.length == 1) {
        try {//from   ww w  . j  av  a 2  s.c o m
            URL url = new URL(args[0]);
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = null;
            while ((line = in.readLine()) != null)
                System.out.println(line);
        } catch (MalformedURLException ex) {
            System.err.println(ex);
        } catch (FileNotFoundException ex) {
            System.err.println("Failed to open stream to URL: " + ex);
        } catch (IOException ex) {
            System.err.println("Error reading URL content: " + ex);
        }
        if (in != null)
            try {
                in.close();
            } catch (IOException ex) {
            }
    } else
        System.err.println("Usage: URLGet URL");
}

From source file:TinyEdit.java

public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str[] = new String[100];

    System.out.println("Enter lines of text.");
    System.out.println("Enter 'stop' to quit.");
    for (int i = 0; i < 100; i++) {
        str[i] = br.readLine();
        if (str[i].equals("stop"))
            break;
    }//w  w w  .  j  a  va 2  s  .c o  m

    System.out.println("\nHere is your file:");

    for (int i = 0; i < 100; i++) {
        if (str[i].equals("stop"))
            break;
        System.out.println(str[i]);
    }
}

From source file:org.openeclass.EclassMobile.java

public static void main(String[] args) {

    try {//from   w  ww.jav a2  s.c o m
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://myeclass/modules/mobile/mlogin.php");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("uname", mUsername));
        nvps.add(new BasicNameValuePair("pass", mPassword));
        httppost.setEntity(new UrlEncodedFormEntity(nvps));

        System.out.println("twra tha kanei add");
        System.out.println("prin to response");

        HttpResponse response = httpclient.execute(httppost);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            System.out.println("Invalid response from server: " + status.toString());

        System.out.println("meta to response");

        Header[] head = response.getAllHeaders();

        for (int i = 0; i < head.length; i++)
            System.out.println("to head exei " + head[i]);

        System.out.println("Login form get: " + response.getStatusLine());

        HttpEntity entity = response.getEntity();
        InputStream ips = entity.getContent();
        BufferedReader buf = new BufferedReader(new InputStreamReader(ips, "UTF-8"), 1024);

        StringBuilder sb = new StringBuilder();
        String s = "";

        while ((s = buf.readLine()) != null) {
            sb.append(s);
        }
        System.out.println("to sb einai " + sb.toString());

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.github.ruananswer.stl.StlPlotter.java

public static void main(String[] args) throws Exception {
    List<Double> times = new ArrayList<Double>();
    List<Double> series = new ArrayList<Double>();
    List<Double> trend = new ArrayList<Double>();
    List<Double> seasonal = new ArrayList<Double>();
    List<Double> remainder = new ArrayList<Double>();

    // Read from STDIN
    String line;/*from  ww  w. j  a va  2 s  .  co m*/
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(",");
        times.add(Double.valueOf(tokens[0]));
        series.add(Double.valueOf(tokens[1]));
        trend.add(Double.valueOf(tokens[2]));
        seasonal.add(Double.valueOf(tokens[3]));
        remainder.add(Double.valueOf(tokens[4]));
    }

    STLResult res = new STLResult(convert(trend), convert(seasonal), convert(remainder));

    double[] tmpSeries = convert(series);
    double[] tmpTimes = convert(times);

    if (args.length == 1) {
        plot(res, tmpSeries, tmpTimes, new File(args[0]));
    } else {
        plotOnScreen(res, tmpSeries, tmpTimes, "Seasonal Decomposition");
    }
}