Java file and disk operation technology detailed

Source: Internet
Author: User
Tags object serialization
1 How do I get a list of files in a directory?

File Mydir = new file ("c:/windows/.");

string[] FileNames = Mydir.list ();

2 How do I implement an open file or a storage file dialog box?

Awt:filedialog class + FilenameFilter class

Swing:jfilechooser class + FileFilter class

Among them, the solution based on swing is more powerful and the interface is more beautiful.

3 error in using Filereader/fileoutputstream to copy Chinese files?

There is no error in copying Chinese files with the following code fragment:

int C;

while ((c = myfilereader.read ())!=-1)

Myfilewriter.write (c);

Copying a Chinese file with the following code fragment will result in a file content error:

int C;

while ((c = myfilereader.read ())!=-1)

Myfileoutputstream.write (c);

The cause of this problem is that Filereader.read () returns an int with a value range of 0 to 65535, usually two bytes, and filewriter.write (int c) writes an int to the file, usually two bytes, If a character's high byte is empty, its high-order byte is discarded; fileoutputstream.write (int b), although accepting an int as an argument, actually writes only one byte to the file, and if the passed argument is a double-byte character, its high byte is discarded , causing file content errors.

Recommendation: Always use only inputstream/outputstream for IO operations.

There is no error in copying Chinese files with the following code fragment:

int C;

while ((c = myfileinputstream.read ())!=-1)

Myfileoutputstream.write (c);

4 How to display and store special characters in a Latin language

Unicode allows you to display and store special characters in a Latin language. Specific application examples

As follows:

Myjtextarea.append ("\u00e1");

Myjtextarea.append ("\u00e2");

Myjtextarea.append ("\u00e3");

Myjtextarea.append ("\u00e4");

Myjtextarea.append ("\u00e5");

Myfileoutputstream.write (Myjtextarea.gettext (). GetBytes ("UTF-8"));

Myfileoutputstream.close ();

Also, when reading a file, you need to convert the read content to a unified code.

Byte[] B = new byte[myfile.length ()];

FileInputStream in = new FileInputStream (MyFile);

In.read (b);

Myjtextarea.append (New String (b, "UTF-8"));

5 How to use files for data access

For general scientific computing applications, the DataInputStream and DataOutputStream classes are usually the best choice. These two classes provide a way to access various data. The following example demonstrates how to construct DataInputStream and DataOutputStream:

Mydatainputstream = new DataInputStream (new FileInputStream (Myinputfile));

Mydataoutputstream = new DataOutputStream (new FileOutputStream (Myoutputfile));

With ObjectInputStream and objectoutputstream the same data access, it should be noted that this increases the cost of the hard disk, because the object serialization process adds some additional information. In the use of ObjectInputStream and ObjectOutputStream communication, although the data processing has been greatly simplified, but the bandwidth requirements are greatly improved.

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.