How to Use javascrip To Read File Information

Source: Internet
Author: User

There are two main types of File Processing:

Create, add, or delete data and read files
Move, copy, and delete objects
Create a file
There are three methods to create an empty text file (sometimes called a text stream.

The first method is to use the createtextfile method. The following example demonstrates how to use the createtextfile method to create a text file:

[VBScript]
Dim FSO, F1
Set FSO = Createobject ("scripting. FileSystemObject ")
Set F1 = FSO. createtextfile ("C:/testfile.txt", true)
[JScript]
VaR FSO, F1;
FSO = new activexobject ("scripting. FileSystemObject ");
F1 = FSO. createtextfile ("C: // testfile.txt", true );
The second method for creating a text file is to use the opentextfile method of the FileSystemObject object and set the forwriting flag.

[VBScript]
Dim FSO, TS
Const forwriting = 2
Set FSO = Createobject ("scripting. FileSystemObject ")
Set Ts = FSO. opentextfile ("C:/test.txt", forwriting, true)
[JScript]
VaR FSO, TS;
VaR forwriting = 2;
FSO = new activexobject ("scripting. FileSystemObject ");
TS = FSO. opentextfile ("C: // test.txt", forwriting, true );
The third way to create a text file is to use the openastextstream method and set the forwriting flag. To use this method, use the following code:

[VBScript]
Dim FSO, F1, TS
Const forwriting = 2
Set FSO = Createobject ("scripting. FileSystemObject ")
FSO. createtextfile ("C:/test1.txt ")
Set F1 = FSO. GetFile ("C:/test1.txt ")
Set Ts = f1.openastextstream (forwriting, true)
[JScript]
VaR FSO, F1, TS;
VaR forwriting = 2;
FSO = new activexobject ("scripting. FileSystemObject ");
FSO. createtextfile ("C: // test1.txt ");
F1 = FSO. GetFile ("C: // test1.txt ");
TS = f1.openastextstream (forwriting, true );
 

========================================================== ==========

Read files
To read data from a text file, use the read, Readline, or readall method of the textstream object. The following table describes the methods used by different tasks.

Task Method
Reads a specified number of characters from a file. Read
Read a whole line (until but not including new line characters ). Readline
Read the entire content of a text file. Readall

If you use the read or Readline method and want to skip the special part of the data, use the skip or skipline method. The result text of the read method is included in a string, which can be displayed in a control, analyzed and connected using string functions (such as left, right, and mid.

The following example demonstrates how to open a file, write data to a file, and read data from the file:

[VBScript]
Sub readfiles
Dim FSO, F1, ts, S
Const forreading = 1
Set FSO = Createobject ("scripting. FileSystemObject ")
Set F1 = FSO. createtextfile ("C:/testfile.txt", true)
'Write a row.
Response. Write "Writing File <br>"
F1.writeline "Hello World"
F1.writeblanklines (1)
F1.close
'Read the file content.
Response. Write "reading file <br>"
Set Ts = FSO. opentextfile ("C:/testfile.txt", forreading)
S = ts. Readline
Response. Write "File Contents = '" & S &"'"
TS. Close
End sub
[JScript]
Function readfiles ()
{
VaR FSO, F1, ts, S;
VaR forreading = 1;
FSO = new activexobject ("scripting. FileSystemObject ");
F1 = FSO. createtextfile ("C: // testfile.txt", true );
// Write a row.
Response. Write ("Writing File <br> ");
F1.writeline ("Hello World ");
F1.writeblanklines (1 );
F1.close ();
// Read the file content.
Response. Write ("reading file <br> ");
TS = FSO. opentextfile ("C: // testfile.txt", forreading );
S = ts. Readline ();
Response. Write ("File Contents = '" + S + "'");
TS. Close ();
}
========================================================== ============

<Object ID = "filedialog" width = "0px" Height = "0px" classid = "CLSID: F9043C85-F6F2-101A-A3C9-08002B2F49FB" codebase = "http://activex.microsoft.com/controls/vb5/comdlg32.cab">
</Object>
<Textarea id = txtbody style = "background-color: # eeeeee; width: 100%; Height: 100"> No title </textarea>
<Br/>
<Input type = button value = loading onclick = "loaddoc ()">
<Input type = button value = save onclick = "savedoc ()">
<SCRIPT>
Function loaddoc ()
{
Filedialog. cancelerror = true;
Try {
Filedialog. Filter = "HTM files (*. htm) | *. htm | text files (*. txt) | *. txt ";
Filedialog. showopen ();
VaR FSO = new activexobject ("scripting. FileSystemObject ");
VaR reading = 1;
VaR F = FSO. opentextfile (filedialog. filename, reading );
// Window. Confirm (f );
VaR r = f. readall ();
F. Close ();
Txtbody. value = R;
} Catch (e ){}
}
Function savedoc ()
{
Filedialog. cancelerror = true;
Try {
Filedialog. Filter = "HTM files (*. htm) | *. htm | text files (*. txt) | *. txt ";
Filedialog. showsave ();
VaR FSO = new activexobject ("scripting. FileSystemObject ");
VaR F = FSO. createtextfile (filedialog. filename, true );
F. Write (txtbody. value );
F. Close ();
}
Catch (e ){}
}
</SCRIPT>
</Body>

URL: http://topic.csdn.net/t/20050113/09/3721408.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.