{"id":4000,"date":"2022-12-21T21:55:20","date_gmt":"2022-12-21T21:55:20","guid":{"rendered":"https:\/\/www.gubatron.com\/blog\/?p=4000"},"modified":"2022-12-21T21:55:20","modified_gmt":"2022-12-21T21:55:20","slug":"what-is-the-rust-equivalent-to-javas-printwriter","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/what-is-the-rust-equivalent-to-javas-printwriter\/","title":{"rendered":"What is the Rust equivalent to Java&#8217;s PrintWriter?"},"content":{"rendered":"<p>In Rust, the equivalent of Java&#8217;s <code>PrintWriter<\/code> is the <code>std::io::Write<\/code> trait, which is implemented by a number of types that can be used to write data to an output stream, such as a file or a network socket.<\/p>\n<p>To use <code>Write<\/code> to write text to an output stream, you can use the <code>write_all<\/code> method, which takes a byte slice as an argument and writes it to the output stream.<\/p>\n<p>You can convert a <code>string<\/code> to a <code>byte<\/code> slice using the <code>as_bytes<\/code> method.<\/p>\n<p>Here is an example of how you might use Write to write text to a file:<\/p>\n<pre><code class=\"language-rust\">use std::fs::File;\nuse std::io::Write;\n\nfn main() -&gt; std::io::Result&lt;()&gt; {\n    let mut file = File::create(\"output.txt\")?;\n    file.write_all(b\"Hello, world!\")?;\n    Ok(())\n}\n<\/code><\/pre>\n<p>If you want to use a buffered writer, similar to <code>PrintWriter<\/code>, you can use the <code>BufWriter<\/code> type from the <code>std::io::BufWriter<\/code> module.<\/p>\n<p>This type wraps a <code>Write<\/code> implementation and buffers the output, improving performance by reducing the number of calls to the underlying write operation.<\/p>\n<p>Here is an example of how you might use <code>BufWriter<\/code> to write text to a file:<\/p>\n<pre><code class=\"language-rust\">use std::fs::File;\nuse std::io::{BufWriter, Write};\n\nfn main() -&gt; std::io::Result&lt;()&gt; {\n    let file = File::create(\"output.txt\")?;\n    let mut writer = BufWriter::new(file);\n    writer.write_all(b\"Hello, world!\")?;\n    writer.flush()?;\n    Ok(())\n}\n<\/code><\/pre>\n<p>You can also use the <code>writeln!<\/code> macro from the <code>std::fmt<\/code> module to write a line of text to an output stream.<\/p>\n<p>This macro takes a <code>Write<\/code> implementation and a format string as arguments, and writes the formatted string to the output stream followed by a newline character.<\/p>\n<p>Here is an example of how you might use <code>writeln!<\/code> to write a line of text to a file:<\/p>\n<pre><code class=\"language-rust\">use std::fs::File;\nuse std::io::Write;\n\nfn main() -&gt; std::io::Result&lt;()&gt; {\n    let mut file = File::create(\"output.txt\")?;\n    writeln!(file, \"Hello, world!\")?;\n    Ok(())\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Rust, the equivalent of Java&#8217;s PrintWriter is the std::io::Write trait, which is implemented by a number of types that can be used to write data to an output stream, such as a file or a network socket. To use Write to write text to an output stream, you can use the write_all method, which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4001,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[583,1659],"class_list":["post-4000","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","tag-java","tag-rust"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-12w","jetpack-related-posts":[{"id":4129,"url":"https:\/\/www.gubatron.com\/blog\/introducing-uninews-a-universal-news-scraper-in-rust\/","url_meta":{"origin":4000,"position":0},"title":"Introducing Uninews: A Universal News Scraper in Rust","author":"gubatron","date":"February 17, 2025","format":false,"excerpt":"The internet is overflowing with news, but extracting clean, readable content from articles can be a tedious task. Whether you're aggregating news for personal consumption, research, or AI training, automating this process is a must. Enter Uninews, a powerful, lightweight, and efficient Rust-based news scraper that simplifies content extraction and\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":1269,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-quick-dirty-hexviewer-updated\/","url_meta":{"origin":4000,"position":1},"title":"How to make a Quick &#038; Dirty HexViewer &#8211; Updated","author":"gubatron","date":"April 20, 2009","format":false,"excerpt":"After I received comments from ispak on Flickr I made a few fixes. ispak pointed out that it was a bad idea reading one byte at the time, also I had a gay ass try\/catch that didn't catch any exception :p So now I read 16 byte chunks, and I\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"HexViewer - r2","src":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=525%2C300 1.5x"},"classes":[]},{"id":4135,"url":"https:\/\/www.gubatron.com\/blog\/the-curious-case-of-inconsistent-cargo-fmt-formatting-and-how-to-fix-it\/","url_meta":{"origin":4000,"position":2},"title":"The Curious Case of Inconsistent cargo fmt Formatting (and How to Fix It)","author":"gubatron","date":"February 17, 2025","format":false,"excerpt":"Have you ever run into a situation where\u00a0cargo fmt, Rust's code formatter, produces different output on different machines, even though you're working on the same project? This can be incredibly frustrating, especially when you're trying to maintain consistent code style across a team or between your own development environments. I\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":980,"url":"https:\/\/www.gubatron.com\/blog\/javareflection-notes-invoking-a-static-main-method-from-a-dinamically-loaded-class\/","url_meta":{"origin":4000,"position":3},"title":"Java\/Reflection notes: Invoking a static main() method from a dinamically loaded class.","author":"gubatron","date":"November 22, 2008","format":false,"excerpt":"Maybe for some wild reason, your Java application will need to execute a pre launcher that won't know about the Main class it's supposed to invoke until it's being executed. For example, you have distributed your Java application but you used pack200 to compress your jars, and your new application\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4178,"url":"https:\/\/www.gubatron.com\/blog\/mining-git-history-to-build-developer-agent-personas\/","url_meta":{"origin":4000,"position":4},"title":"Mining Git History to Build Developer Agent Personas","author":"gubatron","date":"February 22, 2026","format":false,"excerpt":"A new software engineering practice for the age of agentic teams and an honest look at what it costs There is a new kind of software team forming inside repositories everywhere. It is not made of humans alone. It is made of humans and AI agents working together, agents that\u2026","rel":"","context":"In &quot;AI&quot;","block_context":{"text":"AI","link":"https:\/\/www.gubatron.com\/blog\/category\/ai\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2026\/02\/download.jpeg?fit=1168%2C784&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3376,"url":"https:\/\/www.gubatron.com\/blog\/what-to-do-when-lighttpd-wont-start-and-wont-give-out-any-error-output\/","url_meta":{"origin":4000,"position":5},"title":"What to do when lighttpd won&#8217;t start and won&#8217;t give out any error output?","author":"gubatron","date":"February 4, 2015","format":false,"excerpt":"So you upgraded your server, or just all of a sudden you try to start lighttpd, it says the server started ok, but you check and there's no lighttpd process. You then go after your error log files, and nothing... what the fuck is happening? try this to attempt to\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/4000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=4000"}],"version-history":[{"count":1,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/4000\/revisions"}],"predecessor-version":[{"id":4002,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/4000\/revisions\/4002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media\/4001"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=4000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=4000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=4000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}