File 类的使用:
File的构造方法:
1、
File file = new File("d:/test.txt");
这个后面直接添加上文件的地址,然后创建对象
2、利用父文件对象和子文件来进行文件对象的创建
(1)先创建一个父文件对象
(2)再创建一个子文件字符串,字符串用放的是子文件的文件名
(3)利用这两个文件对象来创建新的文件创建
File parent = new File("d:/");
String child = "test.txt";
File newFile = new File(parent,child);
这两种是我们最经常用的文件创建方法
File的常用方法:
1、判断File文件对象是否存在:exists()方法,他的返回值是布尔型,true表示存在,false表示不存在
System.out.println(newFile.exists());
因为d:/test.txt文件不存在,所以false,此文件不存在
2、判断File对象是否是文件夹对象isDirectory,返回值是布尔型,true表示是文件夹,false表示不是
System.out.println(newFile.isDirectory());
3、判断是否为文件,可以是.txt与可以是.mp3之类的等文件 newFile.isFile(),返回值是布尔型,true表示是文件,false表示不是文件
System.out.println(newFile.isFile());
注意:若此文件本就不存在,现在他虽然是一个文件,不是一个文件夹,但是他是一个不存在的文件,所以再调用isFile()方法时也会显示为false
4、文件夹的创建,newFile.mkdir();
这个方法是创建一个新的文件夹,所以newFile一定要是一个文件夹对象,不然会创建一个带有文件后缀名的文件夹
5、创建文件的方法,createNewFile():
这是创建文件的方法,所以需要newFile对象是一个文件对象,不然就将会创建一个没有后缀名的文件
6、获取文件夹中的文件列表:newFile.listFiles(),他可以将文件夹中的所有的文件包括是文件夹和文件都将其转换为File对象然后存放到一个File数组中并返回。或newFile是一个文件对象而不是一个文件夹的话,那这个文件返回的就是一个null对象,但是若是一的文件夹的话,如果文件夹为空,那么这个File数组就是一个空数组,他的长度为0
File[] files = newFile.listFiles();
System.out.println(files.length);
7、文件删除方法,delete():
这个是用来删除文件的,他可以直接删除文件,但是如果要删除的是一个文件夹的话,就需要先删除其中的文件,然后再次其删除,这里就需要用到递归方法来删除文件夹了,
下面就是递归删除文件的方法了:
public void delFile(File file)
{
if (!file.exists())
{
System.out.println("文件不存在");
return;
}
if (file.isFile())
file.delete();
else if (file.isDirectory())
{
File[] files = file.listFiles();
for (File f : files) {
delFile(f);
f.delete();
}
}
}
8、文件大小的获取:newfile.length(),返回的是文件的字节数,但是他只能返回文件的字节数,不需要返回文件夹的字节数,所以我们需要递归遍历一个文件夹,以下就是具体文件大小的遍历:
public long getFileSize(File file) throws Exception {
if (!file.exists())
{
throw new Exception("文件不存在");
}
if (file.isFile())
return file.length();
else if (file.isDirectory())
{
File[] files = file.listFiles();
long sum = 0;
if (files!=null && files.length!=0)
{
for (File f : files) {
sum += getFileSize(f);
}
}
return sum;
}
else
return 0;
}
OutputStream这都是InputStream文件的流的超类,他们都是抽象类,所以我们不直接使用他们,我们可以使用他们的子类,FileInputStream和FileOutputStream来进行使用
FileInputStream是文件读取的功能:
具体的文件读取功能代码如下:
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("D:\\test/斗罗大陆.txt");
InputStream reader = new FileInputStream(file);
int ch;
while ((ch=reader.read())!=-1)
{
System.out.print((char) ch);
}
}
}
虽然我们可以读取到他的数据,但是会出现中文乱码的现在,这个再我们使用FileInputStream时是很难解决的,而且利用他来进行文件的读取也会十分的慢,这个时候,我们就需要用到他们的字节缓冲流BufferedInputStream和BufferedOutputStream来进行,他们可以直接再创建对象就设置编码格式,具体如下:
File file = new File("D:\\test/斗罗大陆.txt");
InputStream reader = new BufferedInputStream(new FileInputStream(file));
byte [] buffer = new byte[1024*10];
int len;
while ((len = reader.read(buffer))!=-1)
{
System.out.println(Arrays.toString(buffer));
}
这个时候我们发现还是行,还有出现乱码问题,这个因为中文是一个汉字两个字节,所以而这个流是一个字节一个字节的读取的,他并不是一个字(字符)一个的进行读取的,所以无法将他转换为中文,这个时候,我们就需要使另一个类来真正的解决这个问题,这个类就是字符缓冲流BufferedReader
他需要的是一个InputStreamReader对象,这个对象是一个字符流的类,他里面又需要一个字节流的类
文件写的类,有
FileOutputStream;
BufferedOutputStream;
OutputStreamWriter;
BufferedWriter;
而我们最经常使用的也是字符缓冲流的类
BufferedWriter;
使用方法如下:
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("D:\\test/斗罗大陆.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
String str;
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:/test/copy.txt")));
while ((str = reader.readLine())!=null)
{
System.out.println(str);
writer.write(str);
writer.newLine();//写入一个换行
}
writer.flush();//写完后要刷新一个他的缓冲区
reader.close();//读完后要关闭
writer.close();//写完后要关闭
}
}
文件夹的复制,代码如下:
//文件的复制
public static void copy(File oldfile,File newfile) throws IOException {
BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldfile));
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newfile));
byte [] buffer = new byte[1024*10];
int len=0;
while ((len=reader.read(buffer))!=-1)
{
writer.write(buffer,0,len);
}
writer.flush();
writer.close();
reader.close();
}
//文件夹的复制
public static void copyFile(File file,File newfile) throws Exception {
if (!file.exists())
{
throw new Exception("文件不存在");
}
if (file.isDirectory())
{
File file1 = new File(newfile,file.getName());
file1.mkdir();
File[] files = file.listFiles();
for (File f : files) {
copyFile(f,file1);
}
}
else if (file.isFile())
{
File file1 = new File(newfile,file.getName());
copy(file,file1);
}
}
序列化:
我们将要序列化的类去实现一个接口Serializable,
然后再new一个序列化定入类:ObjectOutputStream
再利用writeObject(序列化的对象)来进行序列化
反序列化:
new一个序列化定入类:ObjectInputStream
在利用readObject()来读取这个序列化文件,再将他赋给一个对象,但是他的返回值是一个Object对象,所以需要进行类型转换
具体操作如下:
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Scanner sc = new Scanner(System.in);
long goodId;
String goodName;
double price;
int stock;
List<Good> goods = new ArrayList<>();
File file = new File("d:/goods.data");
if (file.exists())
{
System.out.println("-----本地信息如下-------");
ObjectInputStream input = new ObjectInputStream(new FileInputStream("d:/goods.data"));
goods = (ArrayList<Good>) input.readObject();
input.close();
for (Good good : goods) {
System.out.println("编号:"+good.getGoodId());
System.out.println("名称:"+good.getGoodName());
System.out.println("价格:"+good.getPrice());
System.out.println("库存:"+good.getStock());
System.out.println();
}
}
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d:/goods.data"));
System.out.println("开始录入");
while (true)
{
System.out.println("请输入:");
System.out.print("编号:");
goodId = Integer.valueOf(sc.nextLine());
System.out.print("名称:");
goodName = sc.nextLine();
System.out.print("价格:");
price = Double.valueOf(sc.nextLine());
System.out.print("库存:");
stock = Integer.valueOf(sc.nextLine());
goods.add(new Good(goodId,goodName,price,stock));
System.out.println("是否结束录入(y/其它键继续)");
if (sc.nextLine().toLowerCase().equals("y"))
break;
}
out.writeObject(goods);
out.close();
}
}