java在磁盘上新建一个文本文件,并写入9*9的乘法表; 读取9*9乘法表文件,并将其复制入另外一个文件; 逐行读取一个文本文件,并将其内容打印出来;
时间: 2024-03-20 20:42:42 浏览: 89
好的,以下是Java实现的代码:
1. 新建一个文本文件,并写入9*9的乘法表
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CreateFile {
public static void main(String[] args) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("multiplication_table.txt"));
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
bw.write(i + " x " + j + " = " + i*j + "\t");
}
bw.newLine();
}
bw.close();
System.out.println("乘法表已写入文件!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 读取9*9乘法表文件,并将其复制入另外一个文件
```java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("multiplication_table.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy_of_multiplication_table.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
System.out.println("乘法表已复制到新文件!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
3. 逐行读取一个文本文件,并将其内容打印出来
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("multiplication_table.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
希望能帮到你!
阅读全文