File
1.File类的一个对象,代表一个文件或者一个文件目录(文件夹)
2.File声明在java.io包下
3.File类中涉及到关于或者文件目录的创建,删除,重命名,修改时间,修改文件大小等方法,并未涉及文件存储操作
4.后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的终点
创建File类
File file = new File("hw.txt"); //相对路径创建文件
File file1 = new File("D:\\java"); // 绝对路径文件目录
File中常用方法
public boolean createNewFile(); //创建文件,成功返回true,失败返回false
public boolean mkdir(); //创建文件目录,如果此目录存在,就不创建了。如果此目录的上层文件不存在,就一并创建
public boolean delete(); //删除,不走回收站
public boolean isDirectory(); //判断是否为文件夹
public boolean isFile(); //判断是否为文件
public boolean exists(); //判断是否存在
public boolean canRead(); //判断是否可读
public boolean canWrite(); //判断是否可写
public boolean isHidden(); //判断是否隐藏
public String getAbsolutePath(); //获取绝对路径
public String getPath(); //获取路径
public String getName(): //获取名称
public String getParent(); //获取上层
public long length(): //获取文件长度
public long LastModified(); //获取最后一次修改时间
public String[] list(); //获取指定目录下的所有文件和文件夹
public File[] listFiles(); //获取指定目录下的所有文件和文件夹
IO流
IO流的原理及流的分类
按操作数据单位的不同可以分为:字节流(8bit),字符流(16bit)
按数据的流向的不同可以分为:输入流,输出流
按流的角色不同可以分为:节点流,处理流
访问文件的输入输出流
说明:
1.输出操作,对于File可以不存在的,并不会报异常
2.File对应的硬盘文件如果不存在,在输出的过程中,会自动创建此文件。
File对应的硬盘中的文件如果存在:
如果流使用的构造器是FileWriter(File,false)/FileWriter(File):就会对原文件的覆盖
FileWriter(File,true):不会对原文件覆盖,而是在原文件原来的基础上追加
3.对于文本文件(.txt,.java,.c,.cpp…)使用字符流处理,对于非文本文件(.jpg,.mp3,.mp4,.avi,…)使用字节流处理
FileReader
FileReader fr = null;
try {
File file = new File("D:\\Java\\src\\io\\hallow.txt");
fr = new FileReader(file);
int len;
char[] a = new char[5];//****
while((len = fr.read(a))!=-1)
{
for (int i=0;i<len;i++)
System.out.print(a[i]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr!=null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileWriter
FileWriter fw = null; //false是对文档进行重写,true是在文档上续写
try {
File file = new File("D:\\Java\\src\\io\\Hellow.txt");
fw = new FileWriter(file,true);
fw.write("i have a dream\n");
fw.write("you have a dream");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fw!=null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream,FileOutputStream
与上述基本类似,但是传输的是Byte数组
例题:判断文本中各符号的个数
Map<Character,Integer> map = null; //Map可以作为标识
FileReader fr =null;
try {
map = new HashMap<>();
File file = new File("D:\\Java\\src\\io\\Hellow.txt");
fr = new FileReader(file);
int len;
while((len = fr.read())!=-1)
{
char ch = (char) len;
if(map.get(ch)==null)
{
map.put(ch,1);
}
else {
map.put(ch,map.get(ch)+1);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr!=null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
File file1 = new File("D:\\Java\\src\\io\\ZY.txt");
FileWriter fw = new FileWriter(file1);
bw = new BufferedWriter(fw);
Set<Map.Entry<Character, Integer>> entries1 = map.entrySet();
Iterator<Map.Entry<Character, Integer>> iterator1 = entries1.iterator();
while(iterator1.hasNext())
{
Map.Entry<Character, Integer> next = iterator1.next();
fw.write(next.getKey()+"="+next.getValue());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw!=null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
例题:实现加密操作
缓冲流
1.缓冲流:
BufferedInputStream
BufferedOutputStraeam
BufferedReader (存在可以读一行的方法readLine)
BufferedWriter
2.作用:提供流的读取速度,写入的速度
BufferedInputStream bfip = null;
BufferedOutputStream bfop = null;
try {
//1.造文件
File file = new File("D:\\Java\\src\\io\\hallow.txt");
File file1 = new File("D:\\Java\\src\\io\\world.txt");
// 2.造流
//造节点流
FileInputStream fip = new FileInputStream(file);
FileOutputStream fop = new FileOutputStream(file1);
// 造缓冲流
bfip = new BufferedInputStream(fip);
bfop = new BufferedOutputStream(fop);
//细节
byte a[] = new byte[5];
int len;
while((len=bfip.read(a))!=-1)
{
bfop.write(a,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bfop!=null)
bfop.close(); //关闭只用关最外层
if(bfip!=null)
bfip.close();
}
转换流
1.转换流:属于字符流
InputStreamReader:将一个字节的输入流转换为字符的输入流
OutputStreamWriter:将一个字符的输出流转换为字节的输出流
2.作用:提供字节流和字符流之间的转换
BufferedReader br = null; //只有这个可以读一整行
try {
InputStreamReader ip = new InputStreamReader(System.in); //将字节流转换为字符流
// 因为标准流为字节流,而读整行只有缓冲流中的字符流才有,使用要转换
br = new BufferedReader(ip);
while (true)
{
System.out.println("输入数据");
String date = br.readLine();
if("e".equalsIgnoreCase(date))
{
System.out.println("程序结束");
break;
}
String up = date.toUpperCase();
System.out.println(up);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
标准流
读取控制台 用户输入的内容
若不在后台输入,会阻塞程序向下执行
标准输出流System.out
往控制台输出内容
一般会与打印流结合使用
键盘输入的俩种方法
Scanner sc = new Scanner(System.in);
String next = sc.next();
System.out.println(next);
public static void main(String[] args) throws IOException {
InputStreamReader isp = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isp);
String s = br.readLine();
System.out.println(s);
}
打印流
public static void main(String[] args) throws FileNotFoundException {
File file = new File("D:\\Java\\src\\io\\print.txt");
FileOutputStream fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos,true);
if(ps!=null)
System.setOut(ps); //输出到文件中
System.out.println("aaaaa");
ps.close();
}
对象流
对象流的使用:
1.ObjectInputStream 和 ObjectOutputStream
2.序列化:用ObjectOutputStream类保存基本数据类型数据或者对象的机制
反序列化:用ObjectInputStream 类读取基本数据类型或者对象的机制
3.作用:用于储存和读取基本数据类型数据或对象的处理流。它的强大之处就在于可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来
4.要想一个Java对象是可序列化的,需要满足:1.实现接口Serializable。 2. 当前类提供一个全局常量 serivalVersionUID(如果不提供这个常量,系统会自动给一个,但是ID会随着对象的改变而改变)。 3.除了当前类是可序列化的,还需要保证类内部的所有属性都是可序列化的
5.不能序列化static和transient
ObjectOutputStream
public static void main(String[] args) throws IOException {
FileOutputStream fp = new FileOutputStream("D:\\Java\\src\\io\\object.txt");
ObjectOutputStream oos = new ObjectOutputStream(fp);
oos.write(123);
oos.flush();
oos.writeObject(new person("aa",12));
oos.flush();
oos.close();
}
ObjectInputStream
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\Java\\src\\io\\object.txt"));
int a;
String b;
a = ois.read();
person o = (person) ois.readObject();
System.out.println(o);
}
序列化的类
public class person implements Serializable
{
public static final long serialVersionUID = 123L;
private String name;
private int age;
}
RandomAcessFile
1.RandomAcessFile直接作用于java.lang.Object类,实现了DataInput和DataOutput接口
2.RandomAcessFile既可以作为输入流也可以作为输出流
3.如果RandomAccessFile作为输出流时候,写到的文件不存在,则在执行的过程中自动创建,如果写出到的文件存在,则会对原有的文件内容进行覆盖(默认情况下从头覆盖)
r:以只读方式打开
rw:以读写方式打开
rwd:以读写方式打开,同步文件内容的更新
rws:以读写方式打开,同步文件内容和元数据的更新
4.在写入的时候要加上 .getbyte,想要在末尾续写需要使用seek方法将指针移动到需要的位置
例题:插入操作
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("D:\\Java\\src\\io\\random.txt","rw");
//raf.write("abcdefg".getBytes()); //写入要转换为byte
raf.seek(3);
StringBuffer sb = new StringBuffer((int) new File("D:\\Java\\src\\io\\random.txt").length()); //用来存储指针后面的内容
byte[] b = new byte[20];
int len;
while((len = raf.read(b))!=-1)
{
sb.append(new String(b,0,len));
}
raf.seek(3);
raf.write("123".getBytes());
raf.write(sb.toString().getBytes());
raf.close();
}