Some methods of manipulating file properties in Java

Source: Internet
Author: User
Tags readline string to file

1. When the java.io, if the file operation, to determine whether to hide with File.ishiden ()

Determines whether read-only, available file.canwrite ().

2. In addition to providing file.setreadonly () in Java, there is no other way to set whether it is readable or hidden.

So we have to go to the DOS environment to set up, in Java with Runtime.getruntime (). EXEC ("attrib" + "" + file.getabsolutepath () + "" + "+r") This method can be implemented. Because there may be spaces in the path File.getabsolutepath (), you must enclose it in quotation marks as a parameter. So that we can achieve the

(1) Set read-only Runtime.getruntime (). EXEC ("attrib" + "" "+ file.getabsolutepath () +" "" + "+r");

(2) Set writable Runtime.getruntime (). EXEC ("attrib" + "" "+ file.getabsolutepath () +" "" + "-r");

(3) Set the hidden Runtime.getruntime (). EXEC ("attrib" + "" "+ file.getabsolutepath () +" "+" +h ");

(4) Set the Runtime.getruntime (). EXEC ("attrib" + "" "+ file.getabsolutepath () +" "+"-H ");

3. Some operating source code for reading and writing files

Package Filecontrol
Import java.io.BufferedReader;
import java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
public class Filecontrol {
private static String fileName,
private static file file;
public static void M Ain (string[] args) {
try {
FileName = "Exercise.java";
File = new file ("E:" + "\", fileName);
if (file.exists ()) {
//File.delete ();
} else {
File.createnewfile ();
}
if (file.isdirectory ()) {
System.out.println ("This file is directory");
}
if (File.isfile ()) {
System.out.println ("This file is a file");
}
FileWriter fw = new FileWriter ("E:" + "\" + fileName);
//buffer is written to the file and can be wrapped by an escape character or bw.newline ();
BufferedWriter bw = new BufferedWriter (fw);
//write string to file
Bw.write ( "Hello, everyone!" ");
Bw.newline ();
Bw.write ("This is the JSP process tip");
Bw.newline ();
Bw.wriTE ("Advice!") ");
Bw.newline ();
Bw.write ("Email:stride@sina.comqianyf");
Bw.flush ();
Bw.close ();
//Append content in file location
Randomaccessfile RF = new Randomaccessfile ("E:" + "\" + fileName, "RW");
Rf.seek (Rf.length ());
Rf.writebytes ("Zui Jia de");
Rf.close ();
//Read file by buffering
FileReader fr = new FileReader ("E:" + "\" + fileName);
BufferedReader br = new BufferedReader (FR);
String linedata = Br.readline ();
while (null!= linedata) {
System.out.println (linedata);
Linedata = Br.readline ();
}
} catch (IOException ee) {
System.out.println ("System Exception");
}
}
}

Related Article

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.