List of usage examples for java.io BufferedReader close
public void close() throws IOException
From source file:MainClass.java
public static void main(String args[]) throws Exception { FileOutputStream fouts = null; System.setProperty("javax.net.ssl.trustStore", "clienttrust"); SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault(); Socket s = ssf.createSocket("127.0.0.1", 5432); OutputStream outs = s.getOutputStream(); PrintStream out = new PrintStream(outs); InputStream ins = s.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(ins)); out.println(args[0]);/*from w w w. jav a 2s . c o m*/ fouts = new FileOutputStream("result.html"); // fouts = new FileOutputStream("result.gif"); int kk; while ((kk = ins.read()) != -1) { fouts.write(kk); } in.close(); fouts.close(); }
From source file:MainClass.java
public static void main(String[] args) { try {/* w w w . j ava2 s .com*/ URL url = new URL("http://www.java2s.com/"); URLConnection urlConnection = url.openConnection(); Map<String, List<String>> headers = urlConnection.getHeaderFields(); Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet(); for (Map.Entry<String, List<String>> entry : entrySet) { String headerName = entry.getKey(); System.out.println("Header Name:" + headerName); List<String> headerValues = entry.getValue(); for (String value : headerValues) { System.out.print("Header value:" + value); } System.out.println(); System.out.println(); } InputStream inputStream = urlConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line = bufferedReader.readLine(); while (line != null) { System.out.println(line); line = bufferedReader.readLine(); } bufferedReader.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.dlut.mycloudserver.service.performancemonitor.PerformanceListener.java
public static void main(String[] args) { long t1 = System.currentTimeMillis(); String url = "http://127.0.0.1:8001"; URLConnection conn;/*from w ww . j av a2 s. c om*/ try { conn = new URL(url).openConnection(); conn.setConnectTimeout(1000); conn.setReadTimeout(1000); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String res = br.readLine(); br.close(); is.close(); JSONObject jsonRes = JSON.parseObject(res); double loadAverage = jsonRes.getDoubleValue("loadAverage"); System.out.println(loadAverage); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t2 = System.currentTimeMillis(); System.out.println((t2 - t1) + "ms"); }
From source file:GZIPcompress.java
public static void main(String[] args) throws IOException { if (args.length == 0) { System.out.println("Usage: \nGZIPcompress file\n" + "\tUses GZIP compression to compress " + "the file to test.gz"); System.exit(1);//from w w w. j ava 2 s . co m } BufferedReader in = new BufferedReader(new FileReader(args[0])); BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz"))); System.out.println("Writing file"); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); System.out.println("Reading file"); BufferedReader in2 = new BufferedReader( new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz")))); String s; while ((s = in2.readLine()) != null) System.out.println(s); }
From source file:ZipDemo.java
public static void main(String[] args) throws Exception { for (int i = 0; i < args.length; ++i) { String uncompressed = ""; File f = new File(args[i]); if (f.exists()) { BufferedReader br = new BufferedReader(new FileReader(f)); String line = ""; StringBuffer buffer = new StringBuffer(); while ((line = br.readLine()) != null) buffer.append(line);/*from w ww . j a v a2 s . c o m*/ br.close(); uncompressed = buffer.toString(); } else { uncompressed = args[i]; } byte[] compressed = ZipDemo.compress(uncompressed); String compressedAsString = new String(compressed); byte[] bytesFromCompressedAsString = compressedAsString.getBytes(); bytesFromCompressedAsString.equals(compressed); System.out.println(ZipDemo.uncompress(compressed)); System.out.println(ZipDemo.uncompress(compressedAsString)); } }
From source file:RemoveHTMLReader.java
/** The test program: read a text file, strip HTML, print to console */ public static void main(String[] args) { try {/* w ww .j a v a2 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:com.trendmicro.hdfs.webdav.tool.Get.java
public static void main(String[] args) throws Exception { // Process command line Options options = new Options(); options.addOption("d", "debug", false, "Enable debug logging"); CommandLine cmd = null;/*from w ww .j a v a 2 s. c o m*/ try { cmd = new PosixParser().parse(options, args); } catch (ParseException e) { printUsageAndExit(options, -1); } boolean debug = cmd.hasOption('d'); args = cmd.getArgs(); if (args.length < 1) { printUsageAndExit(options, -1); } // Do the fetch AuthenticatedURL.Token token = new AuthenticatedURL.Token(); AuthenticatedURL url = new AuthenticatedURL(); HttpURLConnection conn = url.openConnection(new URL(args[0]), token); if (debug) { System.out.println("Token value: " + token); System.out.println("Status code: " + conn.getResponseCode() + " " + conn.getResponseMessage()); } if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); } reader.close(); } System.out.println(); }
From source file:Main.java
public static void main(String[] args) throws Exception { URL url = new URL("http://www.google.com"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); BufferedWriter writer = new BufferedWriter(new FileWriter("data.html")); String line;//from ww w. ja v a 2s . c o m while ((line = reader.readLine()) != null) { System.out.println(line); writer.write(line); writer.newLine(); } reader.close(); writer.close(); }
From source file:RhymingWords.java
public static void main(String[] args) throws IOException { FileReader words = new FileReader("words.txt"); // do the reversing and sorting Reader rhymedWords = reverse(sort(reverse(words))); // write new list to standard out BufferedReader in = new BufferedReader(rhymedWords); String input;/*from w ww .ja v a 2s . c o m*/ while ((input = in.readLine()) != null) System.out.println(input); in.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(/*from w w w .j av a2 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(); }