In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Java operation file and disk method is what", in daily operation, I believe many people in Java operation file and disk method is what problem there is doubt, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation method, hope to answer "Java operation file and disk method is what" doubt helpful! Next, please follow the small series to learn together!
1 How do I 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 Save File dialog box?
AWT: FileDialog + FilenameFilter
swing: JFileSelector + FileFilter
Among them, Swing based solutions are more powerful and have a more beautiful interface.
3 Error copying Chinese files using FileReader/FileOutputStream?
There is no error in copying Chinese files using the following code snippet:
int c;
while ((c = MyFileReader.read()) != -1)
MyFileWriter.write(c);
Copying a Chinese file using the following code snippet will result in incorrect file content:
int c;
while ((c = MyFileReader.read()) != -1)
MyFileOutputStream.write(c);
FileReader.read() returns an int whose range is
0 to 65535, usually two bytes;FileWriter.write(int c) writes to a file
Enter an int, usually two bytes, if the high byte of a character is empty, then
FileOutputStream.write(int b) although accept a
int as a parameter, actually only writes one byte to the file, if the parameter passed is a
For double-byte Chinese characters, the high-order bytes 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 using the following code snippet:
int c;
while ((c = MyFileInputStream.read()) != -1)
MyFileOutputStream.write(c);
4 How to display and store special characters in Latin
Unicode is used to display and store special characters in Latin languages. 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();
Similarly, when reading a file, you need to convert what you read into Unicode.
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, DataInputStream and DataOutputStream classes are common.
Often the best choice. These two classes provide methods for accessing various data. The following example demonstrates
How to construct DataInput Stream and DataOutput Stream:
MyDataInputStream = new DataInputStream(
new FileInputStream(MyInputFile));
MyDataOutputStream = new DataOutputStream(
new FileOutputStream(MyOutputFile));
ObjectInput Stream and ObjectOutput Stream can also be used for data access.
Note that doing so increases the overhead of the hard disk because doing object serialization adds a
some additional information. Communicating with ObjectInput Stream and ObjectOutput Stream
Although the data transmission and reception process has been greatly simplified, the bandwidth requirements are also greatly reduced.
improved.
6 What are the basic principles of file operation?
a. Avoid multiple disk accesses, e.g. reading n bytes at a time is better than reading 1 byte at a time
Much more efficient.
b. Avoid multiple visits to the operating system.
c. Avoid calling file access methods multiple times.
d. Avoid confusing bytes and characters. In Java, bytes and characters are different concepts.
It is more error-prone when it comes to double-byte characters.
7 How to get free hard disk space?
No clean, pure Java approach has been found to solve this problem. ordinary solution
The solution is to access the operating system directly to obtain this information. There is a class library called JConfig.
There are some ways to get disk and file information, but it is certain that this library uses JNI
Methods.
If you're using the Faint Dead operating system, then the following method may be able to get it right
the result. I say maybe because I've actually tested it on multiple fainting platforms,
In the English version of faint death basically can get the correct results, in the Chinese version of faint death
Basically, they don't get the right results.
String osname = System.getProperty("os.name");
String command = "";
if (osname.indexOf("NT") > -1)
command = "c:winntSystem32cmd.exe";
else if (osname.indexOf("Windows") > -1)
command = "c:windowscommand.com";
Process p = Runtime.getRuntime().exec(
command + " /c dir > c:dir.txt");
p.waitFor();
Then all you need to do is parse the resulting dir.txt file.
If you are using unix/Linux, you can use a similar method to get the correlation.
Information. The recommended command is df -k > dir.txt.
8 Can I format my hard disk or floppy disk in Java?
There will be no pure Java solution for this problem in the near future. as
If you must format your C drive in your Java application, here's how
It might help. Of course, before you use this method, please carefully back up your girlfriend for you
Write down the date of your next online date.
Create a file called FormatDrive.bat, which must be placed in the current directory or system
Under the system path, the contents of the file are as follows:
rundll32.exe shell32.dll, SHFormatDrive
To format a hard disk, you can write:
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?
Under UNIX/Linux you usually don't have to worry about this, just remember the slash.
The hard disk can have multiple logical partitions under faint death, which can be found by applying the following method:
public void ListDisks()
{
File[] roots = File.listRoots();
for (int i=0; i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.