List of usage examples for java.io FileOutputStream flush
public void flush() throws IOException
From source file:net.pms.medialibrary.commons.helpers.FileImportHelper.java
/** * Saves the file specified by the url to the file saveFileName * @param url url of the file to save/* ww w . j a v a2 s .co m*/ * @param saveFileName absolute path to save the file to * @throws IOException thrown if the save operation failed or the content type of the file was of type text */ public static void saveUrlToFile(String url, String saveFileName) throws IOException { URL u = new URL(url); URLConnection uc = u.openConnection(); String contentType = uc.getContentType(); int contentLength = uc.getContentLength(); if (contentType.startsWith("text/") || contentLength == -1) { throw new IOException("This is not a binary file."); } InputStream raw = uc.getInputStream(); InputStream in = new BufferedInputStream(raw); byte[] data = new byte[contentLength]; int bytesRead = 0; int offset = 0; while (offset < contentLength) { bytesRead = in.read(data, offset, data.length - offset); if (bytesRead == -1) break; offset += bytesRead; } in.close(); if (offset != contentLength) { throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); } FileOutputStream out = new FileOutputStream(saveFileName); out.write(data); out.flush(); out.close(); log.debug(String.format("Saved url='%s' to file='%s'", url, saveFileName)); }
From source file:com.aliyun.odps.ogg.handler.datahub.BadOperateWriterTest.java
@Test public void testCheckFileSize() throws IOException { for (int i = 0; i < 2; i++) { String content = "hello,world"; if (i == 1) { content = "hello,liangyf"; }// ww w. j ava 2s . co m String fileName = "1.txt"; File file = new File(fileName); FileOutputStream outputStream = FileUtils.openOutputStream(file); outputStream.write(content.getBytes()); outputStream.flush(); outputStream.close(); BadOperateWriter.checkFileSize(fileName, 1); Assert.assertTrue(!file.exists()); BufferedReader br = new BufferedReader(new FileReader(fileName + ".bak")); String line = ""; StringBuffer buffer = new StringBuffer(); while ((line = br.readLine()) != null) { buffer.append(line); } br.close(); String fileContent = buffer.toString(); Assert.assertEquals(content, fileContent); } FileUtils.deleteQuietly(new File("1.txt")); FileUtils.deleteQuietly(new File("1.txt.bak")); }
From source file:at.tugraz.sss.serv.util.SSFileU.java
public static void writeFileBytes(final FileOutputStream fileOut, final InputStream streamIn, final Integer transmissionSize) throws SSErr { try {//w w w. jav a 2 s . c om final byte[] fileBytes = new byte[transmissionSize]; int read; while ((read = streamIn.read(fileBytes)) != -1) { if (fileBytes.length == 0 || read <= 0) { fileOut.write(new byte[0]); fileOut.flush(); break; } fileOut.write(fileBytes, 0, read); fileOut.flush(); } } catch (Exception error) { SSServErrReg.regErrThrow(error); } finally { if (fileOut != null) { try { fileOut.close(); } catch (IOException ex) { SSLogU.err(ex); } } if (streamIn != null) { try { streamIn.close(); } catch (IOException ex) { SSLogU.err(ex); } } } }
From source file:it.unisannio.srss.dame.android.services.FTPService.java
public void downloadFile(String remoteFilePath, String localFilePath) { File file = new File(localFilePath); File parentFile = file.getParentFile(); if (!parentFile.isDirectory() && !file.getParentFile().mkdirs()) { Log.e(TAG, "Unable to create the directory " + parentFile.getAbsolutePath()); return;/*from w w w . ja va 2 s . c o m*/ } FileOutputStream fos = null; try { fos = new FileOutputStream(file); this.ftp.retrieveFile(remoteFilePath, fos); fos.flush(); } catch (FileNotFoundException e) { Log.e(TAG, "Unable to open " + file.getAbsolutePath() + " for writing.", e); } catch (IOException e) { Log.e(TAG, "Error while downloading the payloads.", e); } finally { if (fos != null) try { fos.close(); } catch (Exception e) { } } }
From source file:com.actelion.research.mapReduceExecSpark.executors.MapReduceExecutorSparkProxy.java
public void writeToFile(String path, String serUUID, byte[] bytes) { //String path = httproot + File.separator + serUUID; System.out.println("serializing map-reduce task to " + path); try {//from w ww . j a va 2 s. com FileOutputStream fileOutputStream = new FileOutputStream(path); fileOutputStream.write(bytes); fileOutputStream.flush(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.wuliu.biz.util.export.strategy.CarIndexExport.java
public String export(String folderPath, String templateName, List<WuliuMergedOrderModel> mergedOrders) throws Exception { List<List<WuliuMergedOrderModel>> mergedOrderLists = split(mergedOrders); if (CollectionUtils.isEmpty(mergedOrderLists)) { return null; }//from ww w .ja v a 2 s .c om File template = new File(this.getClass().getClassLoader().getResource(templateName).getFile()); InputStream inp = new FileInputStream(template); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); fillSheet(sheet, mergedOrders); File file = new File(folderPath, getName(mergedOrders)); try { FileOutputStream outputStream = new FileOutputStream(file); wb.write(outputStream); outputStream.flush(); outputStream.close(); wb.close(); System.out.println("success"); } catch (Exception e) { System.out.println("It cause Error on WRITTING excel workbook: "); e.printStackTrace(); } return file.getAbsolutePath(); }
From source file:hu.sztaki.lpds.dcibridge.util.io.HttpHandler.java
public void read(String pURL, String pPath) throws Exception { File f = new File(pPath); f.createNewFile();//w ww. j a v a 2 s . c om open(pURL); InputStream is = getStream(new Hashtable<String, String>()); FileOutputStream outFile = new FileOutputStream(f); byte[] b = new byte[5120]; int ln = 0; while ((ln = is.read(b)) > 0) { outFile.write(b, 0, ln); outFile.flush(); } outFile.close(); is.close(); close(); }
From source file:com.google.code.maven.plugin.http.client.transformer.ResponseToFileResource.java
@Override protected Resource transform(HttpResponse response) throws Exception { InputStream in = getContentInputStream(response.getEntity()); File file = FileResourceUtils.create(target, overwrite); FileOutputStream out = new FileOutputStream(file); byte buffer[] = new byte[256]; int size;/*from w w w .j a v a2 s .c o m*/ while ((size = in.read(buffer)) > 0) { out.write(buffer, 0, size); out.flush(); } out.close(); in.close(); return target; }
From source file:com.galenframework.actions.GalenActionConfig.java
@Override public void execute() throws IOException { File file = new File("config"); if (!file.exists()) { file.createNewFile();//from w ww . ja v a 2 s.c om FileOutputStream fos = new FileOutputStream(file); StringWriter writer = new StringWriter(); IOUtils.copy(getClass().getResourceAsStream("/config-template.conf"), writer, "UTF-8"); IOUtils.write(writer.toString(), fos, "UTF-8"); fos.flush(); fos.close(); outStream.println("Created config file"); } else { errStream.println("Config file already exists"); } }
From source file:MainServer.ImageUploadServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); try {/* ww w. j a v a 2 s. c o m*/ List<FileItem> items = this.upload.parseRequest(request); if (items != null && !items.isEmpty()) { for (FileItem item : items) { String filename = item.getName(); String filepath = fileDir + File.separator + filename; System.out.println("File path: " + filepath); File file = new File(filepath); InputStream inputStream = item.getInputStream(); BufferedInputStream bis = new BufferedInputStream(inputStream); FileOutputStream fos = new FileOutputStream(file); int f; while ((f = bis.read()) != -1) { fos.write(f); } fos.flush(); fos.close(); bis.close(); inputStream.close(); System.out.println("File: " + filename + "Uploaded"); } } System.out.println("Uploaded!"); out.write("Uploaded!"); } catch (FileUploadException | IOException e) { System.out.println(e); out.write(fileDir); } }