Вот несколько способов записи строки в файл на различных языках программирования:
-
Python:
string = "Hello, world!" with open("filename.txt", "w") as file: file.write(string) -
Java:
import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { String string = "Hello, world!"; try { FileWriter fileWriter = new FileWriter("filename.txt"); fileWriter.write(string); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } -
C++:
#include <iostream> #include <fstream> using namespace std; int main() { string str = "Hello, world!"; ofstream file("filename.txt"); if (file.is_open()) { file << str; file.close(); } return 0; } -
JavaScript (Node.js):
const fs = require('fs'); const string = "Hello, world!"; fs.writeFile('filename.txt', string, (err) => { if (err) throw err; console.log('File written!'); });