문제

파일에서 원시 바이트 배열을 읽고 해당 바이트 배열을 새 파일에 다시 쓸 수 있습니까?

도움이 되었습니까?

해결책

(편집 : 질문이 바뀌 었음에 유의하십시오. 언급하지 않았습니다. byte[] 초기에; 보다 개정 1)

잘, File.Copy 떠오른다. 그러나 그렇지 않으면 이것은 A처럼 들립니다 Stream 대본:

    using (Stream source = File.OpenRead(inPath))
    using (Stream dest = File.Create(outPath)) {
        byte[] buffer = new byte[2048]; // pick size
        int bytesRead;
        while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
            dest.Write(buffer, 0, bytesRead);
        }
    }

다른 팁

byte[] data = File.ReadAllBytes(path1);
File.WriteAllBytes(path2, data);

당신은 알고 있습니까? Textreader 그리고 텍스트 작가, 그리고 그들의 후손들 스트림 리더 그리고 스트림 라이터? 나는 이것이 인코딩을 처리하기 때문에 문제를 해결할 것이라고 생각합니다. BinaryReader는 인코딩이나 텍스트에 대해 알지 못하며 바이트와 관련이 있습니다.

파일에서 텍스트를 읽는 방법

파일에 텍스트를 작성하는 방법

이것은 IO 및 인코딩을 제출하는 훌륭한 소개입니다.

최신 답변 추가,

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        source.CopyTo(dest);
    }
}

선택적으로 버퍼 크기를 지정할 수 있습니다

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        source.CopyTo(dest, 2048); // or something bigger.
    }
}

또는 다른 스레드에서 작업을 수행 할 수 있습니다.

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        await source.CopyToAsync(dest);
    }
}

메인 스레드가 WPF 및 Windows 스토어 앱과 같은 다른 작업을 수행해야 할 때 유용합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top