字节流输入,一次读取一个
File file = new File("E://Test//book.txt");
//字节流输入
//一次读取一个
FileInputStream in = new FileInputStream(file);
int i = 0;
while ((i=in.read())!=-1) {
System.out.print((char)i);
}
in.close();
字节流输入,一次读取多个
File file = new File("E://Test//book.txt");
//字节流输入
//一次传多个
FileInputStream in = new FileInputStream(file);
byte[] buf = new byte[8];
int i = 0;
while ((i=in.read(buf))!=-1) {
for (int j = 0; j < buf.length; j++) {
if (buf[j] == 0) {
break;
}
System.out.print((char)buf[j]);
}
}
in.close();
字节流实现文件复制:方法一
File file = new File("E:\\Test\\book.txt");//调用文件E盘文件
//读取数据或者写入数据需要运用IO流
//I:InputStream(输出)
FileInputStream in = new FileInputStream(file);
File filee = new File("F:\\abc\\hello.txt");//调用F盘文件
//O:OutputStream(输入)
FileOutputStream out = new FileOutputStream(filee);
int i = 0;//定义i接收read返回的值
filee.delete();//如果有原文件先删除
filee.createNewFile();//如果没有新文件则添加
while ((i=in.read())!=-1) {
out.write(i);
}
字节流实现文件复制:方法二
FileInputStream fis=new FileInputStream("E:\\Test\\book.txt");
FileOutputStream fiss = new FileOutputStream("F:\\abc\\hello.txt");
byte[] buf = new byte[64];
while(fis.read(buf)!=-1){
System.out.println("---");
}
fiss.write(buf);
fiss.close();
希望能帮到你