Чтобы создать текстовый файл с помощью командной строки на различных языках программирования, вы можете использовать следующие методы:
Python:
# Method 1: Using the built-in open() function
file_path = "example.txt"
with open(file_path, "w") as file:
file.write("This is a text file created using Python.")
print(f"File '{file_path}' created successfully.")
# Method 2: Using the pathlib module
from pathlib import Path
file_path = Path("example.txt")
file_path.write_text("This is a text file created using Python.")
print(f"File '{file_path}' created successfully.")
Java:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class CreateTextFile {
public static void main(String[] args) {
String filePath = "example.txt";
try {
File file = new File(filePath);
FileWriter writer = new FileWriter(file);
writer.write("This is a text file created using Java.");
writer.close();
System.out.println("File '" + filePath + "' created successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
C#:
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "example.txt";
try {
File.WriteAllText(filePath, "This is a text file created using C#.");
Console.WriteLine("File '" + filePath + "' created successfully.");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
}
JavaScript (Node.js):
const fs = require('fs');
const filePath = 'example.txt';
const fileContent = 'This is a text file created using JavaScript (Node.js).';
fs.writeFile(filePath, fileContent, (err) => {
if (err) {
console.error(err);
} else {
console.log(`File '${filePath}' created successfully.`);
}
});