File file = new File( String pathname );
pathname 形式要写成"c:\test .txt"或者"c:/test .txt"!
使用Filel类创建文件:
1、如果父级目录可达,可以直接用createNewFile()创建文件
也可以用mkdir()来创建目录
2、如果父级目录不存在,需要先用getParentFile ()获取父级File对象
用父级对象.mkdirs()可以递归创建全部的父级目录
文件File的操作:
增:createNewFile()、mkdir()、mkdirs()
删:delete()、deleteonExit();
改:renameTo()
查:getName ()、getPath() 、getAbsolutePath() 、getParentFile ()、
List()、ListFiles()
list()方法是返回某个目录下的所有文件和目录的文件名,返回的是String数组
listFiles()方法是返回某个目录下所有文件和目录的绝对路径,返回的是File数组
下面一段代码是实现查看文件属性、创建和删除文件功能
public class Test {
public static void main(String[] args) throws IOException {
File f=new File("/myDoc/test.txt");
// File f2=new File("/myDocs/test.txt");
// File p2=f2.getParentFile();
// p2.mkdirs();
// f2.createNewFile();
File f1=new File("zz.txt");
f1.createNewFile();
File p = f.getParentFile();
if(!p.exists())
p.mkdirs();
if(!f.exists())
f.createNewFile();
System.out.println("名称:"+f.getName());
System.out.println("相对路径:"+f.getPath());
System.out.println("绝对路径:"+f.getAbsolutePath());
System.out.println("文件大小:"+f.length()+"字节");
}
}
IO流:
我们通过流来读写文件
流是一组有序的数据序列;以先进先出方式发送信息的通道
Java流的分类
输入输出流是相对于计算机内存来说的
字节流是 8 位通用字节流,字符流是 16 位 Unicode 字符流
基础流:
文件字节流:FileInputStream、FileOutputStream
文件字符流:FileReader、FileWriter
转换流:InputStreamReader、OutputStreamWriter
高级流:
缓冲流:BufferReader、BufferWriter
二进制流:DataInputStream、DataOutputStream
对象流:ObjectInputStream、ObjectOutputStream
字节流常用方法介绍:1 、lnputstream 字节输入流( 抽象类一基类) 一一读
int read () :
从输入流一个字节一个字节的读, 返回的是该字节的整数表示形式, 如果读到了输入流的末尾,返回-1
int read (byte [ ] b) :
从输入流读取若干字节, 把这些字节保存到数组b 中。返回的是读取到的字节数, 如果读到了输入流的末尾, 返回-1
int read (byte [ ] b , int off , int len) :
从输入流读取若干字节, 把这些字节保存到数组b 中。off 指的是字节数组中开始保存数据的起始下标。len 指读取的字节数目。返回的是实际读取到的字节数, 如果读到了输入流的末尾, 返回-1
close() : 关闭输入流
2 、0utputStream 字节输出流( 抽象类一基类) 一一写
write(int) : 往输出流中写入一个个的字节
write(byte[]) : 往输出流中写入一个字节数组
write(byte[] b , int off , int len) : 往输出流中写入一个字节数组, off 代表开始从字节数组的off 位置开始往外写, len 代表往外写len 长度的字节
close() : 关闭输出流
flush() : 强制把缓冲区中的数据全部写出到输出流中
FileInputStream常用的构造方法
FileInputStream(File file)
FileInputStream(String name)
FileOutputStream常用的构造方法
FileOutputStream (File file)
FileOutputStream(String name)
FileOutputStream(String name,boolean append)
1.前两种构造方法在向文件写数据时将覆盖文件中原有的内容
2.创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件
下面是FileInputStream、FileOutputStream的三种用法
//FileInputStream用法一:字节流每个字节逐个读取,读取的字节转换成字符型,存放到用StringBuilder建立的字符串数组中
public static String readFile(String path) throws IOException {
FileInputStream fis=new FileInputStream(path);
StringBuilder sb=new StringBuilder();
int tmp;
while((tmp=fis.read())!=-1){
sb.append((char)tmp);//每个字节逐个读取
}
fis.close();//关流
return sb.toString();
}
//FileInputStream用法二:建立一个字节型数组,大小是数据流里的字节数,然后把字节流里的内容一次性读取到数组中
// public static String readFileBytes(String path) throws IOException {
// FileInputStream fis=new FileInputStream(path);
// byte[] b=new byte[fis.available()];//available()在读写操作前得知数据流里有多少个字节可以读取
// fis.read(b);
// fis.close();
// return new String(b);
// }
//FileInputStream用法三:和方法二是一样的,只是他把异常用try-catch处理了,没有抛异常
public static String readFileBytes(String path) {
FileInputStream fis = null;
byte[] b = null;
try {
fis = new FileInputStream(path);
b = new byte[fis.available()];
fis.read(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(b);
}
//FileOutputStream用法一:把字符串每个字符逐个写出来
public static void writeFile(String path,String s)throws IOException{
FileOutputStream fos=new FileOutputStream(path);
for (int i = 0; i < s.length(); i++) {
char c=s.charAt(i);//charAt() 方法用于返回指定下标处的字符。
fos.write(c);
}
fos.close();
}
//FileOutputStream用法二:把字符串转化为一个字节数组,在一次性全部写入
// public static void writeFileBytes(String path,String s)throws IOException{
// FileOutputStream fos=new FileOutputStream(path);
// byte[] b=s.getBytes();//把字符串转化为一个字节数组
// fos.write(b);
// fos.close();
// }
//FileOutputStream用法三:和方法二是一样的,只是他把异常用try-catch处理了,没有抛异常
public static void writeFileBytes(String path,String s){
FileOutputStream fos=null;
byte[] b=s.getBytes();
try {
fos=new FileOutputStream(path,true);//TRUE表示每次吸入的内容不会覆盖,会被保存
fos.write(b);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//FileReader的用法:
public static String readChar(String path)throws IOException{
File f=new File(path);
FileReader fr =new FileReader(f);
// int tmp;
// StringBuilder sb=new StringBuilder();//建立一个字符串,用于存储要要读的内容;每个字节逐个读到字符串里的
// while((tmp=fr.read())!=-1){
// sb.append((char)tmp);
// }
char[] c=new char[(int)f.length()];//建立一个字符型数组,用于存储要要读的内容;
// 在读中文时,length()不够精确,容易出现空格溢出;
// 在main里调用方法时可以用trim()去除空格: writeChar("c.txt","start:"+s.trim()+"abc",false);//*****trim()去除空格
fr.read(c);//
fr.close();
return new String(c);
// return sb.toString();
}
// FileWriter的用法:
public static void writeChar(String path,String s,boolean isAppend)throws IOException{
FileWriter fw=new FileWriter(path,isAppend);//如果isAppend=false,则相当于没有
char[] cc=s.toCharArray();
// for (int i = 0; i < cc.length; i++) {
// fw.write(cc[i]);//逐个字节写入
// }
fw.write(s);//直接全部写入
fw.close();
}
演示BufferReader和BufferWrite
public class TestBuffered {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("a.txt");
BufferedReader br=new BufferedReader(fr);
StringBuffer sb=new StringBuffer();
String tmp;
while((tmp=br.readLine())!=null){
sb.append(tmp+"n");
// System.out.println(tmp);
}
System.out.println(sb.toString());
FileWriter fw=new FileWriter("d.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write(sb.toString());
bw.close();
fw.close();
br.close();
fr.close();
}
}
演示InputStreamReader、OutputStreamWriter
public class TestTransfer {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("a.txt" );
InputStreamReader isr=new InputStreamReader(fis);
char[] c=new char[fis.available()];
isr.read(c);
FileOutputStream fos=new FileOutputStream("b.txt",true);
OutputStreamWriter osw=new OutputStreamWriter(fos);
osw.write(new String(c).trim());//*****trim()去除空格
osw.close();
fos.close();
isr.close();
fis.close();
}
}
二进制流:DataInputStream、DataOutputStream
public class TestData {
public static void copy(String from,String to, boolean isAppend)throws IOException {
FileInputStream fis=new FileInputStream(from);
FileOutputStream fos=new FileOutputStream(to);
DataInputStream dis=new DataInputStream(fis);
DataOutputStream dos=new DataOutputStream(fos);
byte[] b=new byte[fis.available()];
fis.read(b);
fos.write(b);
dis.read(b);
dos.write(b);
dos.close();
dis.close();
fos.close();
fis.close();
}
public static void main(String[] args)throws IOException {
//C:UsersPicturesSaved Pictures微信图片_20211019001509
copy("C:\Users\Pictures\Saved Pictures\微信图片_20211019001509.jpg",
"ok.jpg",false);
}
}
演示对象流:ObjectInputStream、ObjectOutputStream
public class TsetOBJ {
public static void main(String[] args)throws Exception {
Student s=new Student(1,"张三",16,180);
s.setId(6);
s.setName("穷奇");
s.setAge(18);
s.setHeight(120);
System.out.println(s);
FileOutputStream fos=new FileOutputStream("e.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(s);
FileInputStream fis=new FileInputStream("e.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Object o=ois.readObject();
System.out.println(o);
if(o instanceof Student){
Student stu=(Student) o;
System.out.println(stu.getName());
}
ois.close();fis.close();oos.close();fos.close();
}
}
什么叫序列化和反序列化,代码如何实现
序列化是将对象的状态写入到特定的流中的过程
FileOutputStream fos=new FileOutputStream("临时文件路径");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(对象);--对象的类必须实现Serializable接口(可序列号)
反序列化则是从特定的流中获取数据重新构建对象的过程
FileInputStream fis=new FileInputStream("临时文件路径");
ObjectInputStream ois=new ObjectInputStream(fis);
Object o=ois.readObject();
今天的IO流就到这里来,再见!



