本文整理汇总了Java中org.apache.commons.io.FileUtils.openOutputStream方法的典型用法代码示例。如果您正苦于以下问题:Java FileUtils.openOutputStream方法的具体用法?Java FileUtils.openOutputStream怎么用?Java FileUtils.openOutputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.io.FileUtils
的用法示例。
在下文中一共展示了FileUtils.openOutputStream方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
/**
* Process the incoming request from IRC client
*
* @param socket IRC client connection socket
* @throws IOException
*/
private void process(Socket socket) throws IOException {
FileOutputStream fileOutputStream = FileUtils.openOutputStream(eventFile);
List<String> input = IOUtils.readLines(socket.getInputStream());
for (String next : input) {
if (isPrivMessage(next)) {
fileOutputStream.write(next.getBytes());
fileOutputStream.write("\n".getBytes());
}
}
fileOutputStream.close();
socket.close();
}
示例2: writeFile
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
/**
* 写二进制流到文件
* @param inputStream 二进制流
* @param filePathAndName 保存的文件
* @param append 是否追加写入
* @return 保存的文件名
* @version 2017-02-28
*/
public static String writeFile(InputStream inputStream, String filePathAndName, boolean append) {
OutputStream out = null;
String fileName = null;
try {
File file = new File(filePathAndName);
fileName = file.getName();
out = FileUtils.openOutputStream(file, append);
byte[] buffer = new byte[1024 * 5];
int byteread = 0;
while ((byteread = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
}
out.close();
}catch(IOException e){
e.printStackTrace();
logger.error("调用ApacheCommon写二进制流到指定文件时:" + e.toString());
}finally {
IOUtils.closeQuietly(out);
}
return fileName;
}
示例3: testcheckAndDeleteCgroup
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Test
public void testcheckAndDeleteCgroup() throws Exception {
CgroupsLCEResourcesHandler handler = new CgroupsLCEResourcesHandler();
handler.setConf(new YarnConfiguration());
handler.initConfig();
FileUtils.deleteQuietly(cgroupDir);
// Test 0
// tasks file not present, should return false
Assert.assertFalse(handler.checkAndDeleteCgroup(cgroupDir));
File tfile = new File(cgroupDir.getAbsolutePath(), "tasks");
FileOutputStream fos = FileUtils.openOutputStream(tfile);
File fspy = Mockito.spy(cgroupDir);
// Test 1, tasks file is empty
// tasks file has no data, should return true
Mockito.stub(fspy.delete()).toReturn(true);
Assert.assertTrue(handler.checkAndDeleteCgroup(fspy));
// Test 2, tasks file has data
fos.write("1234".getBytes());
fos.close();
// tasks has data, would not be able to delete, should return false
Assert.assertFalse(handler.checkAndDeleteCgroup(fspy));
FileUtils.deleteQuietly(cgroupDir);
}
示例4: writeFilterToFile
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
private void writeFilterToFile(String bloomFilterFile) throws IOException {
System.out.println("Writing bloom filter to file " + bloomFilterFile);
File file = new File(bloomFilterFile);
FileOutputStream fos = FileUtils.openOutputStream(file, false);
filter.writeTo(fos);
fos.close();
}
示例5: splitZipToFolder
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
public static void splitZipToFolder(File inputFile, File outputFolder,
Set<String> includeEnties) throws IOException {
if (!outputFolder.exists()) {
outputFolder.mkdirs();
}
if (null == includeEnties || includeEnties.size() < 1) {
return;
}
final byte[] buffer = new byte[8192];
FileInputStream fis = new FileInputStream(inputFile);
ZipInputStream zis = new ZipInputStream(fis);
try {
// loop on the entries of the jar file package and put them in the final jar
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
// do not take directories or anything inside a potential META-INF folder.
if (entry.isDirectory()) {
continue;
}
String name = entry.getName();
if (!includeEnties.contains(name)) {
continue;
}
File destFile = new File(outputFolder, name);
destFile.getParentFile().mkdirs();
// read the content of the entry from the input stream, and write it into the archive.
int count;
FileOutputStream fout = FileUtils.openOutputStream(destFile);
while ((count = zis.read(buffer)) != -1) {
fout.write(buffer, 0, count);
}
fout.close();
zis.closeEntry();
}
} finally {
zis.close();
}
fis.close();
}
示例6: testDeleteCgroup
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Test
public void testDeleteCgroup() throws Exception {
final MockClock clock = new MockClock();
clock.time = System.currentTimeMillis();
CgroupsLCEResourcesHandler handler = new CgroupsLCEResourcesHandler();
handler.setConf(new YarnConfiguration());
handler.initConfig();
handler.clock = clock;
FileUtils.deleteQuietly(cgroupDir);
// Create a non-empty tasks file
File tfile = new File(cgroupDir.getAbsolutePath(), "tasks");
FileOutputStream fos = FileUtils.openOutputStream(tfile);
fos.write("1234".getBytes());
fos.close();
final CountDownLatch latch = new CountDownLatch(1);
new Thread() {
@Override
public void run() {
latch.countDown();
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
//NOP
}
clock.time += YarnConfiguration.
DEFAULT_NM_LINUX_CONTAINER_CGROUPS_DELETE_TIMEOUT;
}
}.start();
latch.await();
Assert.assertFalse(handler.deleteCgroup(cgroupDir.getAbsolutePath()));
FileUtils.deleteQuietly(cgroupDir);
}
示例7: downloadFileFromInternet
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
private void downloadFileFromInternet(CloseableHttpResponse result, File localFile) throws IOException {
FileOutputStream buffer = FileUtils.openOutputStream(localFile);
try {
long maxLength = result.getEntity().getContentLength();
long nextLog = -1;
// ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = result.getEntity().getContent().read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
long fileSize = FileUtils.sizeOf(localFile);
if (fileSize > nextLog) {
System.err.print("\r" + Ansi.ansi().eraseLine());
System.err.print(FileUtils.byteCountToDisplaySize(fileSize));
if (maxLength > 0) {
System.err.print(" [");
int stars = (int) (50.0f * ((float) fileSize / (float) maxLength));
for (int i = 0; i < stars; i++) {
System.err.print("*");
}
for (int i = stars; i < 50; i++) {
System.err.print(" ");
}
System.err.print("]");
}
System.err.flush();
nextLog += 100000;
}
}
buffer.flush();
System.err.println();
System.err.flush();
} finally {
IOUtils.closeQuietly(buffer);
}
}
示例8: unCompress
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
public static void unCompress (String zip_file, String output_folder)
throws IOException, CompressorException, ArchiveException
{
ArchiveInputStream ais = null;
ArchiveStreamFactory asf = new ArchiveStreamFactory ();
FileInputStream fis = new FileInputStream (new File (zip_file));
if (zip_file.toLowerCase ().endsWith (".tar"))
{
ais = asf.createArchiveInputStream (
ArchiveStreamFactory.TAR, fis);
}
else if (zip_file.toLowerCase ().endsWith (".zip"))
{
ais = asf.createArchiveInputStream (
ArchiveStreamFactory.ZIP, fis);
}
else if (zip_file.toLowerCase ().endsWith (".tgz") ||
zip_file.toLowerCase ().endsWith (".tar.gz"))
{
CompressorInputStream cis = new CompressorStreamFactory ().
createCompressorInputStream (CompressorStreamFactory.GZIP, fis);
ais = asf.createArchiveInputStream (new BufferedInputStream (cis));
}
else
{
try
{
fis.close ();
}
catch (IOException e)
{
LOGGER.warn ("Cannot close FileInputStream:", e);
}
throw new IllegalArgumentException (
"Format not supported: " + zip_file);
}
File output_file = new File (output_folder);
if (!output_file.exists ()) output_file.mkdirs ();
// copy the existing entries
ArchiveEntry nextEntry;
while ((nextEntry = ais.getNextEntry ()) != null)
{
File ftemp = new File (output_folder, nextEntry.getName ());
if (nextEntry.isDirectory ())
{
ftemp.mkdir ();
}
else
{
FileOutputStream fos = FileUtils.openOutputStream (ftemp);
IOUtils.copy (ais, fos);
fos.close ();
}
}
ais.close ();
fis.close ();
}
示例9: runOnSeparateThread
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
public void runOnSeparateThread() throws Exception {
for (String resource: resourcesToBeExtractedInDirectory) {
InputStream resourceAsStream = this.getClass().getResourceAsStream("/" + resource);
File outputFile = Paths.get(baseOutputDirectory.getAbsolutePath(), resource).toFile();
FileOutputStream fileOutputStream = FileUtils.openOutputStream(outputFile);
IOUtils.copy(resourceAsStream, fileOutputStream);
}
ResourceConfig config = new ResourceConfig();
config.packages("com.hribol.bromium.demo.app");
ServletHolder servlet = new ServletHolder(new ServletContainer(config));
server = new Server(0);
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase(baseOutputDirectory.getAbsolutePath());
resourceHandler.setDirectoriesListed(true);
resourceHandler.setWelcomeFiles(resourcesToBeExtractedInDirectory);
ServletContextHandler context = new ServletContextHandler(server, "/*");
context.addServlet(servlet, "/*");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {
resourceHandler,
context
});
server.setHandler(handlers);
server.start();
this.port = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
logger.info("Server started on port " + port);
new Thread(() -> {
try {
server.join();
} catch (InterruptedException e) {
logger.info("Interrupted!", e);
}
}).start();
}
示例10: testCheckFileSize
import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Test
public void testCheckFileSize() throws IOException {
for(int i = 0 ; i < 2; i++){
String content = "hello,world";
if(i == 1){
content = "hello,liangyf";
}
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"));
}