Table of contents
About creation and destruction operations
Create folders (multi-level directories)
The first type: byte stream reading
The second type: character stream reading (Reader)
The first type: byte stream writing
The second method: character stream input
1.File class
File translated as "file"
Then the operations of the File class are actually all operations on files.
Operations on names and paths
public static void main(String[] args) {
//虚拟一个名字叫做test.txt的文件,但这一步只是虚拟的,还没有创建
File file = new File("./test.txt");
System.out.println(file.getParent());//获取这个文件的父目录的路径
System.out.println(file.getName());//获取这个文件的名字
System.out.println(file.getPath());//获取这个文件的相对路径
System.out.println(file.getAbsolutePath());//获取这个文件的绝对路径
}
About creation and destruction operations
public static void main(String[] args) throws IOException {
//拟创建一个叫做Test.txt的文件,但还没创建
File file = new File("./Test.txt");
//真的在计算机的当前目录创建了这个文件
file.createNewFile();
System.out.println(file.isFile());//判断是否是普通文件
System.out.println(file.isDirectory());//判断是否是目录文件
System.out.println(file.exists());//判断这个文件是否存在
System.out.println(file.delete());//删除这个文件
System.out.println(file.exists());
}
Create folders (multi-level directories)
public static void main(String[] args) {
File file = new File("./hello1");
File file2 = new File("./hello2/6666");
file.mkdir();//只能创建一级目录
file2.mkdirs();//能创建多级目录
}
InputStream
Just read data from the file
There are generally two ways to read
The first type: byte stream reading
(The basic unit of reading and writing is bytes)
Basic method: read(): Read one byte of data and return -1 to indicate that the reading is completed.
use
We first create a file named hello.txt on the D drive, and then enter hello
public static void main(String[] args) throws FileNotFoundException {
InputStream inputStream = new FileInputStream("D:/hello.txt");//这里输入读取的文件地址,如果输入错误会报错无法读取
while(true){
try {
int ret = inputStream.read();//如果全部读取完毕返回-1
if(ret == -1){
break;
}
System.out.println(ret);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
The output is the corresponding ASCII code~
The second type: character stream reading (Reader)
The operating principle is the same as byte stream reading, the only difference is that characters are displayed on the screen instead of numbers corresponding to the ask table.
public static void main(String[] args) throws FileNotFoundException {
Reader reader = new FileReader("D:/hello.txt");
while(true){
try {
int ret = reader.read();
if(ret == -1){
break;
}
System.out.println((char)ret);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
OutputStream
Just write data to the file
There are generally two ways to write
The first type: byte stream writing
Basic method: write()
public static void main(String[] args) throws FileNotFoundException {
OutputStream outputStream = new FileOutputStream("d:/hello.txt");
try {
//注意,这里的98,99,100是ask码表对应的数字的字符,不是数字98,99,100
outputStream.write(98);
outputStream.write(99);
outputStream.write(100);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
The second method: character stream input
In this method, be sure to use the flush method at the end.
Get the byte stream stored in memory
Otherwise, there will be nothing but the program will still not report an error.
public static void main(String[] args) throws IOException {
Writer writer = new FileWriter("d:/hello2.txt");
writer.write("hello");
//一定要加上flush!!!!!
//一定要加上flush!!!!!
//一定要加上flush!!!!!
writer.flush();
}