文件的相关操作

创建文件

完整的路径 文件的copy path
String path="";
File file=new File(path.concat("/1.txt"));
判断文件是否存在
if (file.exists()==false)
{
file.createNewFile();
}

抛出异常

第一种
在main后面加 throws IOException
public static void main(String[]args) throws IOException, ClassNotFoundException
第二种
try {
file.createNewFile();
}
catch (IOException e)
{
System.out.println("IO异常");
}

I/O 流的说明
  • 流的方向:参考的是自己的内存空间

  • 输出流:从内存空间将数据写道外部设备(磁盘/硬盘/光盘)

  • 输入流:将外部设备写入内存

  • 流:统一管理数据的写入和读取

  • 输出流:开发者只需要将内存里面的数据写道流(stream)里面

  • 输入流:或者从流里面读取数据

  • 输出流:OutputStream字节流 Writer 字符流

  • 输入流:InputStream字节流 Read 字符流

  • I/O流对象 不属于内存对象 就需要自己关闭

  • OutputStream和InputStream都是抽象类 不能直接使用

  • 和FileInputStream(对文件操作)字节流(写入或读取字节数组)

  • ObjectOutputStream和ObjectInputStream(对对象操作)

  • FileWriter和FileRead 字符流(写入或读取字符数组)

向文件写入数据

第一种 写入字节数组
1.创建文件输出流(字节数组)对象
FileOutputStream fos=new FileOutputStream(file);
2.调用write 方法写入
byte[]text ={'1','2','3'};
fos.write(text);
3.操作完毕需要关闭stream对象
fos.close();
第二种 写入字符数组
向文件写入数据字符(字符数组)流
FileWriter fw=new FileWriter(file);
char [] name={'张','欣','欣'};
fw.write(name);
fw.close();

读取内容

第一种读取字节
FileInputStream fis=new FileInputStream(file);
byte[] name=new byte[9];
fis.read(name);
fis.close();
System.out.println(new String(name));
第二种读取字符
FileReader fr=new FileReader(file);
char [] book=new char[4];
fr.read(book);
fr.close();
System.out.println(new String(book));

向文件里面存一个对象
  • 序列化 Serializable
  • 保存的对象必须实现 Serializable接口
  • 如果这个对象内部还有属性变量是其他类的对象
  • 这个类也必须实现 Serializable接口

Person xw = new Person();
xw.age=20;
xw.name="小王";
OutputStream ops=new FileOutputStream(file);
ObjectOutputStream oops=new ObjectOutputStream(ops);
oops.writeObject(xw);
oops.close();

从文件里面读取一个对象

InputStream is= new FileInputStream(file);
ObjectInputStream ois=new ObjectInputStream(is);
Person xw=(Person) ois.readObject();
System.out.println(xw.name+""+xw.age);
ois.close();

将一个文件 copy 到另一个位置
第一种 单字节输出
         //1.源文件的路径
         String  p="C:\\Users\\ASUS\\Desktop\\1.abc.png";
         //2.目标文件的路径
         String c="C:\\Users\\ASUS\\AndroidStudioProjects\\day4\\java\\src\\main\\java\\swu\\zxxday8\\java\\PassWord\\1.abc.png";
         //3.图片 字节

         FileInputStream fis= new FileInputStream(p);
         FileOutputStream  fos= new FileOutputStream(c);
         byte [] in=new byte[1024];
         //第二种
         int count=0;
         while((count=fis.read(in))!=-1)
         {
             fos.write(in,0,count);
         }
        //第一种
         while (true)
         {
             int count =fis.read(in);
             if (count!=-1)
             {
                 //将这次读取的内容写入文件
                 fos.write(in,0,count);
             }else {
                 break;
             }
         }


        fis.close();
         fos.close();
         */
       /*
   第二种 多字节输出
         long start = System.currentTimeMillis();
 String  p="C:\\Users\\ASUS\\Desktop\\1.abc.png";
         Stringc="C:\\Users\\ASUS\\AndroidStudioProjects
\\day4\\java\\src\\main\\java\\swu\\zxxday8\\java\\PassWord\\1.abc.png";
         //输出流
         InputStream is=new FileInputStream(p);
         BufferedInputStream  bis=new BufferedInputStream(is);
         //输出流
         OutputStream os=new FileOutputStream(c);
         BufferedOutputStream bos=new BufferedOutputStream(os);
         byte [] in=new byte[1024];
         int count =0;
         while(  (count=bis.read(in))!=-1)
         {
             bos.write(in,0,count);
         }
        bis.close();
         bos.close();
         long end = System.currentTimeMillis();

         System.out.println(end-start);

感悟:这几天的内容不是很难,就是要记得东西多了,一个操作要分好几个步骤,一个步骤有好几种用法,不同的情况使用不同的代码。还好每天基本只学一个操作,还是很轻松的。

你可能感兴趣的:(文件的相关操作)