List of usage examples for java.io OutputStreamWriter write
public void write(int c) throws IOException
From source file:com.foudroyantfactotum.mod.fousarchive.utility.midi.FileSupporter.java
public static void main(String[] args) throws InterruptedException, IOException { for (int i = 0; i < noOfWorkers; ++i) pool.submit(new ConMidiDetailsPuller()); final File sourceDir = new File(source); final File outputDir = new File(output); Logger.info(UserLogger.GENERAL, "source directory: " + sourceDir.getAbsolutePath()); Logger.info(UserLogger.GENERAL, "output directory: " + outputDir.getAbsolutePath()); Logger.info(UserLogger.GENERAL, "processing midi files using " + noOfWorkers + " cores"); FileUtils.deleteDirectory(outputDir); FileUtils.touch(new File(outputDir + "/master.json.gz")); for (File sfile : sourceDir.listFiles()) { recFile(sfile, files);/*from w w w. j av a2s . co m*/ } for (int i = 0; i < noOfWorkers; ++i) files.put(TERMINATOR); pool.shutdown(); pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);//just get all the work done first. try (final OutputStream fstream = new FileOutputStream(outputDir + "/master.json.gz")) { try (final GZIPOutputStream gzstream = new GZIPOutputStream(fstream)) { final OutputStreamWriter osw = new OutputStreamWriter(gzstream); osw.write(JSON.toJson(processedMidiFiles)); osw.flush(); } } catch (IOException e) { Logger.info(UserLogger.GENERAL, e.toString()); } Logger.info(UserLogger.GENERAL, "Processed " + processedMidiFiles.size() + " midi files out of " + fileCount + " files. " + (fileCount - processedMidiFiles.size()) + " removed"); }
From source file:com.athena.peacock.agent.Starter.java
/** * <pre>//from w ww. j av a 2 s . c om * * </pre> * @param args */ @SuppressWarnings("resource") public static void main(String[] args) { int rand = (int) (Math.random() * 100) % 50; System.setProperty("random.seconds", Integer.toString(rand)); String configFile = null; try { configFile = PropertyUtil.getProperty(PeacockConstant.CONFIG_FILE_KEY); } catch (Exception e) { // nothing to do. } finally { if (StringUtils.isEmpty(configFile)) { configFile = "/peacock/agent/config/agent.conf"; } } /** * ${peacock.agent.config.file.name} ?? load ? ?? ? ? ? . */ String errorMsg = "\n\"" + configFile + "\" file does not exist or cannot read.\n" + "Please check \"" + configFile + "\" file exists and can read."; Assert.isTrue(AgentConfigUtil.exception == null, errorMsg); Assert.notNull(AgentConfigUtil.getConfig(PeacockConstant.SERVER_IP), "ServerIP cannot be empty."); Assert.notNull(AgentConfigUtil.getConfig(PeacockConstant.SERVER_PORT), "ServerPort cannot be empty."); /** * Agent ID ?? ${peacock.agent.agent.file.name} ? ?, * ?? ? Agent ID ? ?? . */ String agentFile = null; String agentId = null; try { agentFile = PropertyUtil.getProperty(PeacockConstant.AGENT_ID_FILE_KEY); } catch (Exception e) { // nothing to do. } finally { if (StringUtils.isEmpty(agentFile)) { agentFile = "/peacock/agent/.agent"; } } File file = new File(agentFile); boolean isNew = false; if (file.exists()) { try { agentId = IOUtils.toString(file.toURI()); // ? ? agent ID agent ID? ? 36? ? ?. if (StringUtils.isEmpty(agentId) || agentId.length() != 36) { throw new IOException(); } } catch (IOException e) { logger.error(agentFile + " file cannot read or saved invalid agent ID.", e); agentId = PeacockAgentIDGenerator.generateId(); isNew = true; } } else { agentId = PeacockAgentIDGenerator.generateId(); isNew = true; } if (isNew) { logger.info("New Agent-ID({}) be generated.", agentId); try { file.setWritable(true); OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(file)); output.write(agentId); file.setReadOnly(); IOUtils.closeQuietly(output); } catch (UnsupportedEncodingException e) { logger.error("UnsupportedEncodingException has occurred : ", e); } catch (FileNotFoundException e) { logger.error("FileNotFoundException has occurred : ", e); } catch (IOException e) { logger.error("IOException has occurred : ", e); } } // Spring Application Context Loading logger.debug("Starting application context..."); AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/context-*.xml"); applicationContext.registerShutdownHook(); }
From source file:HelloSmartsheet.java
public static void main(String[] args) { HttpURLConnection connection = null; StringBuilder response = new StringBuilder(); //We are using Jackson JSON parser to deserialize the JSON. See http://wiki.fasterxml.com/JacksonHome //Feel free to use which ever library you prefer. ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); try {//w ww . j av a 2s .c o m System.out.println("STARTING HelloSmartsheet..."); //Create a BufferedReader to read user input. BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Smartsheet API access token:"); String accessToken = in.readLine(); System.out.println("Fetching list of your sheets..."); //Create a connection and fetch the list of sheets connection = (HttpURLConnection) new URL(GET_SHEETS_URL).openConnection(); connection.addRequestProperty("Authorization", "Bearer " + accessToken); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; //Read the response line by line. while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); //Use Jackson to conver the JSON string to a List of Sheets List<Sheet> sheets = mapper.readValue(response.toString(), new TypeReference<List<Sheet>>() { }); if (sheets.size() == 0) { System.out.println("You don't have any sheets. Goodbye!"); return; } System.out.println("Total sheets: " + sheets.size()); int i = 1; for (Sheet sheet : sheets) { System.out.println(i++ + ": " + sheet.name); } System.out.print("Enter the number of the sheet you want to share: "); //Prompt the user to provide the sheet number, the email address, and the access level Integer sheetNumber = Integer.parseInt(in.readLine().trim()); //NOTE: for simplicity, error handling and input validation is neglected. Sheet chosenSheet = sheets.get(sheetNumber - 1); System.out.print("Enter an email address to share " + chosenSheet.getName() + " to: "); String email = in.readLine(); System.out.print("Choose an access level (VIEWER, EDITOR, EDITOR_SHARE, ADMIN) for " + email + ": "); String accessLevel = in.readLine(); //Create a share object Share share = new Share(); share.setEmail(email); share.setAccessLevel(accessLevel); System.out.println("Sharing " + chosenSheet.name + " to " + email + " as " + accessLevel + "."); //Create a connection. Note the SHARE_SHEET_URL uses /sheet as opposed to /sheets (with an 's') connection = (HttpURLConnection) new URL(SHARE_SHEET_URL.replace(SHEET_ID, "" + chosenSheet.getId())) .openConnection(); connection.setDoOutput(true); connection.addRequestProperty("Authorization", "Bearer " + accessToken); connection.addRequestProperty("Content-Type", "application/json"); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); //Serialize the Share object writer.write(mapper.writeValueAsString(share)); writer.close(); //Read the response and parse the JSON reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } Result result = mapper.readValue(response.toString(), Result.class); System.out.println("Sheet shared successfully, share ID " + result.result.id); System.out.println("Press any key to quit."); in.read(); } catch (IOException e) { BufferedReader reader = new BufferedReader( new InputStreamReader(((HttpURLConnection) connection).getErrorStream())); String line; try { response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); Result result = mapper.readValue(response.toString(), Result.class); System.out.println(result.message); } catch (IOException e1) { e1.printStackTrace(); } } catch (Exception e) { System.out.println("Something broke: " + e.getMessage()); e.printStackTrace(); } }
From source file:Main.java
private static void writeXmlFootInfo(OutputStreamWriter fs) throws IOException { fs.write("</software>"); }
From source file:Main.java
public static void writeTmxCloseTag(OutputStreamWriter p_writer) throws IOException { p_writer.write("</tmx>\r\n"); }
From source file:Main.java
public static void writeBodyCloseTag(OutputStreamWriter p_writer) throws IOException { p_writer.write("</body>\r\n"); }
From source file:Main.java
public static void wirteStringToStream(OutputStream out, String src) throws IOException { final OutputStreamWriter writer = new OutputStreamWriter(out); writer.write(src); writer.flush();//from w ww . jav a 2s. com }
From source file:Main.java
public static void reboot() { try {/*from w ww.j a v a 2 s .c o m*/ Process su = Runtime.getRuntime().exec("su"); OutputStreamWriter out = new OutputStreamWriter(su.getOutputStream()); out.write("reboot"); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
static public void writeToFile(String filename, String content) { try {// w ww . j a v a 2s .co m File file = new File(filename); OutputStream outputStream = new FileOutputStream(file); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); outputStreamWriter.write(content); outputStreamWriter.close(); } catch (IOException e) { // Log.e("Exception", "File write failed: " + e.toString()); } }
From source file:Main.java
/** * Method - saveMessage//www.j a va 2 s. co m * * Description - Saves messages to the file by either appending or rewriting, used by any activity that needs to save the messages * * @param ctx - Context of the Application that called the function * @param message - The messages to save to the file, most often a long String of several files * @param mode - Very likely either Context.MODE_APPEND or Context.MODE_PRIVATE for appending or rewriting respectively */ public static void saveMessage(Context ctx, String message, int mode) { try { // Creating objects to write to the file FileOutputStream fos = ctx.openFileOutput("messages.dat", mode); OutputStreamWriter osw = new OutputStreamWriter(fos); osw.write(message); // Writing to the file osw.flush(); // Making sure all characters have been written // Closing objects after writing has finished osw.close(); fos.close(); } catch (FileNotFoundException e) { return; } catch (IOException e) { return; } }