1.读+写,拷贝
代码:
import java.io.*;
public class FileRW_Test{
public static void main(String[] args) throws IOException {
String A_Path = "..\\bin\\谷歌浏览器页面翻译恢复详细流程.txt";
String B_Path = "D:\\Program Files\\Java\\jdk1.8.0_92\\bin\\拷贝.txt";
FileInputStream A_file = new FileInputStream(A_Path);
FileOutputStream B_file = new FileOutputStream(B_Path,true);
byte[] buffer = new byte[1024];
int length;
while((length = A_file.read(buffer)) != -1){
System.out.print(new String(buffer, 0, length));
}
A_file.getChannel().position(0);
B_file.write("我将继续学习JAVA!\n".getBytes());
while ((length = A_file.read(buffer)) != -1) {
B_file.write(buffer, 0, length);
}
A_file.close();
B_file.close();
System.out.println("文件读写拷贝成功!");
}
}
运行结果:
2.将斐波那契数列写入文档。
代码:
import java.util.*;
import java.io.*;
public class Fbnq_file{
public static void main(String[] args) throws IOException {
String B_Path = "D:\\Program Files\\Java\\jdk1.8.0_92\\bin\\斐波那契数.txt";
FileOutputStream B_file = new FileOutputStream(B_Path, true);
int i = 1;
int j = 1;
int sum;
Scanner input = new Scanner(System.in);
System.out.println("请输入你想得到多少个斐波那契数");
int n = input.nextInt();
System.out.print(i + " " );
System.out.print(j + " " );
B_file.write("1 1 ".getBytes());
for (int m = 3; m <= n; m++) {
sum = i + j;
System.out.print(sum + " " );
B_file.write((Integer.toString(sum)+" ").getBytes());
i = j;
j = sum;
}
B_file.close();
}
}
运行结果: