- Python:
Python предоставляет простой и элегантный способ копирования содержимого файла. Вот пример:
with open('input.txt', 'r') as source_file:
with open('output.txt', 'w') as destination_file:
destination_file.write(source_file.read())
- JavaScript:
В JavaScript для этого можно использовать модуль «Файловая система». Вот пример:
const fs = require('fs');
fs.readFile('input.txt', 'utf8', (err, data) => {
if (err) throw err;
fs.writeFile('output.txt', data, (err) => {
if (err) throw err;
console.log('File contents copied successfully!');
});
});
- Java:
Java предоставляет несколько способов копирования содержимого файла. Вот пример использования классов BufferedInputStream и BufferedOutputStream:
import java.io.*;
try {
FileInputStream sourceFile = new FileInputStream("input.txt");
FileOutputStream destinationFile = new FileOutputStream("output.txt");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = sourceFile.read(buffer)) != -1) {
destinationFile.write(buffer, 0, bytesRead);
}
sourceFile.close();
destinationFile.close();
System.out.println("File contents copied successfully!");
} catch (IOException e) {
e.printStackTrace();
}
- C#:
В C# вы можете использовать классы StreamReader и StreamWriter для копирования содержимого файла. Вот пример:
using System;
using System.IO;
try
{
using (StreamReader sourceFile = new StreamReader("input.txt"))
{
using (StreamWriter destinationFile = new StreamWriter("output.txt"))
{
destinationFile.Write(sourceFile.ReadToEnd());
}
}
Console.WriteLine("File contents copied successfully!");
}
catch (IOException e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
- Bash:
В Bash вы можете использовать команду cat для копирования содержимого файла. Вот пример:
cat input.txt > output.txt
echo "File contents copied successfully!"