(文件及文件夹的重命名可以实现文件及文件夹的移动操作!但必须是在同一分区下。)
import java.io.File;
import java.io.IOException;
public class mFileTest {
public static void main(String[] args) {
File file = new File("test.txt");
try {
//判断文件是否被创建
if(file.createNewFile()){
System.out.println("文件创建成功!");
}else{
if(file.exists()){
System.out.println("文件已经存在,无需再次创建!");
}else{
System.out.println("文件创建失败!");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
public class mFileTest {
public static void main(String[] args) {
File file = new File("test.txt");
if(file.delete()){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
}
import java.io.File;
public class mFileTest {
public static void main(String[] args) {
File file = new File("test.txt");
File newFile = new File("newTest.txt");
if(file.exists()){//判断文件是否存在
if(file.renameTo(newFile)){
System.out.println("重命名成功!");
}else{
System.out.println("重命名失败!");
}
}
}
}
mkdir()和mkdirs(),这两个方法用于创建文件夹
exists()方法用于判断文件夹是否存在
delete()方法用于删除文件夹
renameTo()方法用于重命名文件夹
import java.io.File;
public class mFileTest01 {
public static void main(String[] args) {
File folder = new File("test");
//创建文件夹
//folder.mkdir();
//创建文件夹
folder.mkdirs();
//删除文件夹
// if(folder.exists()){
// folder.delete();
// }
//重命名文件夹
File newFolder = new File("new-Test");
folder.renameTo(newFolder);
}
}
import java.io.File;
public class mFileTest02 {
public static void main(String[] args) {
File file = new File("new-Test/test.txt");
//判断文件是否存在
System.out.println("文件是否存在:" + file.exists());
//读取文件名称
System.out.println("文件名称为:" + file.getName());
//读取文件路径
System.out.println("文件路径为:" + file.getPath());
//读取文件绝对路径
System.out.println("文件的绝对路径:" + file.getAbsolutePath());
//获取文件父级路径
System.out.println("文件父级路径:" + file.getParent());
System.out.println("文件父级绝对路径:" + file.getParentFile().getAbsolutePath());
//读取文件的大小
System.out.println("文件的大小是:" + (float)file.length() / 1024 + "KB");
//文件是否被隐藏
System.out.println("文件是否被隐藏:" + file.isHidden());
//文件是否可读
System.out.println("文件是否可读:" + file.canRead());
//文件是否可写
System.out.println("文件是否可写:" + file.canWrite());
//文件是否为文件夹
System.out.println("文件是否是文件夹:" + file.isDirectory());
}
}
import java.io.File;
public class mFileTest03 {
public static void main(String[] args) {
File file = new File("new-Test/test.txt");
//设置为只读
file.setReadOnly();
//设置为可写
file.setWritable(true);
//设置为可读
file.setReadable(true);
}
}
import java.io.File;
public class mFileTest04 {
public static void main(String[] args) {
printFiles(new File("../myjavatest"), 1);
}
//递归法遍历文件夹
public static void printFiles(File file, int tab){
if(file.isDirectory()){
File files[] = file.listFiles();
for(int i = 0; i < files.length; i++){
for(int j = 0; j < tab; j++){
System.out.print("|--");
}
System.out.println(files[i].getName());
if(files[i].isDirectory()){
printFiles(files[i], tab+1);
}
}
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class mFileTest05 {
public static void main(String[] args) {
File file = new File("new-Test/test.txt");
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File newFile = new File("myText.txt");
try {
FileOutputStream fos = new FileOutputStream(newFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write("窗前明月光\n");
bw.write("疑是地上霜\n");
bw.close();
osw.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}