public static void main(String[] args) throws IOException { int num = 0; int guess = -1; int answer; String name; String line; String result; String scorefile = "highscores_java"; BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("guess.java - Guess a number between 1 and 100\n"); // generate random number java.util.Random generator = new java.util.Random(System.currentTimeMillis()); answer = generator.nextInt() / 42949673 + 50; // play game while (guess != answer) { num++; System.out.print("Enter guess " + num + ": "); result = stdin.readLine(); guess = Integer.parseInt(result); if (guess < answer) { System.out.println("Higher..."); } else if (guess > answer) { System.out.println("Lower..."); } } System.out.println("Correct! That took " + num + " guesses.\n"); // save high score System.out.print("Please enter your name: "); name = stdin.readLine(); try { BufferedWriter outfile = new BufferedWriter(new FileWriter(scorefile, true)); outfile.write(name + " " + num + "\n"); outfile.close(); } catch (IOException exception) { System.out.println("ERROR: Can't read from " + scorefile + "\n"); } // print high scores try { BufferedReader infile = new BufferedReader(new FileReader(scorefile)); while ((line = infile.readLine()) != null) { System.out.println(line); } infile.close(); } catch (IOException exception) { System.out.println("ERROR: Can't read from " + scorefile + "\n"); } }
public Duder(String args[]) { getConnected(args); play(); try { sin.close(); sout.close(); s.close(); } catch (IOException e) { System.out.println(e); } }
private void goGet(String host, int port, String contextPath) throws Exception { sock = new Socket(host, port); OutputStream os = sock.getOutputStream(); System.out.println(("GET " + contextPath + " HTTP/1.0\n")); os.write(("GET " + contextPath + " HTTP/1.0\n").getBytes()); os.write("\n".getBytes()); InputStream is = null; BufferedReader bis = null; String line = null; boolean pass = false; try { is = sock.getInputStream(); bis = new BufferedReader(new InputStreamReader(is)); while ((line = bis.readLine()) != null) { System.out.println(line); // Check if the filter was invoked if (EXPECTED_RESPONSE.equals("LLiFFiSSi")) { pass = true; break; } } } finally { try { if (is != null) { is.close(); } } catch (IOException ioe) { // ignore } try { if (bis != null) { bis.close(); } } catch (IOException ioe) { // ignore } } if (pass) { System.out.println("security constraint processed"); stat.addStatus(TEST_NAME + " PASSED", stat.PASS); } else { System.out.println("security constraint NOT processed"); stat.addStatus(TEST_NAME + " FAILED", stat.FAIL); } }
public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("numtri.in")); PrintWriter out = new PrintWriter(new FileWriter("numtri.out")); int r = Integer.parseInt(in.readLine()); final int LEN = r; int[] weights = new int[LEN]; weights[0] = Integer.parseInt(in.readLine()); search(weights, in, 1); insertionSort(weights, r); out.println(weights[r - 1]); in.close(); out.close(); System.exit(0); }
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); int i, j; String amount = req.getParameter("amount"); String from = req.getParameter("from"); String to = req.getParameter("to"); String ch = req.getParameter("choice"); String answer, answer1, answer2, answer3; if ("INFO".equals(ch)) { Redirect_info newurl = (Redirect_info) getServletContext().getAttribute("redirect"); res.sendRedirect(newurl.getUrl()); } else { out.println("<html>"); out.println("<title>Currency Converter</title>"); String addr = "http://www.google.com/ig/calculator?hl=en&q=" + amount + from + "=?" + to; URL convert = new URL(addr); BufferedReader in = new BufferedReader(new InputStreamReader(convert.openStream())); answer = in.readLine(); answer = new String(answer.getBytes("ISO-8859-1"), "ISO-8859-7"); from = new String(from.getBytes("ISO-8859-1"), "ISO-8859-7"); to = new String(to.getBytes("ISO-8859-1"), "ISO-8859-7"); amount = new String(amount.getBytes("ISO-8859-1"), "ISO-8859-7"); in.close(); i = answer.indexOf('"'); answer = answer.substring(i + 1); i = answer.indexOf('"'); answer = answer.substring(i + 1); i = answer.indexOf('"'); answer = answer.substring(i + 1); i = answer.indexOf('"'); answer = answer.substring(0, i); out.println("<p ALIGN=CENTER>" + amount + " " + from + " == " + answer + "(" + to + ")</p>"); out.println("</body>"); out.println("</html>"); } }
/** * Reads the population from a file. * * @param fileName is the file name where the classifiers have to be read. */ public void readPopulationFromFile(String fileName) { String line = null; try { System.out.print("\n LOADING POPULATION FROM FILE"); BufferedReader fin = new BufferedReader(new FileReader(fileName)); macroClSum = new Integer(fin.readLine().trim()) .intValue(); // We write the number of macro classifiers. microClSum = new Integer(fin.readLine().trim()) .intValue(); // We write the number of micro classifiers. fin.readLine(); // This line is only information to interpret the file. for (int i = 0; i < macroClSum; i++) { line = fin.readLine(); set[i] = new Classifier(new StringTokenizer(line)); } fin.close(); System.out.println(": done!"); } catch (IOException eio) { System.out.println( "ERROR when reading the population from the file: " + fileName + ": " + eio); } } // end readPopulationFromFile
/* public static void main(String[] args) { try { // solve(args); solve("-file=gc_70_5"); } catch (IOException e) { e.printStackTrace(); } } */ public static void solve(String args) throws IOException { String fileName = null; // get the temp file name // for (String arg : args) { if (args.startsWith("-file=")) { fileName = args.substring(6); } // } if (fileName == null) return; // read the lines out of the file List<String> lines = new ArrayList<String>(); BufferedReader input = new BufferedReader(new FileReader(fileName)); try { String line = null; while ((line = input.readLine()) != null) { lines.add(line); } } finally { input.close(); } // parse the data in the file String[] firstLine = lines.get(0).split("\\s+"); int nodes = Integer.parseInt(firstLine[0]); int edges = Integer.parseInt(firstLine[1]); g = new Graph(nodes, edges); g.initialiseGraph(); for (int i = 1; i < edges + 1; i++) { String line = lines.get(i); String[] parts = line.split("\\s+"); g.setEdges(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])); } noOfColors = g.getMaxDeg(); // System.out.println(noOfColors); colorOfNode = new int[nodes]; prevColorOfNode = new int[nodes]; domainOfNode = new int[nodes][noOfColors]; colorTheGraph(nodes); int colorsUsed = 0; for (int i = 0; i < nodes; i++) { int c = colorOfNode[i]; if (c > colorsUsed) colorsUsed = c; } // System.out.println(noOfColors); // System.out.println(checkEverything(nodes)); // prepare the solution in the specified output format System.out.println(colorsUsed + " " + 0); for (int i = 0; i < nodes; i++) { System.out.print(colorOfNode[i] + " "); } System.out.println(""); }
public boolean test() { ExtendedService es = null; boolean ok = true; String tmpfilename = getParameter("tmpfile"); try { // Lookup the javax.jnlp.ExtendedService object es = (ExtendedService) ServiceManager.lookup("javax.jnlp.ExtendedService"); } catch (UnavailableServiceException ue) { System.out.println(ue); ue.printStackTrace(); // Service is not supported ok = false; } if (!ok) return false; // Open a specific file in the local machine File tmpfile = new File(tmpfilename); // Java Web Start will pop up a dialog asking the user to grant permission // to read/write the file 'tempfile' try { FileContents fc_tmpfile = es.openFile(tmpfile); if (!tmpfilename.equals(fc_tmpfile.getName())) { System.out.println( "\t tmpfile(out): " + tmpfilename + ", unequal to fc-filename: " + fc_tmpfile.getName() + " - info"); // ok=false; // return ok; } if (!fc_tmpfile.canWrite()) { System.out.println( "\t outfile: " + tmpfilename + ", no write access (may not exist yet) - info"); } // You can now use the FileContents object to read/write the file java.io.OutputStream sout = fc_tmpfile.getOutputStream(true); BufferedWriter bwsout = new BufferedWriter(new OutputStreamWriter(sout)); bwsout.write(datum, 0, datum.length()); bwsout.flush(); bwsout.close(); } catch (Exception e) { System.out.println(e); e.printStackTrace(); System.out.println("\t Error while IO write - failed"); ok = false; return ok; } // read back .. try { FileContents fc_tmpfile = es.openFile(tmpfile); if (!tmpfilename.equals(fc_tmpfile.getName())) { System.out.println( "\t tmpfile(in): " + tmpfilename + ", unequal to fc-filename: " + fc_tmpfile.getName() + " - info"); // ok=false; // return ok; } if (!fc_tmpfile.canRead()) { System.out.println("\t outfile: " + tmpfilename + ", read access failed"); ok = false; } // You can now use the FileContents object to read/write the file java.io.InputStream sin = fc_tmpfile.getInputStream(); BufferedReader brsin = new BufferedReader(new InputStreamReader(sin)); String in = brsin.readLine(); if (!in.equals(datum)) { System.out.println("\t file content <" + in + "> does not match <" + datum + "> - failed"); ok = false; } brsin.close(); } catch (Exception e) { System.out.println(e); e.printStackTrace(); System.out.println("\t Error while IO read - failed"); ok = false; return ok; } return ok; }
public static void main(String[] args) throws Exception { IO.CreateFile io; io = new IO.CreateFile(args[0]); char[] array = io.giveArray(); BufferedReader br = new BufferedReader(new FileReader(args[1])); String line; int n = 0; int e = 0; int d = 0; int count = 0; while ((line = br.readLine()) != null) { if (count == 0) n = Integer.parseInt(line); if (count == 1) e = Integer.parseInt(line); if (count == 2) d = Integer.parseInt(line); count++; } br.close(); int index = 0; long num = 0; long encrypt = 0; int number = 0; int exponent = 0; while (index < array.length - 1) { num = 0; encrypt = 1; number = n; exponent = e; int first = 0; num += first << 16; for (int i = 0; i < 3; i++) { if (i == 0) { int a = array[index]; num += a << 16; index++; } if (i == 1) { int a = array[index]; num += a << 8; index++; } if (i == 2 && index < array.length) { int a = array[index]; num += a; index++; } } while (exponent != 0) { if (exponent % 2 == 1) { encrypt = (encrypt * num) % number; } exponent = exponent / 2; num = (num * num) % number; } int shift = 24; for (int i = 0; i < 4; i++) { io.write((byte) ((encrypt >> shift) & 0xFF)); shift -= 8; } } io.done(); }
public static void main(String[] args) throws FileNotFoundException, IOException { /*Get variables ready*/ String torrentFN; String saveFN; ArrayList<Peer> peers = null; byte[] b = null; /*check command line arguments*/ if (args.length == 2) { torrentFN = args[0]; saveFN = args[1]; file_destination = saveFN; } else { System.out.println("Invalid number of command line arguments"); return; } RandomAccessFile fSave = null; try { fSave = new RandomAccessFile(new File(saveFN), "rw"); } catch (FileNotFoundException e) { } /*read torrent file*/ BufferedReader reader = null; try { b = Files.readAllBytes(Paths.get(torrentFN)); } catch (FileNotFoundException e) { System.out.println("Caught Exception File not found"); // e.printStackTrace(); } catch (IOException e) { System.out.println("Caught Exception IOException"); // e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { System.out.println("Caught Exception IOException"); // e.printStackTrace(); } } /*send bytes to helper class*/ try { tInfo = new TorrentInfo(b); try { fSave.setLength((long) tInfo.file_length); byte[] empty = new byte[tInfo.file_length]; fSave.write(empty, 0, empty.length); } catch (IOException e) { } globalMemory = new MemCheck(tInfo, fSave); System.out.println("File mem length is " + fSave.length()); System.out.println(tInfo.file_name); } catch (BencodingException e) { System.out.println("Bencoding Exception"); } try { tResponse = TrackerResponse.getTrackerResponseStart(tInfo, 0, 0, tInfo.file_length); } catch (Exception e) { System.out.println("Problem with GET Request, program exiting"); return; } /*decode the tracker response*/ try { tResponseDecoded = TrackerResponse.decodeTrackerResponse(tResponse); } catch (Exception e) { System.out.println("Problem decoding tracker response"); return; } peers = tResponseDecoded.peers; int peerIndex; info_hash = tInfo.info_hash.array(); TrackerThread announceThread = new TrackerThread(); // in actual production peerIndex < peers.size(); for (peerIndex = 0; peerIndex < peers.size(); peerIndex++) { if (!peers.get(peerIndex).ipAdd.equals("128.6.171.132")) { Download peer = new Download(peers.get(peerIndex).ipAdd, peers.get(peerIndex).port); /*Lets schedule everything together*/ new Thread(peer).start(); } } new Thread(announceThread).start(); }