How to open a txt file

I added a file.txt inside my app, now through the menubar, by clicking on “open” I would like to have this file opened. How can I do? How do you open a .txt file?

you dragged the txt file into the project ?

if so you dont need to open the file as the compiler adds it like a constant to your application

  textarea1.text = <name of the txt file shown in the left hand navigator>

I’m sure this is mentioned somewhere in the user guide but I cant find it

But can’t I open the file directly? Without putting it in the TextArea?

http://documentation.xojo.com/api/files/binarystream.html

dim s as string = <name of the txt file shown in the left hand navigator>

Mario, the reference of the text file is the name that appears in the Navigator (as Norman wrote above).

Anyway, what do you want to do with it ?

Imagine the name of the text file is “Text”:
My_String = Left(Text, 100) // Store the first 100 characters in My_String
My_TextArea.Text = Right(Text, 100) // Store the last 100 characters in My_String

What else ?

Why would you need to when the IDE actually makes it behave like a constant ? It basically opens the file and reads it in for you
All you need to do it use it and skip opening the file entirely

@Dave S, @Norman Palardy, @Emile Schwarz Hi all, I want to put a file readme.txt in a folder and when I open I menubar and click on readme, I want to see the readme.txt file. It’s possible?

I write this, I see “Open” but the file not open.

Dim f As FolderItem Dim t As TextOutputStream f = GetOpenFolderItem(ReadMetxt) If f <> Nil then t = TextOutputStream.Append(f) //t.Write(TextField1.Text) t.Close End If

Introduction to Programming Book @ http://documentation.xojo.com/ (main page)

Page 149 of the PDF starts the In Out Chapter).

Good readings.

PS: in the archive file, you will get a projects folder…

I have this error:
Window1.Note, line 39
Parameter “file” expects class FolderItem, but this is type String.
t = TextInputStream.Open(ReadMetxt)

You declared (Dim) ReadMetxt as String. It have to be a TextInputStream.

LR said (http://documentation.xojo.com/api/files/textinputstream.html#textinputstream-open):

Dim f As FolderItem Dim t As TextInputStream f = GetOpenFolderItem("text") // file type defined in File Type Sets Editor If f <> Nil Then t = TextInputStream.Open(f) t.Encoding = Encodings.UTF8 //specify encoding of input stream TextArea1.Text = t.ReadAll t.Close End if

That is why I gave you the pdf / page values (read above).

I added a file.txt inside my app, now through the menubar, by clicking on "open" I would like to have this file opened. How can I do? How do you open a .txt file?

The question holds two different concepts:
a. How do I open a text file from the disk (select / load / display)
b. How do I display a text file I dragged into the project (you do not do that from an Open MenuItem).

What do you really want to do ?

My code above (minutes ago), allows you to load and display text from a user selected file, not one you drop in the project.

As already said to you, your readme file can be displayed using:

my_ReadMe.Text = ReadMetxt

my_ReadMe is the Reference of the TextField (or TextArea) you want to display the dropped file text contents into.
ReadMetxt is the name in the Navigator of the file you dropped

Is it clear now ?

Hi @Emile Schwarz , I have a problem. I’m using your code you sent me. I uploaded a txt file inside the project and when I click on the menubar “readme”, I get the txt file inside the project, I open it, but it still doesn’t open

You wrote: “I open it”

How ?

OK. I just created a .txt file.
I named it ReadMe.txt

I dropped it into the Navigation pane and its name is now ReadMe (and this is an alias).

So, to access to the file text contents, you have to use ReadMe.

I imagine you added a MenuItem called Read Me in the Help menu (of the MenuBar.

In the Read Me MenuHandler, you place the code:

Window1.My_TextArea.Text = ReadMe Return True

Run,
Fire the Read Me MenuItem and enjoy your text.

Your next move will be to save an RTF file as Read Me.rtf and display that (for example). Other solutions exists.

I do what I wrote above in a current project and it works fine.

But I don’t want this in my TextArea. I want to open the file

Again: http://documentation.xojo.com/api/files/binarystream.html

@Luka Jovic: please describe in more detail what you want to do. How does your code look like? Do you have some example we could have a look at?

If it’s not added to the Project via Drag&Drop but a File inside your Project Folder (added via a CopyFile Build Step to your Build) you would like to read from within your App at Runtime, you can do it like this:

Dim f As FolderItem = GetOpenFolderItem("text") // as defined in File Type Sets Editor If f <> Nil Then If f.Exists Then // Be aware that TextInputStream.Open could raise an exception Dim t As TextInputStream Try t = TextInputStream.Open(f) t.Encoding = Encodings.UTF8 TextArea1.Text = t.ReadAll Catch e As IOException MsgBox("Error accessing file.") End Try t.Close End If End If

Source: TextInputStream — Xojo documentation

This is my case, when I click NOTE and not open the file.