• JAVA文件操作


    一、JAVA中的IO基本知识

    1、关于编码:
    JAVA中的常见编码格式有:GBK、UTF-8、UTF-16BE等
    1)GBK格式:中文占2字节,英文占1字节。如果没有经过人为更改,Eclipse和MyEclipse中JAVA项目的默认编码格式都是GBK格式
    2)UTF-8格式:中文占3字节,英文占1字节
    3)UTF-16BE格式:中文和英文都占2字节

    2、文件(目录)的简单操作:
    锁定一个文件(目录):File file1 = new File("D:\myfiles\a.txt");
    输出文件(目录)的路径:System.out.println(file1);   或   System.out.println(file1.getAbsolutePath());
    文件(目录)名:file1.getName();
    文件(目录)是否存在:file1.exists(); 存在为true,不存在为false
    删除文件(目录):file1.delete();(该方法只能删除文件和空文件夹,不能删除有子文件的目录)
    文件(目录)是否是目录:file1.isDirectory();
    文件(目录)是否是文件:file1.isFile();
    创建目录:file1.mkdir();
    创建多级目录:file1.mkdirs();
    创建文件:file1.createNewFile();
    获取一个目录下的所有文件(目录):File[] files = dir.listFiles();

    二、JAVA中对文件的操作

    1、复制文件:
    注:该方法支持中文处理,适用于TXT、XML、JPG、DOC等多种格式的文件

     1 public static void copyFile(String source, String destination) throws IOException {
     2     FileInputStream in = new FileInputStream(source);
     3     File file = new File(destination);
     4     if (!file.exists())
     5         file.createNewFile();
     6     FileOutputStream out = new FileOutputStream(file);
     7     int c;
     8     byte buffer[] = new byte[1024];
     9     while ((c = in.read(buffer)) != -1) {
    10         for (int i = 0; i < c; i++)
    11             out.write(buffer[i]);
    12     }
    13     in.close();
    14     out.close();
    15 } //测试:copyFile("D:\TASK.txt", "E:\TASK.txt");

    2、文件重命名:

     1 public static void renameFile(String oldName, String newName) {
     2     if (!oldName.equals(newName)) {// 新的文件名和以前文件名不同时,才有必要进行重命名
     3         File oldFile = new File(oldName);
     4         File newFile = new File(newName);
     5         if (newFile.exists())// 若在该目录下已经有一个文件和新文件名相同,则不允许重命名
     6             System.out.println(newName + "已经存在!");
     7         else {
     8             oldFile.renameTo(newFile);
     9         }
    10     }
    11 } //测试:renameFile("D:\TASK.txt", "D:\TASK2.txt");

    3、读文件:
    1)使用BufferedReader类读取:

     1 public static String readFile(String path) throws IOException {
     2     File file = new File(path);
     3     if (!file.exists() || file.isDirectory())
     4         throw new FileNotFoundException();
     5     BufferedReader br = new BufferedReader(new FileReader(file));
     6     String temp = null;
     7     StringBuffer sb = new StringBuffer();
     8     temp = br.readLine();
     9     while (temp != null) {
    10         sb.append(temp + "
    ");
    11         temp = br.readLine();
    12     }
    13     return sb.toString();
    14 }

    2)使用FileInputStream类读取:

     1 public static String readFile(String path) throws IOException {
     2     File file = new File(path);
     3     if (!file.exists() || file.isDirectory())
     4         throw new FileNotFoundException();
     5     FileInputStream fis = new FileInputStream(file);
     6     byte[] buf = new byte[1024];
     7     StringBuffer sb = new StringBuffer();
     8     while ((fis.read(buf)) != -1) {
     9         sb.append(new String(buf));
    10         buf = new byte[1024];// 重新生成,避免和上次读取的数据重复
    11     }
    12     return sb.toString();
    13 }

    4、写文件:

     1 public static void writeFile(String path, String content, String encode)throws IOException {
     2     File file = new File(path);
     3     if (!file.exists())
     4         file.createNewFile();
     5     FileOutputStream out = new FileOutputStream(file, true);
     6     StringBuffer sb = new StringBuffer();
     7     sb.append(content);
     8     out.write(sb.toString().getBytes(encode));
     9     out.close();
    10 } //测试:writeFile("D:\TASK2.txt", "
    这是我加入的内容", "GBK");

    5、删除(有子文件的)目录:
    注:如果方法参数指向的目录有子目录,则删除会失败,因此必须通过递归调用逐层删除文件,最后删除目录

     1 public static void deleteDir(String path) {
     2     File dir = new File(path);
     3     if (dir.exists()) {
     4         File[] tmp = dir.listFiles();
     5         for (int i = 0; i < tmp.length; i++) {
     6             if (tmp[i].isDirectory()) {
     7                 deleteDir(path + "/" + tmp[i].getName());
     8             } else {
     9                 tmp[i].delete();
    10             }
    11         }
    12         dir.delete();
    13     }
    14 } //测试:deleteDir("D:\task");
  • 相关阅读:
    使用eclipse新建一个SWT工程
    C++类的构造函数
    D3D编程的常见报错及解决
    D3D窗口的初始化
    C++联合体的内存使用
    QT程序如何编译
    Restart
    HTML
    信号、槽位及布局
    QT对话框程序
  • 原文地址:https://www.cnblogs.com/blog-wzy/p/5129772.html
Copyright © 2020-2023  润新知