Java学习Day16 IO流基础
File
package com.yuhuw.basic.IO;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
public class FileBasic {
public static void main(String[] args) {
}
@Test
public void create01() throws IOException {
String filePath = "g:\\news1.txt";
File file = new File(filePath);
file.createNewFile();
System.out.println("OK");
}
@Test
public void create02() {
File parentFile = new File("g:\\");
String fileName = "news2.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("OK");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void create03() throws IOException {
String parentPath = "g:/";
String fileName = "news3.txt";
File file = new File(parentPath, fileName);
file.createNewFile();
}
}
FileMethod
package com.yuhuw.basic.IO;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
public class FileBasic {
public static void main(String[] args) {
}
@Test
public void create01() throws IOException {
String filePath = "g:\\news1.txt";
File file = new File(filePath);
file.createNewFile();
System.out.println("OK");
}
@Test
public void create02() {
File parentFile = new File("g:\\");
String fileName = "news2.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("OK");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void create03() throws IOException {
String parentPath = "g:/";
String fileName = "news3.txt";
File file = new File(parentPath, fileName);
file.createNewFile();
}
}
Directory
public class DirectoryOperate {
public static void main(String[] args) {
}
@Test
public void m1() {
String filePath = "g:\\news1.txt";
File file = new File(filePath);
if (file.exists()) {
if (file.delete()) {
System.out.println("Ok");
} else {
System.out.println("失败了");
}
} else {
System.out.println("文件不存在");
}
}
@Test
public void m2() {
String filePath = "G:\\新建文件夹";
File file = new File(filePath);
if (file.exists()) {
if (file.delete()) {
System.out.println("Ok");
} else {
System.out.println("失败了");
}
} else {
System.out.println("目录不存在");
}
}
@Test
public void m3() {
String directoryPath = "G:\\新建文件夹";
File file = new File(directoryPath);
if (file.exists()) {
System.out.println("目录已经存在");
} else {
if (file.mkdirs()) {
System.out.println("创建成功");
} else {
System.out.println("创建失败");
}
}
}
}
IO
public class IOBasic {
public static void main(String[] args) {
}
}