@@ -375,6 +375,41 @@ impl File {
375375 OpenOptions :: new ( ) . read ( true ) . open ( path. as_ref ( ) )
376376 }
377377
378+ /// Attempts to open a file in read-only mode with buffering.
379+ ///
380+ /// See the [`OpenOptions::open`] method, the [`BufReader`][io::BufReader] type,
381+ /// and the [`BufRead`][io::BufRead] trait for more details.
382+ ///
383+ /// If you only need to read the entire file contents,
384+ /// consider [`std::fs::read()`][self::read] or
385+ /// [`std::fs::read_to_string()`][self::read_to_string] instead.
386+ ///
387+ /// # Errors
388+ ///
389+ /// This function will return an error if `path` does not already exist.
390+ /// Other errors may also be returned according to [`OpenOptions::open`].
391+ ///
392+ /// # Examples
393+ ///
394+ /// ```no_run
395+ /// #![feature(file_buffered)]
396+ /// use std::fs::File;
397+ /// use std::io::BufRead;
398+ ///
399+ /// fn main() -> std::io::Result<()> {
400+ /// let mut f = File::open_buffered("foo.txt")?;
401+ /// assert!(f.capacity() > 0);
402+ /// for (line, i) in f.lines().zip(1..) {
403+ /// println!("{i:6}: {}", line?);
404+ /// }
405+ /// Ok(())
406+ /// }
407+ /// ```
408+ #[ unstable( feature = "file_buffered" , issue = "none" ) ]
409+ pub fn open_buffered < P : AsRef < Path > > ( path : P ) -> io:: Result < io:: BufReader < File > > {
410+ File :: open ( path) . map ( io:: BufReader :: new)
411+ }
412+
378413 /// Opens a file in write-only mode.
379414 ///
380415 /// This function will create a file if it does not exist,
@@ -404,6 +439,42 @@ impl File {
404439 OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path. as_ref ( ) )
405440 }
406441
442+ /// Opens a file in write-only mode with buffering.
443+ ///
444+ /// This function will create a file if it does not exist,
445+ /// and will truncate it if it does.
446+ ///
447+ /// Depending on the platform, this function may fail if the
448+ /// full directory path does not exist.
449+ ///
450+ /// See the [`OpenOptions::open`] method and the
451+ /// [`BufWriter`][io::BufWriter] type for more details.
452+ ///
453+ /// See also [`std::fs::write()`][self::write] for a simple function to
454+ /// create a file with some given data.
455+ ///
456+ /// # Examples
457+ ///
458+ /// ```no_run
459+ /// #![feature(file_buffered)]
460+ /// use std::fs::File;
461+ /// use std::io::Write;
462+ ///
463+ /// fn main() -> std::io::Result<()> {
464+ /// let mut f = File::create_buffered("foo.txt")?;
465+ /// assert!(f.capacity() > 0);
466+ /// for i in 0..100 {
467+ /// writeln!(&mut f, "{i}")?;
468+ /// }
469+ /// f.flush()?;
470+ /// Ok(())
471+ /// }
472+ /// ```
473+ #[ unstable( feature = "file_buffered" , issue = "none" ) ]
474+ pub fn create_buffered < P : AsRef < Path > > ( path : P ) -> io:: Result < io:: BufWriter < File > > {
475+ File :: create ( path) . map ( io:: BufWriter :: new)
476+ }
477+
407478 /// Creates a new file in read-write mode; error if the file exists.
408479 ///
409480 /// This function will create a file if it does not exist, or return an error if it does. This
0 commit comments