Java編程中最基本的文件和目次操作辦法詳解。本站提示廣大學習愛好者:(Java編程中最基本的文件和目次操作辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java編程中最基本的文件和目次操作辦法詳解正文
文件操作
平凡常常應用JAVA對文件停止讀寫等操作,這裡匯總一下經常使用的文件操作。
1、創立文件
public static boolean createFile(String filePath){
boolean result = false;
File file = new File(filePath);
if(!file.exists()){
try {
result = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
2、創立文件夾
public static boolean createDirectory(String directory){
boolean result = false;
File file = new File(directory);
if(!file.exists()){
result = file.mkdirs();
}
return result;
}
3、刪除文件
public static boolean deleteFile(String filePath){
boolean result = false;
File file = new File(filePath);
if(file.exists() && file.isFile()){
result = file.delete();
}
return result;
}
4、刪除文件夾
遞歸刪除文件夾上面的子文件和文件夾
public static void deleteDirectory(String filePath){
File file = new File(filePath);
if(!file.exists()){
return;
}
if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
File[] files = file.listFiles();
for (File myfile : files) {
deleteDirectory(filePath + "/" + myfile.getName());
}
file.delete();
}
}
5、讀文件
(1)以字節為單元讀取文件,經常使用於讀二進制文件,如圖片、聲響、影象等文件
public static String readFileByBytes(String filePath){
File file = new File(filePath);
if(!file.exists() || !file.isFile()){
return null;
}
StringBuffer content = new StringBuffer();
try {
byte[] temp = new byte[1024];
FileInputStream fileInputStream = new FileInputStream(file);
while(fileInputStream.read(temp) != -1){
content.append(new String(temp));
temp = new byte[1024];
}
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
(2)以字符為單元讀取文件,經常使用於讀文本,數字等類型的文件,支撐讀取中文
public static String readFileByChars(String filePath){
File file = new File(filePath);
if(!file.exists() || !file.isFile()){
return null;
}
StringBuffer content = new StringBuffer();
try {
char[] temp = new char[1024];
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
while(inputStreamReader.read(temp) != -1){
content.append(new String(temp));
temp = new char[1024];
}
fileInputStream.close();
inputStreamReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
(3)以行動單元讀取文件,經常使用於讀面向行的格局化文件
public static List<String> readFileByLines(String filePath){
File file = new File(filePath);
if(!file.exists() || !file.isFile()){
return null;
}
List<String> content = new ArrayList<String>();
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
BufferedReader reader = new BufferedReader(inputStreamReader);
String lineContent = "";
while ((lineContent = reader.readLine()) != null) {
content.add(lineContent);
System.out.println(lineContent);
}
fileInputStream.close();
inputStreamReader.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
6、寫文件
字符串寫入文件的幾個類中,FileWriter效力最高,BufferedOutputStream次之,FileOutputStream最差。
(1)經由過程FileOutputStream寫入文件
public static void writeFileByFileOutputStream(String filePath, String content) throws IOException{
File file = new File(filePath);
synchronized (file) {
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(content.getBytes("GBK"));
fos.close();
}
}
(2)經由過程BufferedOutputStream寫入文件
public static void writeFileByBufferedOutputStream(String filePath, String content) throws IOException{
File file = new File(filePath);
synchronized (file) {
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath));
fos.write(content.getBytes("GBK"));
fos.flush();
fos.close();
}
}
(3)經由過程FileWriter將字符串寫入文件
public static void writeFileByFileWriter(String filePath, String content) throws IOException{
File file = new File(filePath);
synchronized (file) {
FileWriter fw = new FileWriter(filePath);
fw.write(content);
fw.close();
}
}
目次操作
目次是一個文件可以包括其他文件和目次的列表。你想要在目次中列出可用文件列表,可以經由過程應用 File 對象創立目次,取得完全具體的能在 File 對象中挪用的和有關目次的辦法列表。
創立目次
這裡有兩個有效的文件辦法,可以或許創立目次:
mkdir( ) 辦法創立了一個目次,勝利前往 true ,創立掉敗前往 false。掉敗情形是指文件對象的途徑曾經存在了,或許沒法創立目次,由於全部途徑不存在。
mkdirs( ) 辦法創立一個目次和它的下級目次。
以下示例創立 “/ tmp / user / java / bin” 目次:
import java.io.File;
public class CreateDir {
public static void main(String args[]) {
String dirname = "/tmp/user/java/bin";
File d = new File(dirname);
// Create directory now.
d.mkdirs();
}
}
編譯並履行以上代碼創立 “/ tmp /user/ java / bin”。
提醒:Java 主動按 UNIX 和 Windows 商定來處置途徑分隔符。假如在 Windows 版本的 Java 中應用正斜槓(/),依然可以獲得准確的途徑。
目次列表
以下,你可以或許用 File 對象供給的 list() 辦法來列出目次中一切可用的文件和目次
import java.io.File;
public class ReadDir {
public static void main(String[] args) {
File file = null;
String[] paths;
try{
// create new file object
file = new File("/tmp");
// array of files and directory
paths = file.list();
// for each name in the path array
for(String path:paths)
{
// prints filename and directory name
System.out.println(path);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
基於你/ tmp目次下可用的目次和文件,將發生以下成果:
test1.txt test2.txt ReadDir.java ReadDir.class