File
An abstract representation of file and directory pathnames, representing files or folders.
Common method
boolean mkdir() creates the specified directory, or returns if it does not exist.
boolean mkdirs() creates the specified directory, including parent directories that do not exist.
boolean delete() deletes a file or directory
boolean exists() whether the file or directory exists
boolean isDirectory() whether is a directory
boolean isFile() is a file
String getName() returns the file or directory name, only the name without the path
String getAbsolutePath() returns the absolute path string
String[] list() returns the file and directory path in the directory
File[] listFiles () returns the files and directory objects in the directory
File[] listFiles(FilenameFilter filter) sets the filter
boolean renameTo(File dest) renames the file
read file
StringBuffer sb= new StringBuffer("");
FileReader reader = new FileReader("c://test.txt");
BufferedReader br = new BufferedReader(reader);
String str = null;
while((str = br.readLine()) != null) {
sb.append(str+"\r\n");
System.out.println(str);
}
specify encoding
File file=new File(this.filePath);
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename),"GBk")));
write file
FileWriter
FileWriter writer = new FileWriter("c://test2.txt");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(sb.toString());
bw.close();
writer.close();
FileOutputStream
file = new File(“c:/newfile.txt”);
fop = new FileOutputStream(file);
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();