package org.springside.examples.showcase.utilities.io;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.commons.io.output.WriterOutputStream;
import org.junit.Test;
import org.springside.modules.utils.Exceptions;
import com.google.common.base.Charsets;
public class IODemo {
@Test
public void workWithFileContent() {
File file = new File("woop.txt");
try {
// text -> file
FileUtils.write(file, "Hey sailor!\nHaha\n", "UTF-8");
// file -> outputstream
System.out.println("copy File to outputstream:");
FileUtils.copyFile(file, System.out);
// file -> string
System.out.println("File to String:");
System.out.println(FileUtils.readFileToString(file, "UTF-8"));
// file -> list<string>
System.out.println("File to List<String>:");
List<String> lines = FileUtils.readLines(file, "UTF-8");
for (String string : lines) {
System.out.println(string);
}
} catch (IOException e) {
Exceptions.unchecked(e);
}
}
/*
* Input/OutputStream
*/
@Test
public void workWithStream() {
InputStream in = null;
try {
String content = "Stream testing";
// String - > InputStream
in = IOUtils.toInputStream(content, "UTF-8");
// String - > OutputStream
System.out.println("String to OutputStram:");
IOUtils.write(content, System.out, "UTF-8");
// InputStream/Reader -> String
System.out.println("\nInputStram to String:");
System.out.println(IOUtils.toString(in, "UTF-8"));
// InputStream/Reader -> OutputStream/Writer
InputStream in2 = IOUtils.toInputStream(content);
System.out.println("InputStram to OutputStream:");
IOUtils.copy(in2, System.out);
// InputStream ->Reader
InputStreamReader reader = new InputStreamReader(in, Charsets.UTF_8);
// Reader->InputStream
ReaderInputStream in3 = new ReaderInputStream(reader,
Charsets.UTF_8);
// OutputStream ->Writer
OutputStreamWriter writer = new OutputStreamWriter(System.out,
Charsets.UTF_8);
// Writer->OutputStream
WriterOutputStream out2 = new WriterOutputStream(writer,
Charsets.UTF_8);
// 收集Writer的内容到String.
StringWriter sw = new StringWriter();
sw.write("\nI am String writer");
System.out.println(sw.toString());
} catch (IOException e) {
Exceptions.unchecked(e);
} finally {
// 安静的关闭Stream
IOUtils.closeQuietly(in);
}
}
@Test
public void workWithFileAndDir() {
try {
long size = FileSystemUtils.freeSpaceKb("C:");
System.out.println("c盘空闲大小:" + (size / 1024 / 1024) + "g");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void copyFile() {
File srcFile = new File("c:\\test.txt");
File destFile = new File("d:\\test.txt");
try {
FileUtils.deleteQuietly(destFile);
if (!srcFile.exists())
srcFile.createNewFile();
FileUtils.copyFile(srcFile, destFile);
Assert.assertTrue(destFile.exists());
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void writerFile() {
File f = new File("c:\\test1.txt");
try {
List<String> list = new ArrayList<String>();
list.add("test1");
list.add("1");
FileUtils.writeLines(f, list); // 写集合
// FileUtils.writeStringToFile(f, data); //写字符串
System.out.println("写成功");
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Test
public void testFileURL() {
File f = new File("c:\\test2.txt");
URL url;
try {
url = new URL("http://www.iteye.com/topic/575996");
FileUtils.copyURLToFile(url, f);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testJDKFile() {
File f = new File("c:\\test2.txt");
try {
System.out.println(f.getCanonicalFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
利用Memory Mapped File实践A,B跨程序的数据交换
java upload file demo,提供文件上传功能示例代码
convert file demo upload and download
代码实现file 各类函数,风格结构化!
最简单的pdf文件格式
文章中使用到的打开PPTX文件示例代码。
if you wan to use your custome config settings you can inherit the IConfigurationSectionHandler if your .net...if you want to store you config settings in another file,YOU CAN DOWNLOAD THIS DEMO PROJECT
适配Android7.0,使用FileProvider关于使用系统拍照,裁剪图片的简单demo。
此demo有bug,请下载最新的文件上传的案例
readme.txt file which briefly describes what the demo does and what features it uses. The demos should be run in the order in which they are listed in the supplied project group. Even if you have ...
BootStrap file upload
file transfer demo.socket
Webpack| 配置文件方式运行webpack : webpack-configfile-demo.zip
uploadQuickStartsee for more detail.Development$ npm i$ npm run dev$ open http://localhost:7001/Deploy$ npm start$ npm stopnpm scriptsUse npm run lint to check code style.Use npm test to run unit test...
demo cpp inference using libtorch for realtime semantic segmentation
jav 读取本地文件列表 FileBrowser_demojav 读取本地文件列表 FileBrowser_demo
上课老师写的file demo,很实用!
主要介绍了Android-WebView支持input file启用相机/选取照片功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
jQuery表单事件——文件域美化File Style Demo实例演示,把单调的文件选择域美化成漂亮的灰色按钮外观,并把文件域按一定规则排列,源码爱好者费了好大劲把文件全部搞齐全了,演示文件完全运行正常了,如图所示,...