FileOutputStream和FileInputStream使用方法说明
2014-12-26 11:03
477 查看
package fileTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileDemo {
public static void main(String[] arg){
File f = new File("1.txt");
//向文件里写如"Hello"字符串.
try {
//要写入的数据转换成字节数组
byte[] buf = "Hello".getBytes();
//如果1.txt存在,则删除1.txt里面的内容,文本所有内容变为Hello
//如果1.txt不存在,在新建1.txt文本,写入Hello
FileOutputStream out = new FileOutputStream(f);
out.write(buf);
out.close();
}catch(Exception e) {
System.out.println(e);
}
//读取文件中的内容。可在程序中单独使用,不用关心"写"是否存在.
try {
//只要f存在就可以读出f的内容,与写操作代码没有关联性.
FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[1024];
int len=in.read(buf); //从流中读取内容
String str = new String(buf,0,len);
System.out.println(str); //打印f文件的内容.
}catch(Exception e) {
System.out.println(e);
}
}
}
转自:http://www.blogjava.net/myfly/archive/2008/09/01/226084.html
相关文章推荐
- FileOutputStream和FileInputStream使用方法说明
- window.open的例子和使用方法以及参数说明(完整版)
- window.open的使用方法以及参数说明
- 简谈 FileInputStream 和 FileOutputStream 的使用方法
- window.open的例子和使用方法以及参数说明(完整版)
- sybase 客户端工具 设置链接的方法 dsedit的使用说明
- [技术]window.open的使用方法以及参数说明
- Linux操作系统下Sudo命令的使用方法说明
- window.open的例子和使用方法以及参数说明
- window.open的例子和使用方法以及参数说明(完整版)
- 关于zeo作品中生成代码使用方法的说明!
- 【专题】window.open的例子和使用方法以及参数说明(完整版)
- window.open使用方法以及参数说明
- window.open使用方法以及参数说明
- 一键GHOST V8.3 Build 060425 硬盘版一键ghost使用方法说明[图文详细教程]
- JavaScript---window.open使用方法以及参数说明(完整版)
- window.open的例子和使用方法以及参数说明(完整版)
- vi 使用方法说明
- js中slice()方法的使用说明
- window.open使用方法以及参数说明