Select a file in the file list and display the contents of a file in textarea

Source: Internet
Author: User
Tags readfile

PrivateFileDialog OpenFileDialog =NewFileDialog ( This, "Open File", filedialog.load);Else if(EventSource = =openFile)            {openfiledialog.show (); FileName= Openfiledialog.getdirectory () +Openfiledialog.getfile (); if(FileName! =NULL) ReadFile (fileName); } Publicvoid ReadFile (String fileName) {File file=NewFile (fileName); FileReader Readin=Newfilereader (file);intSize = (int) file.length ();intCharsread = 0;Char[] content =New Char[size]; while(Readin.ready ()) Charsread+ = Readin.read (content, charsread, size-charsread); Readin.close (); Textarea.settext (NewString (content, 0, Charsread));}

Basic steps: Create an object of type FileDialog with parameter this,string,filedialog.load

In the Actionperformed method, if the event source is OpenFile, the File List dialog box is displayed, and the full file pathname is obtained using the GetPath () and GetFile () methods

Override the ReadFile function to define the file and FileReader objects, the Int type object representing the length of the files, and the array of char types

Finally close the FileReader object

Select a file in the file list and display the contents of a file in textarea

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.