编写一个程序,它读取一个文件以判断其中的字节数,然后使用0覆盖所有的字节

创建了一个类,在构造方法中初始化用户输入的要覆盖的文件;在resetChar()方
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class File15_9 {
	String sourceName = "";		//要覆盖的文件名
	
	public File15_9() {		//在构造函数中实现获取用户输入的文件路径
		sourceName = getString();
	}
	
	public void resetChar() throws IOException {
		File sourceFile = new File(sourceName);		//创建File对象,实现对sourcenName的引用
		FileInputStream fis = new FileInputStream(sourceFile);		//打开一个到实际文件的链接
		
		int count = 0;		//统计文件中字符的个数
		int ch = 0;		//用于判断是否读到文件尾
		do {
			ch = fis.read();	//读取
			if(ch != -1) {
				count++;
			}
		}while(ch != -1);
		fis.close();
		FileOutputStream fos = new FileOutputStream(sourceFile);	//创建一个向指定File对象表示的文件中写入数据的文件输出流
		int temp = '0';
		while((count--) != 0) {
			fos.write(temp);
		}
		fos.close();
	}
	
	private String getString() {	//获取用户输入的要覆盖的文件路径
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = "";
		try {
			System.out.println("请输入要进行覆盖的文件的完整路径");
			str = br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
}

法中实现覆盖。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值