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);
This problem is caused by the fact that Filereader.read () returns an int whose value range is
0 to 65535, usually two bytes; filewriter.write (int c) writes to a file
into an int, which is usually two bytes, if the high byte of a character is empty, that
Its high byte will be discarded; fileoutputstream.write (int b) while accepting a
int as a parameter, actually writes only one byte to the file, if the argument passed over is a
Double-byte Chinese characters, their high byte will be discarded, resulting in 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 Pass
is often the best choice. These two classes provide a way to access various data. The following example demonstrates the
Methods of constructing DataInputStream and DataOutputStream:
Mydatainputstream = new DataInputStream (
New FileInputStream (Myinputfile));
Mydataoutputstream = new DataOutputStream (
New FileOutputStream (Myoutputfile));
With ObjectInputStream and ObjectOutputStream, data access is also available, requiring
Note that this increases the overhead of the hard drive because the object serialization process adds a
Some extra information. In the use of ObjectInputStream and ObjectOutputStream communication
, although the data delivery process has been greatly simplified, but the bandwidth requirements are greatly
Improved.
6 What is the basic principle of file operation?
A. Avoid multiple access to the disk, such as reading out n bytes at a time more than 1 bytes of access per read
Efficiency is much higher.
B. Avoid multiple access to the operating system.
C. Avoid multiple calls to file access methods.
D. Avoid confusing bytes and characters, the concept of byte and character is different in the Java language
, it is more error prone to problems involving double-byte characters.
7 How do I get the available hard disk space?
It has not been found that any neat, pure Java methods can solve this problem. The usual solution
The solution is to access the operating system directly to obtain this information. There is a class called Jconfig Kuti
There are 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 are using a faint dead series operating system, the following methods may be able to get the correct
The results. I said maybe because I did a real test on multiple faint dead platforms,
In the English version of the fainting death basically can get the correct results, in the Chinese version of the fainting death
are basically not able to get the right results.
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 what you need to do is analyze the resulting dir.txt file.
If you are using a Unix/linux operating system, you can use a similar method to get the relevant
Information. The recommended command is Df-k > dir.txt.
8 Can I format my hard drive or floppy disk in Java?
In the near future, there is no pure Java solution for this problem. Such as
If you want to format your C-disk in your Java application, the following method
Maybe it will help. Of course, before you use this method, please carefully back up your good girlfriend to you
Love letter or write down a date with the next net friend.
Create a file called Formatdrive.bat, which must be placed in the current directory or in a department
Under the same path, the contents of the document are as follows:
rundll32.exe shell32.dll, Shformatdrive
The way you format your hard disk can be written like this:
public void Formatdrive ()
{
Try
{
Process p = runtime.getruntime (). EXEC ("Formatdrive.bat");
P.waitfor ();
catch (Exception e)
{
System.out.println (e);
}
}
9 How do I know how many storage devices I have available?
You don't usually need to care about the problem under the Unix/linux, just remember the slash.
In the faint of death the hard drive can have multiple logical partitions, which can be found by applying the following method:
public void Listdisks ()
{
File[] roots = File.listroots ();
for (int i=0; i
{
System.out.println (Roots[i]);
}
}