如何写入覆盖以前内容的文件
How to write to file with overriding previous content
文件中有写入数据的方法:
to output:someThing toFile:someFile
try
tell application "System Events" to set myname to get name of (path to me)
set AppleScript's text item delimiters to myname
set pathToFolder to text item 1 of ((path to me) as text)
set outputFileWithPath to (((pathToFolder) as text) & someFile)
set fileRef to (open for access outputFileWithPath with write permission)
write (someThing) to fileRef starting at 0
close access fileRef
on error errmess
log errmess
try -- make sure file is closed on any error
close access fileRef
end try
end try
end output:toFile:
set outputFile to "test1"
(my output:"first second third" toFile:outputFile)
(my output:"forth" toFile:outputFile)
这个例子中的问题是:最后,我将有一个文件:
forth second third
目标是只有最后一条记录。在这个例子中:
forth
write (someThing) to fileRef starting at [...]
结构是为了将新数据插入文件:它会覆盖文件中该点的数据而不更改任何其他内容。如果你想在某一点之后删除文件的内容,使用set eof fileRef to [some integer]
。 'eof'这里代表'end of file'。您的代码将是:
set fileRef to (open for access outputFileWithPath with write permission)
set eof fileRef to 0
write (someThing) to fileRef
close access fileRef
文件中有写入数据的方法:
to output:someThing toFile:someFile
try
tell application "System Events" to set myname to get name of (path to me)
set AppleScript's text item delimiters to myname
set pathToFolder to text item 1 of ((path to me) as text)
set outputFileWithPath to (((pathToFolder) as text) & someFile)
set fileRef to (open for access outputFileWithPath with write permission)
write (someThing) to fileRef starting at 0
close access fileRef
on error errmess
log errmess
try -- make sure file is closed on any error
close access fileRef
end try
end try
end output:toFile:
set outputFile to "test1"
(my output:"first second third" toFile:outputFile)
(my output:"forth" toFile:outputFile)
这个例子中的问题是:最后,我将有一个文件:
forth second third
目标是只有最后一条记录。在这个例子中:
forth
write (someThing) to fileRef starting at [...]
结构是为了将新数据插入文件:它会覆盖文件中该点的数据而不更改任何其他内容。如果你想在某一点之后删除文件的内容,使用set eof fileRef to [some integer]
。 'eof'这里代表'end of file'。您的代码将是:
set fileRef to (open for access outputFileWithPath with write permission)
set eof fileRef to 0
write (someThing) to fileRef
close access fileRef