2008-11-11 16:41:27 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								USING:  help.markup  help.syntax  io  strings  arrays  io.backend 
							 
						 
					
						
							
								
									
										
										
										
											2009-01-29 01:08:40 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								io.files.private 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-04-23 04:48:32 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsection "io.files.examples"  }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 15:59:15 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"File streams:" 
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection <file-reader> }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection <file-writer> }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection <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:" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection file-contents }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection set-file-contents }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection file-lines }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection set-file-lines }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-27 15:59:15 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Utility combinators:" 
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsection with-file-reader }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $subsection with-file-writer }
							 
						 
					
						
							
								
									
										
										
										
											2008-04-06 21:09:31 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsection 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 
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "quot"  "a 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 
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "quot"  "a 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 
							 
						 
					
						
							
								
									
										
										
										
											2008-02-16 17:25:45 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $values { "path"  "a pathname string"  } { "encoding"  "an encoding descriptor"  } { "quot"  "a 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."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-04-03 22:39:52 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $errors "Throws an error if the file cannot be opened for reading."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								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."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ set-file-lines file-lines set-file-contents file-contents } related-words
							 
						 
					
						
							
								
									
										
										
										
											2008-02-06 21:04:46 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								HELP:  exists? 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "path"  "a pathname string"  } { "?"  "a boolean"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Tests if the file named by "  { $snippet "path"  } " exists."  } ;