bufferstream缓冲流复制文件

       using namespace System::IO;

 

 

  //选择目标文件
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 if(this->saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
                     this->textBox2->Text = this->saveFileDialog1->FileName;
             }

             //选择源文件
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
             if(this->openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
                 this->textBox1->Text = this->openFileDialog1->FileName;
         }


         //开始复制
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
             String^ FileSource;
             String^ FileTarget;
             Stream^ FileInputStream;
             Stream^ FileOutputStream;
             BufferedStream^ InputBufferStream;
             BufferedStream^ OutputBufferStream;
             array<unsigned char>^ myBuffer = gcnew array<unsigned char>(1024);
             int MyCount;
             FileSource = this->textBox1->Text;
             if(!File::Exists(FileSource)){
                 MessageBox::Show("源文件不存在", "", MessageBoxButtons::OK, MessageBoxIcon::Information);
                 return;
             }
             FileTarget = this->textBox2->Text;
             if(FileSource->Length < 1){
                 MessageBox::Show("请指定目标文件位置", "", MessageBoxButtons::OK, MessageBoxIcon::Information);
                 return;
             }
             try{
                 FileInputStream = File::OpenRead(FileSource);
                 FileOutputStream = File::OpenWrite(FileTarget);
                 InputBufferStream = gcnew BufferedStream(FileInputStream);
                 OutputBufferStream = gcnew BufferedStream(FileOutputStream);
                 MyCount = InputBufferStream->Read(myBuffer, 0 ,1024);
                 while(MyCount > 0){
                     OutputBufferStream->Write(myBuffer, 0, MyCount);
                     MyCount = InputBufferStream->Read(myBuffer, 0, 1024);
                 }
                 OutputBufferStream->Flush();
                 InputBufferStream->Close();
                 OutputBufferStream->Close();
                 MessageBox::Show("success", "", MessageBoxButtons::OK, MessageBoxIcon::Information);
             }
             catch(Exception^ eee){
                MessageBox::Show(eee->Message, "", MessageBoxButtons::OK, MessageBoxIcon::Information);
             }
         }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用字节和字符分别复制文件的示例代码: 使用字节复制文件: ```java import java.io.*; public class ByteStreamCopy { public static void main(String[] args) { String srcFile = "source.txt"; String destFile = "destination.txt"; try (FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile)) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } System.out.println("File copied successfully using byte stream."); } catch (IOException e) { e.printStackTrace(); } } } ``` 使用字符复制文件: ```java import java.io.*; public class CharacterStreamCopy { public static void main(String[] args) { String srcFile = "source.txt"; String destFile = "destination.txt"; try (FileReader reader = new FileReader(srcFile); FileWriter writer = new FileWriter(destFile)) { char[] buffer = new char[1024]; int length; while ((length = reader.read(buffer)) > 0) { writer.write(buffer, 0, length); } System.out.println("File copied successfully using character stream."); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上两个示例代码中,都使用了try-with-resources语句来自动关闭文件。在使用字节复制文件时,定义了一个长度为1024的字节数组作为缓冲区,每次读取文件时将数据读入缓冲区,然后再将缓冲区中的数据写入目标文件。在使用字符复制文件时,也定义了一个长度为1024的字符数组作为缓冲区,使用reader.read(buffer)方法读取文件数据,然后使用writer.write(buffer)方法将数据写入目标文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值