import java.io.*;
public class IODemo {
//创建目录
public static void CreateDir(String path){
File dir = new File(path);
if(!dir.exists()){
dir.mkdir();
}
}
//创建新文件(isDel为true删除原来存在的文件)
public static void CreateFile (String path, boolean isDel) throws IOException{
File file = new File(path);
if(file.exists()){
if(isDel){
file.delete();
file.createNewFile();
}
}else{
file.createNewFile();
}
}
//移动文件(isDel为true删除目标文件)
public static void MoveTo(String oldPath, String newPath, boolean isDel) throws IOException{
File fold = new File(oldPath);
File fnew = new File(newPath);
if(fnew.exists()){
if(isDel){
fnew.delete();
fold.renameTo(fnew);
}
}else{
fold.renameTo(fnew);
}
}
//写文件方法1
public static void WriteTxtMethod1(String path, String content, boolean append) throws IOException{
PrintWriter pw = new PrintWriter(new FileWriter(path, append));
pw.print(content);
pw.close();
}
//写文件方法2
public static void WriteTxtMethod2(String path, String content, boolean append) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter(path, append));
bw.write(content);
bw.close();
}
//写文件方法3
public static void WriteTxtMethod3(String path, String content, boolean append) throws IOException{
OutputStream os = new FileOutputStream(new File(path), append);
os.write(content.getBytes());
os.close();
}
//读文件方法1
public static String ReadTxtMethod1(String path) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(new File(path)));
StringBuffer bd = new StringBuffer();
while (true) {
String str = br.readLine();
if (str == null) break;
bd.append(str + "\n");
}
br.close();
return bd.toString();
}
//读文件方法2
public static String ReadTxtMethod2(String path) throws IOException{
File file = new File(path);
InputStream is = new FileInputStream(file);
byte[] b = new byte[Integer.parseInt(file.length()+ "")];
is.read(b);
is.close();
String str = new String(b);
return str;
}
//读文件方法3
public static String ReadTxtMethod3(String path) throws IOException{
File file = new File(path);
Reader r = new FileReader(file);
char[] c = new char[(int) file.length()];
r.read(c);
r.close();
String str = new String(c);
return str;
}
public static void main(String arg[]) {
try{
String dirPath = "E:/Lc/java/test"; //目录路径
String filePath = "E:/Lc/java/test.txt"; //原文件路径
String nfilePath = "E:/Lc/java/test/test.txt"; //新文件路径
//创建目录
CreateDir(dirPath);
//创建文件
CreateFile(filePath, true);
//移动文件
MoveTo(filePath, nfilePath, true);
//获取指定目录下的目录和文件
File[] files = new File(dirPath).listFiles();
for(int i=0;i<files.length;i++){
if(files[i].isDirectory()){
System.out.println("目录:" + files[i].getName() + " 路径:" + files[i].getAbsolutePath());
}else{
System.out.println("文件:" + files[i].getName() + " 路径:" + files[i].getAbsolutePath());
}
}
//写文件
WriteTxtMethod1(nfilePath, "测试写文件test1\r\n", true);
WriteTxtMethod2(nfilePath, "测试写文件test2\r\n", true);
WriteTxtMethod3(nfilePath, "测试写文件test3\r\n", true);
//读文件
String s1 = ReadTxtMethod1(nfilePath);
System.out.println(s1);
String s2 = ReadTxtMethod2(nfilePath);
System.out.println(s2);
String s3 = ReadTxtMethod3(nfilePath);
System.out.println(s3);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
/*
E:\Lc\java>javac IODemo.java
E:\Lc\java>java IODemo
文件:test.txt 路径:E:\Lc\java\test\test.txt
测试写文件test1测试写文件test2测试写文件test3
测试写文件test1
测试写文件test2
测试写文件test3
测试写文件test1
测试写文件test2
测试写文件test3
*/