Hello
This is a follow up question to 'How do I make sure a directory exists?' The answer to that question was dir := FileSystem workingDirectory / '..' / 'public'. dir ensureCreateDirectory. Now I want to create a writeStream. dir writeStream gives 'Unable to open file' It seems that I need to ask to resolve the relative path first. How does the proper code idiom look like? Regards Hannes |
> On 11 Mar 2018, at 09:42, H. Hirzel <[hidden email]> wrote: > > Hello > > This is a follow up question to 'How do I make sure a directory exists?' > > The answer to that question was > > dir := FileSystem workingDirectory / '..' / 'public'. > dir ensureCreateDirectory. > > > Now I want to create a writeStream. > > dir writeStream > > gives 'Unable to open file' You want to write to a directory ? You probably mean dir := FileSystem workingDirectory / '..' / 'public'. dir ensureCreateDirectory. (dir / 'foo.txt') writeStreamDo: [ :out | out << 'bar' ]. You are probably also looking for dir canonicalize. (dir / 'foo.txt') canonicalize. > It seems that I need to ask to resolve the relative path first. > How does the proper code idiom look like? > > > Regards > > Hannes > |
or try dir := (FileSystem workingDirectory / '..' / 'public' / 'testing' / 'test2.txt') ensureCreateFile. dir writeStream. On Sun, Mar 11, 2018 at 1:16 AM, Sven Van Caekenberghe <[hidden email]> wrote:
|
> On 11 Mar 2018, at 10:39, john pfersich <[hidden email]> wrote: > > or try > > dir := (FileSystem workingDirectory / '..' / 'public' / 'testing' / 'test2.txt') ensureCreateFile. > dir writeStream. Indeed! > On Sun, Mar 11, 2018 at 1:16 AM, Sven Van Caekenberghe <[hidden email]> wrote: > > > > On 11 Mar 2018, at 09:42, H. Hirzel <[hidden email]> wrote: > > > > Hello > > > > This is a follow up question to 'How do I make sure a directory exists?' > > > > The answer to that question was > > > > dir := FileSystem workingDirectory / '..' / 'public'. > > dir ensureCreateDirectory. > > > > > > Now I want to create a writeStream. > > > > dir writeStream > > > > gives 'Unable to open file' > > You want to write to a directory ? > > You probably mean > > dir := FileSystem workingDirectory / '..' / 'public'. > dir ensureCreateDirectory. > (dir / 'foo.txt') writeStreamDo: [ :out | out << 'bar' ]. > > You are probably also looking for > > dir canonicalize. > (dir / 'foo.txt') canonicalize. > > > It seems that I need to ask to resolve the relative path first. > > How does the proper code idiom look like? > > > > > > Regards > > > > Hannes > > > > > |
The first solution given by Sven works fine.
dir := FileSystem workingDirectory / '..' / 'public'. dir ensureCreateDirectory. (dir / 'myFile.txt') writeStreamDo: [ :out | out << 'Hello World!' ]. The second version obj := (FileSystem workingDirectory / '..' / 'public' / 'testing' / 'test2.txt') ensureCreateFile. obj writeStream gives a #streamError in the class FileHandle. This is for Pharo 6.1 On 3/11/18, Sven Van Caekenberghe <[hidden email]> wrote: > > >> On 11 Mar 2018, at 10:39, john pfersich <[hidden email]> wrote: >> >> or try >> >> dir := (FileSystem workingDirectory / '..' / 'public' / 'testing' / >> 'test2.txt') ensureCreateFile. >> dir writeStream. > > Indeed! > >> On Sun, Mar 11, 2018 at 1:16 AM, Sven Van Caekenberghe <[hidden email]> >> wrote: >> >> >> > On 11 Mar 2018, at 09:42, H. Hirzel <[hidden email]> wrote: >> > >> > Hello >> > >> > This is a follow up question to 'How do I make sure a directory >> > exists?' >> > >> > The answer to that question was >> > >> > dir := FileSystem workingDirectory / '..' / 'public'. >> > dir ensureCreateDirectory. >> > >> > >> > Now I want to create a writeStream. >> > >> > dir writeStream >> > >> > gives 'Unable to open file' >> >> You want to write to a directory ? >> >> You probably mean >> >> dir := FileSystem workingDirectory / '..' / 'public'. >> dir ensureCreateDirectory. >> (dir / 'foo.txt') writeStreamDo: [ :out | out << 'bar' ]. >> >> You are probably also looking for >> >> dir canonicalize. >> (dir / 'foo.txt') canonicalize. >> >> > It seems that I need to ask to resolve the relative path first. >> > How does the proper code idiom look like? >> > >> > >> > Regards >> > >> > Hannes >> > >> >> >> > > > |
Hi Hannes,
On 11 March 2018 at 17:49, H. Hirzel <[hidden email]> wrote: > The first solution given by Sven works fine. > > dir := FileSystem workingDirectory / '..' / 'public'. > dir ensureCreateDirectory. > (dir / 'myFile.txt') writeStreamDo: [ :out | out << 'Hello World!' ]. > > > The second version > > obj := (FileSystem workingDirectory / '..' / 'public' / 'testing' > / 'test2.txt') ensureCreateFile. > obj writeStream > > gives a #streamError in the class FileHandle. This is for Pharo 6.1 I don't get any error in 6.1 or 7.0. Maybe supply a call stack? In case you aren't familiar with this, the debugger has a little hamburger menu button to the right of "Through" which has "Copy Stack to Clipboard". Cheers, Alistair |
What OS are you using? My code was executed on Ubuntu 16.04.
Sent from my iPhone Encrypted email at [hidden email] > On Mar 11, 2018, at 10:37, Alistair Grant <[hidden email]> wrote: > > Hi Hannes, > >> On 11 March 2018 at 17:49, H. Hirzel <[hidden email]> wrote: >> The first solution given by Sven works fine. >> >> dir := FileSystem workingDirectory / '..' / 'public'. >> dir ensureCreateDirectory. >> (dir / 'myFile.txt') writeStreamDo: [ :out | out << 'Hello World!' ]. >> >> >> The second version >> >> obj := (FileSystem workingDirectory / '..' / 'public' / 'testing' >> / 'test2.txt') ensureCreateFile. >> obj writeStream >> >> gives a #streamError in the class FileHandle. This is for Pharo 6.1 > > I don't get any error in 6.1 or 7.0. > > Maybe supply a call stack? > > In case you aren't familiar with this, the debugger has a little > hamburger menu button to the right of "Through" which has "Copy Stack > to Clipboard". > > Cheers, > Alistair > |
Hi, do not forget also that there is an entire chapter on FileSystem in here: And I particularly recommend everybody dealing with files to read about how to manage encodings in here: On Mon, Mar 12, 2018 at 7:09 AM, john pfersich <[hidden email]> wrote: What OS are you using? My code was executed on Ubuntu 16.04.
|
On 12 March 2018 at 10:03, Guillermo Polito <[hidden email]> wrote:
> > Hi, > > do not forget also that there is an entire chapter on FileSystem in here: > > http://files.pharo.org/books-pdfs/deep-into-pharo/2013-DeepIntoPharo-EN.pdf > > And I particularly recommend everybody dealing with files to read about how to manage encodings in here: > > http://files.pharo.org/books-pdfs/entreprise-pharo/2016-10-06-EnterprisePharo.pdf Cool! I wasn't aware of the section on character encoding, it's probably the best introduction to character encoding I've read. There is one important mistake in the examples. On page 46 it gives the following code: 'encoding-test.txt' asFileReference writeStreamDo: [ :out | (ZnCharacterWriteStream on: out binary encoding: #utf8) nextPutAll: 'Hello'; space; nextPutAll: 'Ελλάδα'; crlf; nextPutAll: 'Les élèves français'; crlf ]. This won't work reliably as #writeStreamDo: (and #readStreamDo:) use MultiByteFileStreams, which are not binary by default. It really should be (in Pharo 7, I haven't checked pharo 6) #binaryWriteStreamDo: and #binaryReadStreamDo:, i.e.: 'encoding-test.txt' asFileReference binaryWriteStreamDo: [ :out | (ZnCharacterWriteStream on: out binary encoding: #utf8) nextPutAll: 'Hello'; space; nextPutAll: 'Ελλάδα'; crlf; nextPutAll: 'Les élèves français'; crlf ]. Cheers, Alistair (I know I should submit a PR for this, but I'm suffering a personal interrupt stack overflow at the moment) |
Hi Alistair,
> On 12 Mar 2018, at 19:06, Alistair Grant <[hidden email]> wrote: > > On 12 March 2018 at 10:03, Guillermo Polito <[hidden email]> wrote: >> >> Hi, >> >> do not forget also that there is an entire chapter on FileSystem in here: >> >> http://files.pharo.org/books-pdfs/deep-into-pharo/2013-DeepIntoPharo-EN.pdf >> >> And I particularly recommend everybody dealing with files to read about how to manage encodings in here: >> >> http://files.pharo.org/books-pdfs/entreprise-pharo/2016-10-06-EnterprisePharo.pdf > > > Cool! I wasn't aware of the section on character encoding, it's > probably the best introduction to character encoding I've read. Thanks. > There is one important mistake in the examples. On page 46 it gives > the following code: > > 'encoding-test.txt' asFileReference writeStreamDo: [ :out | > (ZnCharacterWriteStream on: out binary encoding: #utf8) > nextPutAll: 'Hello'; space; nextPutAll: 'Ελλάδα'; crlf; > nextPutAll: 'Les élèves français'; crlf ]. > > > This won't work reliably as #writeStreamDo: (and #readStreamDo:) use > MultiByteFileStreams, which are not binary by default. It does work, you missed the #binary message sent to out. > It really should be (in Pharo 7, I haven't checked pharo 6) > #binaryWriteStreamDo: and #binaryReadStreamDo:, i.e.: > > 'encoding-test.txt' asFileReference binaryWriteStreamDo: [ :out | > (ZnCharacterWriteStream on: out binary encoding: #utf8) > nextPutAll: 'Hello'; space; nextPutAll: 'Ελλάδα'; crlf; > nextPutAll: 'Les élèves français'; crlf ]. This is indeed better (but I am not sure #binaryWriteStreamDo: was available in the past). In this case, the #binary message sent to out should be dropped. Sven > Cheers, > Alistair > > (I know I should submit a PR for this, but I'm suffering a personal > interrupt stack overflow at the moment) |
Hi Sven,
On 12 March 2018 at 19:38, Sven Van Caekenberghe <[hidden email]> wrote: > Hi Alistair, > >> On 12 Mar 2018, at 19:06, Alistair Grant <[hidden email]> wrote: >> >> On 12 March 2018 at 10:03, Guillermo Polito <[hidden email]> wrote: >>> >>> Hi, >>> >>> do not forget also that there is an entire chapter on FileSystem in here: >>> >>> http://files.pharo.org/books-pdfs/deep-into-pharo/2013-DeepIntoPharo-EN.pdf >>> >>> And I particularly recommend everybody dealing with files to read about how to manage encodings in here: >>> >>> http://files.pharo.org/books-pdfs/entreprise-pharo/2016-10-06-EnterprisePharo.pdf >> >> >> Cool! I wasn't aware of the section on character encoding, it's >> probably the best introduction to character encoding I've read. > > Thanks. > >> There is one important mistake in the examples. On page 46 it gives >> the following code: >> >> 'encoding-test.txt' asFileReference writeStreamDo: [ :out | >> (ZnCharacterWriteStream on: out binary encoding: #utf8) >> nextPutAll: 'Hello'; space; nextPutAll: 'Ελλάδα'; crlf; >> nextPutAll: 'Les élèves français'; crlf ]. >> >> >> This won't work reliably as #writeStreamDo: (and #readStreamDo:) use >> MultiByteFileStreams, which are not binary by default. > > It does work, you missed the #binary message sent to out. You're right, of course. My apologies (I did miss the #binary). Cheers, Alistair >> It really should be (in Pharo 7, I haven't checked pharo 6) >> #binaryWriteStreamDo: and #binaryReadStreamDo:, i.e.: >> >> 'encoding-test.txt' asFileReference binaryWriteStreamDo: [ :out | >> (ZnCharacterWriteStream on: out binary encoding: #utf8) >> nextPutAll: 'Hello'; space; nextPutAll: 'Ελλάδα'; crlf; >> nextPutAll: 'Les élèves français'; crlf ]. > > This is indeed better (but I am not sure #binaryWriteStreamDo: was available in the past). In this case, the #binary message sent to out should be dropped. > > Sven > >> Cheers, >> Alistair >> >> (I know I should submit a PR for this, but I'm suffering a personal >> interrupt stack overflow at the moment) |
Free forum by Nabble | Edit this page |