Java IO——File类



/**
 * Created by LiuHuiChao on 2016/3/8.
 */
public class FileTest {
    /*
    * java.io.file类用于表示文件(目录)
    * File类只用于表示文件(目录)的信息,不能用于文件的访问
    *
    * */
    public static void main(String[] args){
        //File file=new File("H:\\LHC");
        File file=new File("H:"+File.separator+"Lhc");//File.separator用户获取系统分隔符
        System.out.println("判断文件是否存在:--"+file.exists());
        if(!file.exists()){
            file.mkdir();//创建目录
        }else{
           // file.delete();  //删除
        }

        //判断是否是一个目录
        System.out.println("判断是否是一个目录--"+file.isDirectory());
        //判断是否是一个文件
        System.out.println("判断是否是一个文件--"+file.isFile());//如果不是目录或不存在,返回false

        /*创建文件对象*/
       // File file2=new File("H:"+File.separator+"LHC"+File.separator+"diary.txt");
        File file2=new File("H:"+File.separator+"LHC","diary.txt");
        if(!file2.exists()){
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
           // file2.delete();
        }

        /*常用的file对象的api*/
        System.out.println(file2);//打印的是file 的tostring
        System.out.println(file2.getAbsolutePath());
        System.out.println(file2.getName());
        System.out.println(file2.getParent());//返回父目录路径、
        System.out.println(file2.getParentFile().getAbsolutePath());

    }
}


/**
 * Created by LiuHuiChao on 2016/3/8.
 *
 * 列出file类的一些常用操作,比如,过滤,遍历等操作
 */
public class FileUtils {
    /*
        列出指定目录下(包括其子目录)的所有文件
     */
    public static void listDirectory(File dir) throws IOException, IllegalAccessException {
        if(!dir.exists()){
            throw new IllegalArgumentException("目录--"+dir+"--不存在!!");
        }
        if(!dir.isDirectory()){
            throw new IllegalArgumentException(dir+"不是目录!");
        }
        /*list方法用于列出当前目录下的子目录和文件
        * 直接子文件名称
        * */
       /* String[] fileNames=dir.list();
        for (String s:fileNames){
            System.out.println(dir+File.separator+s);
        }*/

        /*如果要遍历子目录下的东西,就要构造File对象做递归操作
        * File提供了直接返回File对象的api
        * */
        File[] files=dir.listFiles();//返回的是直接子目录的抽象
       if(!files!=null && files.length>0){
           for (File file:files){
               if (file.isDirectory()){
                   //递归
                   listDirectory(file);
               }else{
                   System.out.println(file);
               }

           }
       }

    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JavaIO流和字符串操作来修改txt文件中某一行的内容。具体步骤如下: 1. 创建一个File对象来表示要修改的txt文件。 2. 创建一个BufferedReader对象来读取txt文件内容。 3. 创建一个StringBuilder对象来存储修改后的txt文件内容。 4. 通过BufferedReader对象逐行读取txt文件内容,并在需要修改的行进行字符串替换操作。 5. 将修改后的内容写入StringBuilder对象中。 6. 关闭BufferedReader对象。 7. 创建一个FileWriter对象来写入修改后的内容。 8. 将StringBuilder对象中的内容写入txt文件。 9. 关闭FileWriter对象。 下面是一个修改txt文件中某一行内容的示例代码: ```java import java.io.*; public class ModifyTxtFile { public static void main(String[] args) { String filePath = "test.txt"; // 要修改的txt文件路径 int lineNumber = 3; // 要修改的行号 String newLineContent = "This is the new line content."; // 修改后的内容 try { // 创建File对象 File file = new File(filePath); // 创建BufferedReader对象 BufferedReader reader = new BufferedReader(new FileReader(file)); // 创建StringBuilder对象 StringBuilder sb = new StringBuilder(); // 逐行读取txt文件内容并进行替换操作 String line; int lineNum = 1; while ((line = reader.readLine()) != null) { if (lineNum == lineNumber) { sb.append(newLineContent).append(System.lineSeparator()); // 添加修改后的内容 } else { sb.append(line).append(System.lineSeparator()); // 添加原内容 } lineNum++; } // 关闭BufferedReader对象 reader.close(); // 创建FileWriter对象 FileWriter writer = new FileWriter(file); // 将StringBuilder对象中的内容写入txt文件 writer.write(sb.toString()); // 关闭FileWriter对象 writer.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们将第3行的内容修改为"This is the new line content."。你可以根据自己的需求修改这个示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值