2014-05-23 23:20:15 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								USING:  help.markup  help.syntax  io  kernel  quotations  sequences  ;
  
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								IN:  io.files  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2009-04-23 04:48:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								ARTICLE: "io.files.examples"  "Examples of reading and writing files" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Sort the lines in a file and write them back to the same file:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $code
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "USING: io io.encodings.utf8 io.files sequences sorting ;" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "\"lines.txt\" utf8 [ file-lines natural-sort ] 2keep set-file-lines" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Read 1024 bytes from a file:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $code
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "USING: io io.encodings.binary io.files ;" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "\"data.bin\" binary [ 1024 read ] with-file-reader" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-12-14 21:03:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								ARTICLE: "io.files"  "Reading and writing files" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections "io.files.examples"  }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 15:59:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"File streams:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    <file-reader>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    <file-writer>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    <file-appender>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2008-04-06 21:09:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"Reading and writing the entire contents of a file; this is only recommended for smaller files:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    file-contents
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    set-file-contents
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:03:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    change-file-contents
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    file-lines
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    set-file-lines
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:03:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    change-file-lines
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 15:59:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"Utility combinators:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    with-file-reader
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    with-file-writer
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    with-file-appender
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 15:59:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 17:31:13 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								ABOUT: "io.files" 
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  <file-reader>  
						 
					
						
							
								
									
										
										
										
											2008-12-14 21:03:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "stream"  "an input stream"  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Outputs an input stream for reading from the specified pathname using the given encoding."  }
							 
						 
					
						
							
								
									
										
										
										
											2009-01-28 20:19:25 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $notes "Most code should use "  { $link with-file-reader } " instead, to ensure the stream is properly disposed of after."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file is unreadable."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  <file-writer>  
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "stream"  "an output stream"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Outputs an output stream for writing to the specified pathname using the given encoding. The file's length is truncated to zero."  }
							 
						 
					
						
							
								
									
										
										
										
											2009-01-28 20:19:25 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $notes "Most code should use "  { $link with-file-writer } " instead, to ensure the stream is properly disposed of after."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for writing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  <file-appender>  
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "stream"  "an output stream"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Outputs an output stream for writing to the specified pathname using the given encoding. The stream begins writing at the end of the file."  }
							 
						 
					
						
							
								
									
										
										
										
											2009-01-28 20:19:25 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $notes "Most code should use "  { $link with-file-appender } " instead, to ensure the stream is properly disposed of after."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for writing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-15 23:20:31 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  with-file-reader  
						 
					
						
							
								
									
										
										
										
											2014-05-23 23:20:15 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "quot"  quotation } }
							 
						 
					
						
							
								
									
										
										
										
											2008-05-05 03:19:25 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Opens a file for reading and calls the quotation using "  { $link with-input-stream  } "."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-06 21:04:46 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file is unreadable."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-15 23:20:31 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  with-file-writer  
						 
					
						
							
								
									
										
										
										
											2014-05-23 23:20:15 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "quot"  quotation } }
							 
						 
					
						
							
								
									
										
										
										
											2008-05-05 03:19:25 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Opens a file for writing using the given encoding and calls the quotation using "  { $link with-output-stream  } "."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-06 21:04:46 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for writing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  with-file-appender  
						 
					
						
							
								
									
										
										
										
											2014-05-23 23:20:15 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "quot"  quotation } }
							 
						 
					
						
							
								
									
										
										
										
											2008-05-05 03:19:25 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Opens a file for appending using the given encoding and calls the quotation using "  { $link with-output-stream  } "."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for writing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-04-03 22:39:52 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  set-file-lines  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "seq"  "an array of strings"  } { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Sets the contents of a file to the strings with the given encoding."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for writing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  file-lines  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "seq"  "an array of strings"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Opens the file at the given path using the given encoding, and returns a list of the lines in that file."  }
							 
						 
					
						
							
								
									
										
										
										
											2013-10-15 07:27:49 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $examples
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  { $example
							 
						 
					
						
							
								
									
										
										
										
											2014-04-13 19:26:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    "USING: io.files io.encodings.utf8 prettyprint sequences ;" 
							 
						 
					
						
							
								
									
										
										
										
											2013-10-15 07:27:49 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    "\"resource:core/kernel/kernel.factor\" utf8 file-lines first ." 
							 
						 
					
						
							
								
									
										
										
										
											2014-04-13 19:26:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    "\"! Copyright (C) 2004, 2009 Slava Pestov.\"" 
							 
						 
					
						
							
								
									
										
										
										
											2013-10-15 07:27:49 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2008-04-03 22:39:52 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for reading."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:03:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  change-file-lines  
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:07:01 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "quot"  quotation } }
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:03:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Reads the file lines, transforms the file lines, and writes them back to the same file name."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for writing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-04-03 22:39:52 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  set-file-contents  
						 
					
						
							
								
									
										
										
										
											2009-01-29 01:08:40 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "seq"  sequence  } { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Sets the contents of a file to a sequence with the given encoding."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for writing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:03:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  change-file-contents  
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:07:01 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "quot"  quotation } }
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:03:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Reads the file, transforms the file contents, and writes it back to the same file name."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for writing."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								HELP:  file-contents  
						 
					
						
							
								
									
										
										
										
											2009-01-29 01:08:40 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "seq"  sequence  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Opens the file at the given path using the given encoding, and the contents of that file as a sequence."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-04-03 22:39:52 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $errors "Throws an error if the file cannot be opened for reading."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 18:03:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ set-file-lines file-lines change-file-lines set-file-contents file-contents change-file-contents } related-words
							 
						 
					
						
							
								
									
										
										
										
											2008-02-06 21:04:46 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								HELP:  exists?  
						 
					
						
							
								
									
										
										
										
											2014-05-23 23:20:15 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "path"  "a pathname string"  } { "?"  boolean  } }
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Tests if the file named by "  { $snippet "path"  } " exists."  } ;