实验题目:文件和流

实验题目:文件和流


目的与要求:熟悉java.io包中的API的使用


内容:
编写方法,完成指定文件的复制和剪切;使用BufferedReader和BufferedWriter完成。


问题:
流操作的一般步骤?




import java.io.*;
class FileReaderTest
{
public static void main(String[] args) throws IOException
{
File fi= new File();
fi.copy();
fi.cut();
}
}
class File{
void copy() throws IOException {
FileReader  fr = new FileReader("F:\\java程序\\Switch.java");
FileWriter fo = new FileWriter("F:\\java程序\\1.txt");
BufferedReader bu = new BufferedReader(fr);
String b = null;
while((b=bu.readLine())!=null){
fo.write(b);
fo.flush();
}
bu.close();
fo.close();
}
void cut()  {
try{ 

FileReader fr = new FileReader("F:\\java程序\\Switch.java");
FileWriter fo = new FileWriter("F:\\java程序\\1.txt");
BufferedReader bu = new BufferedReader(fr);
String b = null;
while((b=bu.readLine())!=null){
fo.write(b);
fo.flush();
}
bu.close();
fo.close();
bu.delet();
}catch(IOException e){
System.out.println(e);
}




}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你一个实现的思路,你可以参考一下: 1. 打开源程序文件和目标文件,分别为输入和输出。 2. 从输入中读取每一个字符,判断是否是注释符号(//或/*),如果是,则忽略该行或直到遇到*/结束。如果不是注释,则将该字符写入输出中。 3. 循环读取字符并写入输出中,直到源程序文件的结尾。 4. 关闭输入和输出。 下面是一段示例代码: ```cpp #include <iostream> #include <fstream> using namespace std; int main() { ifstream input("source.cpp"); // 打开源程序文件 ofstream output("output.cpp"); // 创建目标文件 char c, next_c; bool is_comment = false; // 是否处于注释中的标志 while (input.get(c)) { // 从输入中读取每一个字符 if (!is_comment) { // 如果不处于注释中 if (c == '/') { // 判断是否为注释符号 input.get(next_c); if (next_c == '/') { is_comment = true; // //'/'为行注释符号,进入行注释模式 } else if (next_c == '*') { is_comment = true; // // '/*'为段注释符号,进入段注释模式 } else { output << c << next_c; // 不是注释符号,写入输出中 } } else { output << c; // 不是注释符号,写入输出中 } } else { // 如果处于注释中 if (c == '*' && input.peek() == '/') { // 判断是否为段注释结束符号 input.get(next_c); is_comment = false; // 退出段注释模式 } } } input.close(); // 关闭输入 output.close(); // 关闭输出 return 0; } ``` 以上代码仅供参考,实际情况还需要根据具体需求进行一些修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值