您的位置:首页 > 编程语言 > Java开发

java IO 流的学习(我们到底能走多远系列1)

2012-08-15 21:47 363 查看

我们到底能走多远系列(1)

“我们到底能走多远系列”:开始我的java之路。我要挖出这个地道,离开这里。


IO入门代码阅读:

字节流:


private void writeTxt(String path, String value) throws IOException{
OutputStream fos = new FileOutputStream(path);//构造方法1
fos.write(value.getBytes());
fos.close();
}
private void readTxt(String path) throws IOException{
File file = new File(path);
InputStream fis = new FileInputStream(path);//构造方法2
byte b[] = new byte[(int)file.length()] ;
fis.read(b);
System.out.print(new String(b));
fis.close();
}


字符流:

private void writeTxt(String path, String value) throws IOException{
Writer writer = new FileWriter(path);
writer.write(value);
writer.close();
}

private void readTxt(String path) throws IOException{
Reader reader = new FileReader(path);
char c[] = new char[1024] ;
int len = reader.read(c);
System.out.print(new String(c, 0, len));
reader.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: