public static void main(String[] args) throws NumberFormatException, IOException { FileInputStream inFile = new FileInputStream(new File("C-small-practice.in")); DataInputStream in = new DataInputStream(inFile); FileOutputStream outFile = new FileOutputStream(new File("out.txt")); DataOutputStream out = new DataOutputStream(outFile); int cases = Integer.parseInt(in.readLine()); t9Map = new HashMap<String, String>(); fillMap(); for (int i = 1; i <= cases; i++) { out.writeBytes("Case #" + i + ": "); String t9, t9last = ""; String text = in.readLine(); for (int j = 0; j < text.length(); j++) { t9 = t9Map.get(String.valueOf(text.charAt(j))); // do we need a break? (letters are on same button) if (j > 0 && (t9.charAt(0)) == t9last.charAt(0)) { out.writeBytes(" "); } out.writeBytes(t9); t9last = t9; } out.writeBytes("\n"); } }
public HTTPSConnectSocket(String host, int port, String urlString) throws IOException { URL url = null; try { url = new URL(urlString); } catch (MalformedURLException me) { System.out.println("Malformed url"); System.exit(1); } SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault(); ssl = (SSLSocket) ssf.createSocket(host, port); ssl.startHandshake(); // Send the CONNECT request ssl.getOutputStream().write(("CONNECT " + url.getFile() + " HTTP/1.0\r\n\r\n").getBytes()); // Read the first line of the response DataInputStream is = new DataInputStream(ssl.getInputStream()); String str = is.readLine(); // Check the HTTP error code -- it should be "200" on success if (!str.startsWith("HTTP/1.1 200 ")) { if (str.startsWith("HTTP/1.1 ")) str = str.substring(9); throw new IOException("Proxy reports \"" + str + "\""); } // Success -- skip remaining HTTP headers do { str = is.readLine(); } while (str.length() != 0); }
@SuppressWarnings("deprecation") private int count(File aSourceFile) { int lines = 0; if (aSourceFile.getName().contains(".java")) { try { FileInputStream fis = new FileInputStream(aSourceFile); DataInputStream d = new DataInputStream(fis); String temp = d.readLine(); while (temp != null) { lines++; temp = d.readLine(); } d.close(); } catch (FileNotFoundException e) { System.out.println("File not found" + e); } catch (IOException e) { System.out.println("IOException: " + e); } // System.out.println("Reading file: " + aSourceFile.getPath() + ", liner= " + lines); if (lines > topLimit) { if (topList.size() < nrInList) { topList.put(aSourceFile.getPath(), lines); } else { replaceLowest(aSourceFile.getPath(), lines); topLimit = topList.get(getLowestKey()).intValue(); } // printTopTen(); } } else { // System.out.print("."); } return lines; }
public static void main(String args[]) throws Exception { DataInputStream in = new DataInputStream(System.in); Vector v1F = new Vector<Double>(); int N = Integer.parseInt(in.readLine().trim()); for (int i = 1; i <= N; i++) { String s = in.readLine().trim(); s += " "; int index = s.indexOf(' '); double G = Double.parseDouble(s.substring(0, index)); int index_old = index + 1; index = s.indexOf(' ', index + 1); double W = Double.parseDouble(s.substring(index_old, index)); index_old = index + 1; index = s.indexOf(' ', index + 1); double M = Double.parseDouble(s.substring(index_old, index)); double pointer[] = new double[2]; double xx = (M - (M % (2 * W))) / (2 * W); double yy = ((M - (xx * W)) - ((M - (xx * W)) % G)) / G; // System.out.println (G + " " + W + " " + M + " " + xx + " " + yy); findMax(G, W, M, yy, xx, pointer); v1F.add(pointer[0]); // System.out.println ("Here: " + pointer[1]); } for (int i = 0; i < v1F.size(); i++) System.out.println(v1F.elementAt(i)); } // main
private int Load_Nodes(DataInputStream inStream) { // need to open file and load data int node_id; int x_cor; int y_cor; // int n_nodes, n_edges, node_cnt, arrow_status; Node n; String line; String item1, item2, item3, item4; node_id = 0; x_cor = 0; y_cor = 0; // n_nodes = 0; // n_edges = 0; // arrow_status = -1; try { if ((line = inStream.readLine()) != null) { StringTokenizer Data = new StringTokenizer(line, " "); item1 = Data.nextToken(); n_nodes = Integer.parseInt(item1); item2 = Data.nextToken(); n_edges = Integer.parseInt(item2); item3 = Data.nextToken(); arrow_status = Integer.parseInt(item3); // item4 = Data.nextToken(); // type = Integer.parseInt( item4 ); // graph = new GraphClass( n_nodes, n_edges, arrow_status ); nodes = new Node[n_nodes]; edges = new Edge[n_edges]; // ??? while ((this.Node_Cnt() < n_nodes) && ((line = inStream.readLine()) != null)) { Data = new StringTokenizer(line, " "); item1 = Data.nextToken(); item2 = Data.nextToken(); item3 = Data.nextToken(); node_id = Integer.parseInt(item1); x_cor = Integer.parseInt(item2); y_cor = Integer.parseInt(item3); n = new Node(node_id, x_cor, y_cor); this.Add_Node(n); } if (n_nodes != 0) { source_node = nodes[0]; } } } catch (IOException e) { System.err.println("error in file" + e.toString()); System.exit(1); } return this.Node_Cnt(); }
/** * Create a new Event Reader * * @param in * @throws IOException */ @SuppressWarnings("deprecation") public EventReader(DataInputStream in) throws IOException { this.in = in; this.version = in.readLine(); if (!EventWriter.VERSION.equals(version)) { throw new IOException("Incompatible event log version: " + version); } this.schema = Schema.parse(in.readLine()); this.reader = new SpecificDatumReader(schema); this.decoder = DecoderFactory.get().jsonDecoder(schema, in); }
public static void main(String[] args) { DataInputStream d = new DataInputStream(System.in); System.out.println("==> Taper deux lignes"); try { System.out.print("?"); String ligne1 = d.readLine(); System.out.print("?"); String ligne2 = d.readLine(); System.out.println("Les deux lignes lues sont:" + ligne1 + "=et=" + ligne2); } catch (java.io.IOException e) { System.out.println("Il y a une erreur de lecture"); } }
public void GenJCov(Environment env) { try { File outFile = env.getcovFile(); if (outFile.exists()) { DataInputStream JCovd = new DataInputStream(new BufferedInputStream(new FileInputStream(outFile))); String CurrLine = null; boolean first = true; String Class; CurrLine = JCovd.readLine(); if ((CurrLine != null) && CurrLine.startsWith(JcovMagicLine)) { // this is a good Jcov file while ((CurrLine = JCovd.readLine()) != null) { if (CurrLine.startsWith(JcovClassLine)) { first = true; for (Enumeration e = SourceClassList.elements(); e.hasMoreElements(); ) { String clsName = CurrLine.substring(JcovClassLine.length()); int idx = clsName.indexOf(' '); if (idx != -1) { clsName = clsName.substring(0, idx); } Class = (String) e.nextElement(); if (Class.compareTo(clsName) == 0) { first = false; break; } } } if (first) // re-write old class TmpCovTable.addElement(CurrLine); } } JCovd.close(); } PrintStream CovFile = new PrintStream(new DataOutputStream(new FileOutputStream(outFile))); CovFile.println(JcovMagicLine); for (Enumeration e = TmpCovTable.elements(); e.hasMoreElements(); ) { CovFile.println(e.nextElement()); } CovFile.close(); } catch (FileNotFoundException e) { System.out.println("ERROR: " + e); } catch (IOException e) { System.out.println("ERROR: " + e); } }
public boolean buildSatsFromKeps(Vector fileList) { Enumeration filelist = fileList.elements(); while (filelist.hasMoreElements()) { String file = (String) filelist.nextElement(); FileInputStream fis = null; DataInputStream dis = null; String lineRead = null; try { if (dir != null) { fis = new FileInputStream(this.dir + file); } else { fis = new FileInputStream(file); } dis = new DataInputStream(fis); System.out.println("Found file '" + dir + file + "': Loading keps"); while (dis.available() > 0) { lineRead = dis.readLine().trim(); if (lineRead.length() > 0) { if (lineRead.length() > 20) lineRead = lineRead.substring(0, 19); Satellite sat = new Satellite(lineRead); String firstLine = dis.readLine().trim(); if (firstLine.length() < 69) { sat = null; } else { String secondLine = dis.readLine().trim(); if (secondLine.length() < 69) { sat = null; } else { sat.setKeps(firstLine, secondLine); satVector.addElement(sat); } } } } } catch (IOException _ex) { System.out.println("Sat data file '" + dir + file + "' not found"); _ex.printStackTrace(); return false; } } mapper.myData.allSats = satVector; return true; }
public static void init() { System.out.println("Connecting to database..."); System.out.println("Please provide the username:"******"Please provide the password:"); try { PASS = in.readLine(); } catch (IOException e) { PASS = null; } }
public void run() { PrintStream out = new PrintStream(outFile); DataInputStream in = new DataInputStream(inFile); try { while (!suspending) { String line = in.readLine(); if (line == null) break; if (line.contains(query)) { out.println(line); } // Make grep take longer so that we don't require extremely // large files for interesting results try { Thread.sleep(2000); } catch (InterruptedException e) { // ignore it } } } catch (EOFException e) { // End of File } catch (IOException e) { System.out.println("GrepProcess: Error: " + e); } try { outFile.close(); } catch (IOException e) { // TODO Auto-generated catch block // e.printStackTrace(); } out.close(); suspending = false; }
@SuppressWarnings("deprecation") @DeployableTestMethod @Test(timeout = 300000) public void testFileReadAndWriteWithDataOutputStreamAndDataInputStream() throws IOException, FileNotFoundException, NullPointerException { Random rng = new Random(); String testString = "This string tests readLine"; double testDouble = rng.nextDouble(); int testInteger = rng.nextInt(); File testFile = new File(TEST_DIRECTORY + "shortReadWriteTestFile.txt"); DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(testFile)); outputStream.writeDouble(testDouble); outputStream.writeBytes(testString + "\n"); outputStream.writeInt(testInteger); outputStream.close(); DataInputStream inputStream = new DataInputStream(new FileInputStream(testFile)); double doubleReadBack = inputStream.readDouble(); String lineReadBack = inputStream.readLine(); int integerReadBack = inputStream.readInt(); assertTrue(testDouble == doubleReadBack); assertTrue(testString.equals(lineReadBack)); assertTrue(testInteger == integerReadBack); }
@SuppressWarnings("deprecation") @Override public void run() { running = true; while (running) { try { String value = in.readLine(); if (value == null) { running = false; break; } String newValue = ""; // clears the string for errors. for (char c : value.toCharArray()) { if (c != 0) { newValue += c; } } value = newValue; LOG.info("Received: " + value); Literal l = Literal.parseLiteral(value); try { l.addSource(ASSyntax.parseTerm("percept")); } catch (ParseException ex) { LOG.log(Level.SEVERE, "Could not add source", ex); } pendingBeliefs.add(l); } catch (IOException ex) { LOG.log(Level.SEVERE, "Could not read BT input.", ex); } } }
public static InputStream sort(InputStream source) { int MAXWORDS = 50; PipedOutputStream ps = null; PipedInputStream is = null; try { DataInputStream dis = new DataInputStream(source); String input; ps = new PipedOutputStream(); is = new PipedInputStream(ps); PrintStream os = new PrintStream(ps); String listOfWords[] = new String[MAXWORDS]; int numwords = 0, i = 0; while ((listOfWords[numwords] = dis.readLine()) != null) { numwords++; } quicksort(listOfWords, 0, numwords - 1); for (i = 0; i < numwords; i++) { os.println(listOfWords[i]); } os.close(); } catch (Exception e) { System.out.println("StringUtils sort: " + e); } return is; }
/** reads an int from InputStream in */ public static int readInt(InputStream in) { int result = -1; DataInputStream input = new DataInputStream((in)); String line; try { line = input.readLine(); } catch (IOException e) { System.out.println("readInt: couldn't read line."); return -1; } // System.out.println("line is: " + line); try { result = Integer.parseInt(line); } catch (NumberFormatException e) { System.out.print("readInt: couln't convert input to int, try again: "); result = readInt(in); } // System.out.println("answer is " + result); if (_out != null) // echo result to file { _out.println(result); _out.flush(); } return result; } // readInt
@SuppressWarnings("deprecation") public void writeLogs(String taskId, OutputStream output) { try (FileInputStream input = new FileInputStream(getLogFile(taskId))) { DataInputStream dataInput = new DataInputStream(input); output.write("[\n".getBytes()); byte[] buffer = new byte[4 * 1024]; boolean needComma = false; try { while (true) { IOUtils.skip(input, 1); String line = dataInput.readLine(); if (line == null || line.isEmpty()) { break; } int length = Integer.parseInt(line); if (length > buffer.length) { buffer = new byte[length]; } IOUtils.readFully(input, buffer, 0, length); if (needComma) { output.write(",\n".getBytes()); } else { needComma = true; } output.write(buffer, 0, length); } } catch (EOFException ignored) { // EOF reached } output.write("]\n".getBytes()); } catch (Throwable throwable) { Throwables.propagate(throwable); } }
public static void main(String[] args) { System.out.println("waiting"); try { ServerSocket serverSocket = new ServerSocket(34561); FunctionGroup langRuntime = new FunctionGroup(System.out); while (true) { Socket socket = serverSocket.accept(); // System.out.println("client connected"); Thread myThread = new Thread( () -> { try { DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); PrintStream printStream = new PrintStream(socket.getOutputStream()); while (true) { String line = dataInputStream.readLine(); // langRuntime.stripParenAndExecute(line); printStream.println(langRuntime.getEnvironment().getBufferVariable()); } } catch (IOException e) { } }); myThread.start(); } } catch (IOException e) { e.printStackTrace(); } }
private ArrayList<String> databaseUpgrade(int oldVersion, int newVersion) { AssetManager assetMgr = _context.getAssets(); ArrayList<String> sqls = new ArrayList<String>(); try { InputStream inputStream = assetMgr.open("db_upgrade_" + oldVersion + "_" + newVersion); DataInputStream dataInput = new DataInputStream(inputStream); StringBuffer sql = new StringBuffer(""); String clause = null; while ((clause = dataInput.readLine()) != null) { clause = clause.trim(); // 过滤空行和注释 if (clause.equals("") || clause.startsWith("--")) { continue; } // 查找语句结束的分号 sql.append(" ").append(clause); if (clause.endsWith(";")) { sqls.add(sql.toString()); EvtLog.d("debugDB", sql.toString()); sql.delete(0, sql.length()); } } } catch (IOException e) { EvtLog.e("debugDB", e); } return sqls; }
private int Load_Edges(DataInputStream inStream, int num) { String line; String item1, item2, item3; int source_node; int dest_node; int value; int n_nodes, edge_cnt; // Node nodes_array[] = new Node[num]; // Wil Edge edge; n_nodes = num; Node nodes_array[]; nodes_array = new Node[n_nodes]; nodes_array = this.Node_Array(); // Wil edge_cnt = 0; try { while ((line = inStream.readLine()) != null) { StringTokenizer Data = new StringTokenizer(line, " "); item1 = Data.nextToken(); item2 = Data.nextToken(); item3 = Data.nextToken(); source_node = Integer.parseInt(item1); dest_node = Integer.parseInt(item2); value = Integer.parseInt(item3); edge = new Edge(nodes_array[source_node - 1], nodes_array[dest_node - 1], value); this.Add_Edge(edge); edge_cnt++; } // inFile.close(); } catch (IOException e) { System.err.println("Error in accessing URL: " + e.toString()); System.exit(1); } return edge_cnt; }
int readScript(boolean local, String script) { DataInputStream in; try { if (local) { in = new DataInputStream(new FileInputStream(script)); } else { try { URL url = new URL(script); URLConnection connection = url.openConnection(); in = new DataInputStream(connection.getInputStream()); } catch (MalformedURLException e) { System.out.println("The URL is malformed: " + script); return 1; } catch (IOException e) { System.out.println("Could not read script file."); return 1; } } while (true) { String s; s = in.readLine(); if (s == null) break; if (echoInput) System.out.println(s); collect(s); } } catch (IOException e) { System.out.println("There was a problem reading the script file."); System.out.println("Tried " + script); return 1; } if (printData) print(); return 0; }
@SuppressWarnings("deprecation") public String runAsUser(String command) { String output = new String(); try { Process p = Runtime.getRuntime().exec("sh"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); DataInputStream is = new DataInputStream(p.getInputStream()); os.writeBytes("exec " + command + "\n"); os.flush(); String line = new String(); while ((line = is.readLine()) != null) { output = output + line; } // os.writeBytes("exit\n"); os.flush(); p.waitFor(); } catch (Throwable e) { Log.i(MyApp.TAG, e.getMessage().toString()); } return output; }
public static String execRootCmd(String cmd) { String result = "result : "; try { Process p = Runtime.getRuntime().exec("su "); OutputStream outStream = p.getOutputStream(); DataOutputStream dOutStream = new DataOutputStream(outStream); InputStream inStream = p.getInputStream(); DataInputStream dInStream = new DataInputStream(inStream); String str1 = String.valueOf(cmd); String str2 = str1 + "\n"; dOutStream.writeBytes(str2); dOutStream.flush(); String str3 = null; String line = ""; while ((line = dInStream.readLine()) != null) { Log.d("result", str3); str3 += line; } dOutStream.writeBytes("exit\n"); dOutStream.flush(); p.waitFor(); return result; } catch (Exception e) { e.printStackTrace(); return result; } }
/** reads an char from InputStream in */ public static char readChar(InputStream in) { char result = '?'; DataInputStream input = new DataInputStream(in); String line; try { line = input.readLine(); } catch (IOException e) { System.out.println("readChar: couldn't read line."); return '?'; } try { result = line.charAt(0); } catch (Exception e) { System.out.print("readChar: couldn't convert input to char, try again: "); result = readChar(in); } // System.out.println("answer is " + result); if (_out != null) // echo result to file { _out.println(result); _out.flush(); } return result; } // readChar
/** * Gets the token. * * @param scope the scope * @return the token */ private String getToken(String scope) { Process curlProc; String outputString; DataInputStream curlIn = null; String access_token = null; String command = "curl -X POST -H Content-Type:application/x-www-form-urlencoded " + adminUrl + "/oauth2/token --insecure --data" + " client_id=" + client_id + "&" + "client_secret=" + client_secret + "&grant_type=client_credentials&scope=" + scope; try { curlProc = Runtime.getRuntime().exec(command); curlIn = new DataInputStream(curlProc.getInputStream()); while ((outputString = curlIn.readLine()) != null) { JSONObject obj = new JSONObject(outputString); access_token = obj.getString("access_token"); } } catch (IOException e1) { e1.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return access_token; }
public String readXML(File file) { StringBuilder builder = new StringBuilder(); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); while (dis.available() != 0) { builder.append(dis.readLine()); } fis.close(); bis.close(); dis.close(); return builder.toString().replaceAll("xmlns=", "dummy="); // dom4j parser doesn't like // xmlns in the root element } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } }
public static String readFile(File file) { FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; StringBuilder br = new StringBuilder(); try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); while (dis.available() != 0) { br.append(dis.readLine() + "\n"); } // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); return br.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return new String(); }
@SuppressWarnings("deprecation") private static void resetEcoChar(File charFile) { try { String cheatStatus, tempData, tempAdd = ""; int curEquip = 0, curItem = 0, curBank = 0; File tempCharFile = new File(charDir.toString() + "ECOBOOST$TEMP"); DataInputStream fileStream = new DataInputStream(new FileInputStream(charFile)); BufferedWriter tempOut = new BufferedWriter(new FileWriter(tempCharFile)); while ((tempData = fileStream.readLine()) != null) { if ((!tempData.trim().startsWith("character-item =")) && (!tempData.trim().startsWith("character-bank ="))) { tempAdd = tempData.trim(); if (tempData.trim().startsWith("character-equip =")) { tempAdd = "character-equip = " + curEquip + "\t-1\t0"; curEquip++; } tempOut.write(tempAdd); tempOut.newLine(); } } fileStream.close(); tempOut.close(); charFile.delete(); tempCharFile.renameTo(charFile); } catch (Exception e) { e.printStackTrace(); } }
public void initilize2() { int i; msg = s2.toCharArray(); for (i = 0; i < s2.length(); i++) { try { dos.write(msg[i]); // System.out.print(msg[i]); } catch (Exception e) { System.out.print("init 2 send" + e); } } try { while ((rs = dis.readLine()) != null) { // System.out.println(rs); if (rs.equals("OK")) { break; } if (rs.equals("ERROR")) { break; } } } catch (Exception e) { System.out.println("init 2 rec" + e); } }
private void appendFile(FileOutputStream out, String path) throws IOException { DataInputStream input = new DataInputStream(new FileInputStream(path)); String strLineinput; while ((strLineinput = input.readLine()) != null) { out.write((strLineinput + "\n").getBytes()); } input.close(); }
/** @tests java.io.DataInputStream#readLine() */ @SuppressWarnings("deprecation") public void test_readLine() throws IOException { os.writeBytes("Hello"); os.close(); openDataInputStream(); String line = dis.readLine(); assertTrue("Incorrect line read: " + line, line.equals("Hello")); }