java 字节型文件流 输入流 输出流; FileInputStream; FileOutputStream;
流按照方向(功能)来区分
in(读取) out(写入)
操作的目标来区分
文件流 数组流 字符串流 数据流 对象流 网络流
文件流-->读取文件中的信息in 将信息写入文件中out
文件流按照读取或写入的单位(字节数)大小来区分
字节型文件流(1字节)
FileInputStream FileOutputStream
字符型文件流 (2字节--1字符)
FileReader FileWriter
容器
1.变量 只能存一份;
2.数组 存储好多个 数据类型统一;
3.集合 存储好多个 存储后个数还能改变; 泛型--数据类型统一
---------------如上三个都是java中的类型(对象-->内存);---------------
----都存储在内存中 程序执行完毕 虚拟机停止的时候 内存空间就回收啦 数据都是临时性存储的---
4.文件 存储好多信息
文件是存储在硬盘上的 永久性保存
数据虽然是安全了
文件毕竟不在内存中 需要通过IO操作文件
----------------------------------------------------------------------------------------------
字节型文件输入流 FileInputStream
1.包 java.io
2.了解一下继承关系 Inputstream类 字节型输入流的父类
3.创建对象
调用一个带File类型的构造方法
调用一个带String类型的构造方法
4.常用方法
int a=read();每次从流管道中读取一个字节 返回字节的code码
int a=read(byte[])每次从流管道中读取若干个字节 存入数组内 返回有效元素个数
int a=available();返回流管道中海油多少缓存的字节数目;
long a=skip(long m) 跳过几个字节 读取;
多线程-->利用几个线程分段读取文件
close() 将流管道关闭--必须要做 最好放在finally里 注意代码的健壮性;
import java.io.*;
public class TestMain{
public static void main(String[] args){
FileInputStream ff=null;
try{
ff=new FileInputStream(new File("D:\\ddd\\xixi.txt"));
int vs=ff.available();//流管道中有多少缓存的字节 读取网络数据可能会有问题
System.out.println(vs);
long fv=ff.skip(5);//跳过5个
System.out.println(fv);
int fc=ff.read();
System.out.println((char)fc);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(ff!=null){
ff.close();//流通道关闭
}
}catch(IOException e){
e.printStackTrace();
}
}
/*
try{
FileInputStream fa=new FileInputStream("D:\\ddd\\xixi.txt");
byte[] bb=new byte[10];
int a=fa.read(bb);//去文件里读东西 装入数组内;//读取到的有效字节个数
while(a>0){//a!=-1;-1表示未读到
//System.out.println("+++="+a);
//for(byte v:bb){
//System.out.print((char)v);
//}
String v=new String(bb,0,a);
System.out.print(v);
//bb=new byte[10];
a=fa.read(bb);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
*/
/*
try{
File f=new File("D:\\ddd\\xixi.txt");
FileInputStream fa=new FileInputStream(f);//真实读取文件;
int i=fa.read();//读取一个字节 i是字节对应的Unicode码 0-65535
while(i!=-1){
System.out.print((char)i);
i=fa.read();
}
}catch(FileNotFoundException e){//编译时异常;
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
*/
}
}
字节型文件输出流
创建的是文件输入流 若文件路径有问题 则抛出异常 FileNotFoundException

创建的是文件输出流 若文件路径有问题 则直接帮我们创建一个新的文件;
FileOutputStream 将数据写入文件中
1.java.io
2.继承OutputStream 所有字节型输出流的父类
3.创建对象
调用一个带File参数 还有File Boolean 重载的
调用一个带String参数 还有String boolean重载
4.常用方法
write(int code);将给定code对应的字符写入文件
write(byte[]) 将数组中的全部字节写入文件 getByte()
flush() 将管道内的字节推入(刷新)文件
close() 注意在finally中关闭
import java.io.*;
public class TestMain{
public static void main(String[] args){
try{
FileOutputStream fo=new FileOutputStream("D:\\ddd\\xixi.txt",true);
//fo.write(98);
//fo.write(97);
fo.write(49);
fo.write('+');
fo.write(49);
fo.write('=');
fo.write(50);//1+1=2
//fo.flush();//刷新 将管道中的字节 推入文件中;
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.io.*;
public class TestMain{
public static void main(String[] args){
FileOutputStream fo=null;
try{
fo=new FileOutputStream("D:\\ddd\\xixi.txt",true);
//byte[] b=new byte[]{97,98,99};
String st="1+1=2";
byte[] b=st.getBytes();
fo.write(b);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fo.close(); //关闭流
}catch(IOException e){
e.printStackTrace();
}
}
}
}
浙公网安备 33010602011771号