//File對象包含了文件中的所有字節數據,以及文件的其他詳細信息
File file;
//使用絕對路徑創建文件
file = new File("d:/myFile/a/test.txt");
//通過父文件對象創建
file = new File(new File("d:/myFile"), "a/test.txt");
file = new File(new File("d:/myFile/a"),"test.txt");
//通過父文件路徑創建
file = new File("d:/myFile", "a/test.txt");
file.getName();
file.getParent();
file.length();
file.getFreeSpace();
file.getTotalSpace();
file.getUsableSpace();
file.canExecute()
file.canRead();
file.canWrite();
file.lastModified();
//返回long型
long time = file.lastModified();
Date date = new Date(time);
file.isHidden();
File[] files = file.listFile();
File[] disks = File.listRoots();
file.delete()
/**
* 查看所有文件的方法
*/
public void showFiles(String path){
//根據路徑獲得文件
File file = new File(path);
//判斷文件是否存在
if(file.exists()){
System.out.println(file.getAbsolutePath());
//判斷是否是文件夾
if(file.isDirectory()){
//如果是文件夾,則獲取文件夾中的子文件
File[] files = file.listFiles();
if(files != null){
//遍歷子文件
for(File childFile : files){
//遞歸查看
showFiles(childFile.getAbsolutePath());
}
}
}
- }
- }
/**
* 根據文件類型查找顯示文件
* @param path
* @param type
*/
public void showFiles(String path, String type){
//根據路徑獲得文件
File file = new File(path);
//判斷文件是否存在
if(file.exists()){
//判斷文件是否是文件
if(file.isFile()){
//如果是文件就要判斷文件的後綴名是否和查找的文件類型匹配
if(validateFile(file.getName())){
//獲取文件的後綴名
String fileType = file.getName().substring(file.getName().lastIndexOf(".")+1);
//判斷後綴名是否和查找文件匹配
if(fileType.equalsIgnoreCase(type)){
//打印顯示文件
System.out.println(file.getAbsolutePath());
}
}
}
//如果是文件夾遞歸查找
else{
//如果是文件夾,則獲取文件夾中的子文件
File[] files = file.listFiles();
if(files != null){
//遍歷子文件
for(File childFile : files){
//遞歸查看
showFiles(childFile.getAbsolutePath(),type);
}
}
}
}
}
/**
* 驗證文件是否是一個合法文件
* @return
*/
public boolean validateFile(String fileName){
if(fileName.indexOf(".") != -1){
return true;
}
return false;
}
/**
* 創建單個文件的方法
* @param fileName 新建的文件名
* @param path 創建的位置
*/
public void createFile(String fileName, String path){
//根據父文件路徑創建File對象
File parentFile = new File(path);
//判斷父文件是否存在
if(parentFile.exists()){
//判斷父文件是否是文件夾
if(parentFile.isDirectory()){
//構建File對象
File file = new File(path, fileName);
//判斷要新建的文件是否存在
if(!file.exists()){
//創建
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
JOptionPane.showMessageDialog(null, "該文件已經存在!");
}
}
}
else{
JOptionPane.showMessageDialog(null, "文件夾不存在!");
}
}
/**
* 創建文件夾的方法
*/
public void createDirectory(String fileName, String path){
//new File("").mkdir();
}
/**
* 對文件進行重命名的方法
* @param source 要修改的源文件路徑
* @param fileName 文件的新命名(不包含後綴名)
*/
public void reNameFile(String source, String fileName){
File sourceFile = new File(source);
//判斷源文件是否存在
if(sourceFile.exists()){
//判斷源文件是否有後綴名
if(validateFile(sourceFile.getName())){
//獲取源文件的後綴名
String type = sourceFile.getName().substring(sourceFile.getName().lastIndexOf("."));
//根據源文件的路徑以及後綴名創建新命名的文件對象
File file = new File(sourceFile.getParent(),fileName+type);
////判斷新的文件名是否存在
if(!file.exists()){
//重命名
sourceFile.renameTo(file);
}
}
}
}
/**
* 刪除文件的方法
*/
public void deleteFile(String path){
File file = new File(path);
// //刪除文件,可以用於文件和文件夾
// //如果是文件夾,並且該文件夾中有子文件,則不能刪除
// //如有以上情況則必須清空文件夾中的子文件
// file.delete();
//判斷文件是否存在
if(file.exists()){
//判斷文件是否是文件夾
if(file.isDirectory()){
//如果是文件夾獲取子文件
File[] files = file.listFiles();
if(files != null){
//遞歸刪除
for(File childFile : files){
deleteFile(childFile.getAbsolutePath());
}
}
}
//無論是否是文件夾都應刪除當前文件
file.delete();
}
}
IO流
int data;
while((data = input.read()) != -1){
output.write(data);
}
//數據流不能對文件夾進行讀寫
FileInputStream fis = new FileInputStream(new File("d:/a.txt"));
//每次讀取一個字節,因此通過循環反覆讀取字節
int data;
while((data = fis.read()) != -1){
system.out.println(data);
}
fis.close();
//創建輸出流的時候如果文件不存在,則會自動創建文件,如果文件存在則將默認覆蓋文件
//第二個參數表示是否追加數據,默認爲false
FileOutputStream fos = new FileOutputStream(new File("d:/b.txt"),true);
//寫入數據
fos.write(66);
fos.write(67);
//清空緩衝流
fos.flush();
fos.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("d:/flash.exe")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("d:/test.exe")));
int len;
byte[] buffer = new byte[1024];
while((len = bis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
bis.close();
bos.flush();
bos.close();
FileInputStream fis = new FileInputStream(new File("d:/flash.exe"));
FileOutputStream fos = new FileOutputStream(new File("d:/test.exe"));
//讀取長度
int len;
//緩衝數組
byte[] buffer = new byte[1024];
//每次讀取都將字節寫入緩衝區
while((len = fis.read(buffer)) != -1){
//將緩衝區中的數據寫入文件
fos.write(buffer,0,len);
}
fis.close();
fos.flush();
fos.close();
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(new File("d:/a.txt")));
char[] buffer = new char[1024];
StringBuffer str = new StringBuffer();
int len;
while((len = reader.read(buffer)) != -1){
str.append(buffer);
}
reader.close();
System.out.println(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("d:/a.txt"))));
String s;
//一次讀取一行
while((s = reader.readLine()) != null){
System.out.println(s);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("d:/b.txt"))));
//使用輸出流寫入數據
writer.write("這是我新建的文本\r\n");
//換行
//writer.newLine();
writer.write("又一段新的內容");
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
import java.io.Serializable;
public class User implements Serializable{
private int userId;
private String name;
public User(int userId, String name) {
super();
this.userId = userId;
this.name = name;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
User user1 = new User(1001, "tom");
User user2 = new User(1002, "jack");
ArrayList<User> list = new ArrayList<User>();
list.add(user1);
list.add(user2);
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("d:/users.data")));
//將對象通過輸出流進行序列化保存
//序列化對象必須實現序列化接口
output.writeObject(list);
output.flush();
output.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("d:/users.data")));
ArrayList<User> list = (ArrayList<User>) input.readObject();
input.close();
for(User user : list){
System.out.println(user.getUserId()+"\t"+user.getName());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}