Java files and disk operations

xiaoxiao2021-03-06  68

How to get a list of files in a directory?

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

String [] filenames = mydir.list ();

2 How to implement an open file or store the file dialog box?

AWT: FileDialog class FileNameFilter class

Swing: Jfilechooser class FileFilter class

Among them, Swing-based solutions are more powerful, and the interface is also more beautiful.

3 Use FileReader / FileOutputStream to copy Chinese file error?

Use the following code snippet to copy Chinese files Will not be wrong:

INT C;

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

MyFileWriter.write (C);

Use the following code snippet to copy Chinese files to cause file content errors:

INT C;

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

MyFileOutputStream.write (C);

The reason for this problem is: FileReader.read () Returns an int, the value range is

0 to 65535, usually two bytes; FileWriter.write (int C) writes to the file

Inti-INT, usually two bytes, if a character's high byte is empty, then

How old bytes will be abandoned; FileOutputStream.write (int b) is accepted

INT as a parameter, actually only writes one byte to the file, if the passing parameter is one

Double-bytes of Chinese characters, its high bytes will be discarded, resulting in file content errors.

Recommendation: Io operation will always be used for IO.

Use the following code snippet to copy Chinese files Will not be wrong:

INT C;

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

MyFileOutputStream.write (C);

4 how to display and store special characters in the Latin language

Use unified code Unicode to display and store special characters in the Latin language. Specific application example

as follows:

MyjtextArea.Append ("/ us00e1");

MyjtextArea.Append ("/ us00e2");

MyjtextArea.Append ("/ us00e3");

MyjtextArea.Append ("/ us00e4");

MyjtextArea.Append ("/ us00e5");

MyFileOutputStream.write (MyjtextArea.getText (). GetBytes ("UTF-8"));

MyFileOutputStream.Close ();

Similarly, it is also necessary to convert the read content into a unified code when reading the file.

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 to perform data access

For general scientific computing applications, DataInputStream and DataOutputStream Class

Often the best choice. These two classes provide methods for accessing various data. The following example demonstrates

Constructing DataInputStream and DataOutputStream: MyDataInputStream = New DataInputStream

New fileInputstream (myinputfile);

MyDataOutputStream = New DataOutputStream

New fileOutputStream (myoutputfile);

Data access can be performed using ObjectInputStream and ObjectOutputStream.

It should be noted that this increases the overhead of the hard disk because the object serialization process has been added.

Some additional information. Communication with ObjectInputStream and ObjectOutputStream

At the time, although the data receiving process has been greatly simplified, the requirements for bandwidth are also great.

increased.

6 What is the basic principle of file operation?

a. Avoid multiple access disks, such as reading n bytes over one byte, more than 1 byte accessed

The efficiency is much higher.

b. Avoid access to the operating system multiple times.

c. Avoid multiple calling file access methods.

d. Avoid processing bytes and characters, in the Java language, bytes, the concept of characters is different.

It is easier to make mistakes on issues involving double-byte characters.

7 How to get available hard disk space?

There is currently not found that there is any clean and rumor Java method to solve this problem. Usual solution

The decision is to access the operating system to get this information. There is a class library called Jconfig.

For some ways to get disk and file information, but it is certain that this class library uses JNI.

method.

Download address: http://www.tolstoy.com/samizdat/jconfig.html

If you use a halo dead series operating system, then the following method may be able to get correct

the result of. The reason why I said maybe because I have made actual tests on multiple faint dead platforms.

Basically, you can get the correct result in the faint death of the English version, in the Chinese version of dizzy

It is basically unable to get the correct result.

String OsName = System.getProperty ("Os.Name");

String command = ""

IF (OsName.indexOf ("NT")> -1)

Command = "c: //winnt//system32//cmd.exe";

Else IF (OsName.indexof ("Windows")> -1)

Command = "c: //windows//command.com";

Process p = runtime.getRuntime (). EXEC (

Command "/ C Dir> C: //dir.txt");

p.WaitFor ();

Then you need to do it to analyze the Dir.txt file.

If you are using a UNIX / Linux operating system, you can use similar methods to get relevant

information. The proposed command is DF -K> Dir.txt.

8 I can format my hard drive or floppy disk with Java?

Regarding this issue, I want to come in the near future, still there will be no pure Java solutions. Such as

If you must format your C drive in your Java app, the following method

Maybe help. Of course, before you use this method, please make a closer to your girlfriend for you.

The love letter or a date and the date of the next netizen dating.

Establish a file called FormatDrive.bat, which must be placed in the current directory or

Under the system, the contents of the document are as follows:

Rundll32.exe shell32.dll, SHFORMATDRIVE formatting hard drive method can be written:

Public void formatdrive ()

{

Try

{

Process p = runtime.getRuntime (). EXEC ("formatdrive.bat");

p.WaitFor ();

} catch (Exception E)

{

System.out.println (e);

}

}

9 How do you know how many useable storage devices I have?

Under UNIX / Linux, you usually don't have to care about this problem, just remember the slash.

In fainting, the hard disk can have multiple logical partitions, which can be applied to the following method:

Public void listdisks ()

{

File [] roots = file.listroots ();

For (int i = 0; i

{

System.out.println (Roots [i]);

}

}

This FAQ reference is referred to or rewritten some of the content provided by www.esus.com website,

Bright. The site is an excellent Java program design website, which is worth visiting.

转载请注明原文地址:https://www.9cbs.com/read-111638.html

New Post(0)