Io流之字节流的介绍及用法
package Io流;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Demo03 {
/**
* 输入流
* 1.字节输入流 InputStream
* 1.1 FileInputStream(File file) 推荐 read();
* FileInputStream(pathName)
* 1.2 BufferInputStream
* 创建数据
* 铺设管道
* 开水龙头
* 关水龙头
*
* 2.字节输出流
* 2.1 FileOutputStream(pathName)
* FileOutputStream(pathName,true)
* write(); close();
* 2.2 BufferedOutputStream
* 创建数据
* 铺设管道
* 开水龙头
* 关水龙头
*/
// FileInputStream(File file)
@Test
public void test(){
try {
File file = new File("d:\\11.txt");
if (file.exists() && file.length()>0){
FileInputStream fis = new FileInputStream(file);
int ch = fis.read();
System.out.println(ch);
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void test1(){
try {
File file = new File("d:\\11.txt");
if (file.exists() && file.length()>0){
FileInputStream fis = new FileInputStream(file);
int ch = 0;
while ((ch =fis.read()) != -1){
System.out.print((char)ch);
}
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//覆盖原文件
@Test
public void test3(){
try {
String str = "not beautiful";
File file = new File("d:\\11.txt");
FileOutputStream fos = new FileOutputStream(file,true);
//write() 如果文件不存在,自动创建
fos.write(str.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
人生不易,多多努力!!!
浙公网安备 33010602011771号