java file read and write code(io包的使用)

package javaBean;

 

import java.io.*;

 

public class UseIO {
 public void ReadAndWrite()
 {
  try
  {

  //创建文本文件对象
   File read = new File("F://clw//3.txt");
   //read.createNewFile(); //新建文本文件
   File write = new File("F://clw//4.txt");
   //write.createNewFile();//新建文本文件
   //创建读取数据的缓冲区对象
   BufferedReader br = new  BufferedReader(new FileReader(read));

  //创建写入缓冲区对象
   BufferedWriter bw = new BufferedWriter(new FileWriter(write));

  //定义接受变量
   String temp = null;

  //读取行数据
   temp = br.readLine();

  //循环读取文本内容
   while(temp!=null)
   {

    //写入数据到目标文件缓冲对象
    bw.write(temp+"/r/n");

   //继续读取下一行
    temp = br.readLine();
   }

   //关闭缓冲区资源
   bw.close();
   br.close();
  }

  //捕获可能发生的异常(文件不存在异常)
  catch(FileNotFoundException e)
  {
   System.out.print(e.getMessage());
  }

  //读入读出异常
  catch(IOException e)
  {
   System.out.println(e.getMessage());
  }
 }
 public static void main(String[] args)
 {
  UseIO ui = new UseIO();
  ui.ReadAndWrite();
  System.out.println("read and write over!");
  
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To implement the file read and write function of Windows computers using Java, you can use the Java File API. Here's an example code snippet to read and write a file: ```java import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Scanner; public class FileReadWriteExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter file path: "); String filePath = scanner.nextLine(); File file = new File(filePath); try { // Read file byte[] fileContent = Files.readAllBytes(file.toPath()); System.out.println("File content:\n" + new String(fileContent)); // Write file System.out.print("Enter new content: "); String newContent = scanner.nextLine(); Files.write(file.toPath(), newContent.getBytes()); System.out.println("File written successfully."); } catch (IOException e) { // Handle file access denied error if (e.getMessage().contains("Access is denied")) { System.out.println("Access denied. Do you want to authorize? (Y/N)"); String response = scanner.nextLine(); if (response.equalsIgnoreCase("Y")) { try { Files.setAttribute(file.toPath(), "dos:hidden", false); Files.setAttribute(file.toPath(), "dos:readonly", false); Files.write(file.toPath(), newContent.getBytes()); System.out.println("File authorized and written successfully."); } catch (IOException ex) { System.out.println("Error authorizing file."); } } else { System.out.println("File access denied."); } } else { System.out.println("Error reading/writing file."); } } } } ``` In this code, we first prompt the user to enter the file path. We then read the file using the `Files.readAllBytes` method and display its content. We then prompt the user to enter new content and write it to the file using the `Files.write` method. If an `IOException` is thrown during writing, we check if it's an "Access is denied" error. If it is, we prompt the user to authorize the file by setting the "hidden" and "readonly" attributes to false using the `Files.setAttribute` method. We then attempt to write the file again and display a success or error message accordingly.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值