File I/O and GUI with JavaFX
目标:I/O学习Java文件
I/O机制写入和读取一个文件
本节对于CW3非常重要
目标:I/O学习Java文件
I/O机制写入和读取一个文件
本节对于CW3非常重要
51.creating, writing to and reading from a file
(不知道什么是CW3见>>前言<<)
Data
Data stored in a variable will be lost when a program terminates.
变量只能用来临时存储Data,所以要专门位置的专门文件来储存Data。
Absolute file name
绝对文件名取决于操作系统-
For example in Windows:
C:\Users\erick.purwanto\Documents\NetBeansProjects\Lecture11Demo\data\text1.txt
Relative file name
相对文件名
relative to the current working directory
For example in Windows:
data\text1.txt
由于操作系统可能有所不同,在创建文件对象时不要使用绝对文件名 使用Java中的相对文件名 File file= new File(“data/text1.txt”);创建文件对象实际上并不会在计算机上创建文件。
Data stored in a variable will be lost when a program terminates.
变量只能用来临时存储Data,所以要专门位置的专门文件来储存Data。
Absolute file name
绝对文件名取决于操作系统-
For example in Windows:
C:\Users\erick.purwanto\Documents\NetBeansProjects\Lecture11Demo\data\text1.txt
Relative file name
相对文件名
relative to the current working directory
For example in Windows:
data\text1.txt
由于操作系统可能有所不同,在创建文件对象时不要使用绝对文件名 使用Java中的相对文件名 File file= new File(“data/text1.txt”);创建文件对象实际上并不会在计算机上创建文件。
52.File Methods
1.file.exists():
>>1-3的解析<<
This method returns a boolean indicating whether the file or directory denoted by this abstract pathname exists.
It returns true if the file or directory exists;
otherwise, it returns false.
This method returns a boolean indicating whether the file or directory denoted by this abstract pathname exists.
It returns true if the file or directory exists;
otherwise, it returns false.
2.file.isFile():
This method tests whether the file denoted by this abstract pathname is a normal file.
It returns true if it is, false otherwise.
Sure, let's go through each of these File methods and their usage:
It returns true if it is, false otherwise.
Sure, let's go through each of these File methods and their usage:
3.file.isDirectory():
This method tests whether the file denoted by this abstract pathname is a directory.
It returns true if it is, false otherwise.
boolean isDirectory = file.isDirectory();
It returns true if it is, false otherwise.
boolean isDirectory = file.isDirectory();
4.file.isHidden():
This method tests whether the file or directory denoted by this abstract pathname is hidden.
It returns true if the file or directory is hidden,
false otherwise. boolean isHidden = file.isHidden();
It returns true if the file or directory is hidden,
false otherwise. boolean isHidden = file.isHidden();
5.file.length():
This method returns the length of the file denoted by this abstract pathname.
For directories, it returns 0.
long length = file.length();
For directories, it returns 0.
long length = file.length();
6.file.canRead():
This method tests whether the application can read the file denoted by this abstract pathname.
It returns true if the file can be read,
false otherwise. boolean canRead = file.canRead();
It returns true if the file can be read,
false otherwise. boolean canRead = file.canRead();
7.file.canWrite():
This method tests whether the application can modify the file denoted by this abstract pathname.
It returns true if the file can be modified,
false otherwise. boolean canWrite = file.canWrite();
It returns true if the file can be modified,
false otherwise. boolean canWrite = file.canWrite();
8.file.getAbsolutePath():
This method returns the absolute pathname string of this abstract pathname.
String absolutePath = file.getAbsolutePath();
现在,对于要重命名和删除的操作:
String absolutePath = file.getAbsolutePath();
现在,对于要重命名和删除的操作:
9.file.delete():
This method deletes the file or directory denoted by this abstract pathname. It returns true if the file or directory is successfully deleted, false otherwise.
boolean deleted = file.delete();
boolean deleted = file.delete();
10.file.renameTo(File dest):
This method renames the file denoted by this abstract pathname. It returns true if the file is successfully renamed,
false otherwise. The dest parameter is the new abstract pathname for the named file. File newFile = new File("data/text2.txt");
boolean renamed = file.renameTo(newFile);
false otherwise. The dest parameter is the new abstract pathname for the named file. File newFile = new File("data/text2.txt");
boolean renamed = file.renameTo(newFile);
11.file.mkdir():
This method creates the directory named by this abstract pathname. It returns true if the directory is successfully created, false otherwise.
boolean directoryCreated = file.mkdir();
Tip:renameTo方法通常用于通过在File参数中指定不同的路径来将文件移动到不同的目录中。如果目标文件已经存在,那么它将会被替换。
boolean directoryCreated = file.mkdir();
Tip:renameTo方法通常用于通过在File参数中指定不同的路径来将文件移动到不同的目录中。如果目标文件已经存在,那么它将会被替换。
53.Creating a new file using PrintWriter
File newFile = new File("data/text2.txt");
//在这一行中,创建了一个名为 newFile 的 File 对象,表示文件系统中的路径为 "data/text2.txt" 的文件。
如上文所述这行代码并不会在文件系统中创建文件,只是在内存中创建了一个文件对象。
if (newFile.exists()) {
System.out.println("File already exists!");
System.exit(0);
}
//if (newFile.exists()) { System.out.println("File already exists!"); System.exit(0); }:
这是一个条件语句。exists() 方法检查文件是否已经存在。如果文件已经存在,
会输出一条消息 "File already exists!" 并终止程序(System.exit(0) 用于退出程序,参数 0 表示正常退出)。
try {
PrintWriter output = new PrintWriter(newFile);
}
//这是一个异常处理块。在 try 块中,创建了一个 PrintWriter 对象,用于写入数据到文件。
如果文件不存在(根据前面的检查),则会创建新文件。请注意,这里的 printWriter 可以用于写入文本数据。
catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
//如果在创建 PrintWriter 对象时发生 IOException 异常,将进入 catch 块。在这里,
只是简单地打印异常消息到控制台。通常,更复杂的应用程序会包含更详细的错误处理逻辑。
54.PrintWriter
File file = new File("data/text1.txt");
try {
PrintWriter output = new PrintWriter(file); //假设文件存在
output.println("Tanjiro");
output.println("Zenitsu");
output.println("Inosuke"); //等同于System.out.println
output.close(); //必须使用close()方法来写入数据和关闭文件
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
//要创建文件data/text1.txt,您需要首先创建文件夹数据
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class WriteToFileExample {
public static void main(String[] args) {
File file = new File("data/text1.txt");
file.getParentFile().mkdirs();
try {
PrintWriter output = new PrintWriter(file);
output.println("Tanjiro");
output.println("Zenitsu");
output.println("Inosuke");
output.close();
System.out.println("Data has been written to the file successfully.");
} catch (IOException ioe) {
System.out.println("An error occurred: " + ioe.getMessage());
}
}
}
55.In-Class Quiz 10.1
反复运行上述写入数据的程序,它会再次创建 "data/text1.txt" 文件并向其中写入相同的三行数据。
每次运行程序时,它都会重新创建该文件并覆盖之前的内容,因此文件中将仍然只包含这三行数据,并不会累加新的数据。
每次运行程序时,它都会重新创建该文件并覆盖之前的内容,因此文件中将仍然只包含这三行数据,并不会累加新的数据。
56.BufferedWriter
缓冲器
newLine方法用于插入平台无关的换行符。这些步骤有助于确保数据准确地写入到文件中,并保持良好的文件内容格式。
try {
FileWriter file = new FileWriter("data/text1.txt");
BufferedWriter buffer = new BufferedWriter(file);
buffer.write("Tanjiro");
buffer.newLine();
buffer.write("Zenitsu");
buffer.newLine();
buffer.flush();
buffer.write("Inosuke");
buffer.newLine();
buffer.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
总的来说,write方法将数据写入已缓冲区,flush方法用于将已缓冲的数据刷新到文件中,确保数据被写入。newLine方法用于插入平台无关的换行符。这些步骤有助于确保数据准确地写入到文件中,并保持良好的文件内容格式。
57.Using Scanner
File file = new File("data/text1.txt");
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String name = input.nextLine();
Swordsman swordsman = new Swordsman(name);
System.out.println(swordsman);
}
input.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
58.Reading csv data using Scanner
csv (comma-separated values) file
关于CSV文件的数据格式,每行的数据由逗号分隔,第一个值是角色名字,第二个值是击败的妖魔数量。示例CSV文件是:
Tanjiro,10
Zenitsu,5
Inosuke,8
关于CSV文件的数据格式,每行的数据由逗号分隔,第一个值是角色名字,第二个值是击败的妖魔数量。示例CSV文件是:
Tanjiro,10
Zenitsu,5
Inosuke,8
File file = new File("data/demonslayerdata.csv");
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
//while循环,它会一行一行地读取文件,直到文件的末尾。
String line = input.nextLine();
//读取当前行的文本内容。
String[] values = line.split(",");
//使用逗号作为分隔符,将当前行的文本内容分割成一个字符串数组。 就是,前算一个字符串的数组元素
String name = values[0];
//从数组中取得第一个元素,即角色的名字。
int numDemonsKilled = Integer.parseInt(values[1]);
//从数组中取得第二个元素,将其解析为整数,表示该角色击败的妖魔数量。
Swordsman swordsman = new Swordsman(name, numDemonsKilled);
//创建一个Swordsman对象,将从文件中读取的数据传递给构造函数。
System.out.println(swordsman);
//打印输出当前读取的Swordsman。
}
input.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
Comments NOTHING