1、编写程序,使用FileInputStream和FileOutputStream实现将D盘test文件夹中的test.txt文件复制到D盘det文件夹中;test.txt的内容是程序通过随机生成10个随机数来形成的。
2、编写程序,将题目1改为使用缓冲区完成文件拷贝。
3、编写程序,使用BufferedReader和BufferedWriter实现将D盘test文件夹中的test.txt文件复制到D盘det文件夹中;test.txt的内容是您未来职业意向(通过键盘录入并存放到test.txt文件)。
package test.pta.Student.wj; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; public class Main1 { public static void main(String[] args) throws IOException { // 创建FileInputStream对象用于读取原文件 File f=new File("D:\\test"); File f1=new File("D:\\det\\"); File f2=new File("D:\\test\\test.txt"); if(!f.exists()) { f.mkdir(); //f=new File("D:\\test"); } if(!f1.exists()) { f1.mkdir(); } /* if(!f2.exists()) { f2.createNewFile(); }*/ FileOutputStream fos = new FileOutputStream("D:\\test\\test.txt"); FileInputStream fis = new FileInputStream("D:\\test\\test.txt");// 创建FileOutputStream对象用于写入新文件 FileOutputStream fos1 = new FileOutputStream("D:\\det\\test.txt"); FileInputStream fis1 = new FileInputStream("D:\\det\\test.txt"); for (int i = 0; i <10 ; i++) { fos.write(String.valueOf(new Random().nextInt(10)).getBytes()); } System.out.println("D:\\test\\test.txt 写入成功"); int read; System.out.println("D:\\test\\test.txt中的内容:"); while ((read = fis.read()) != -1) { fos1.write(read); System.out.print((char)read); } System.out.println("\nD:\\test\\test.txt 复制到D:\\det\\test.txt成功"); System.out.println("D:\\det\\test.txt中的内容:"); while ((read = fis1.read()) != -1) { System.out.print((char)read); } // 关闭流 fis.close(); fos.close(); } }
package test.pta.Student.wj; import java.io.*; public class Main2 { public static void main(String[] args) throws IOException { // 创建BufferedInputStream和BufferedOutputStream对象 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test\\test.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\det\\test.txt")); BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream("D:\\det\\test.txt")); System.out.println("D:\\test\\test.txt中的内容:"); int read; while ((read = bis.read()) != -1) { bos.write(read); System.out.print((char)read); } // 关闭流 bis.close(); bos.close(); System.out.println("\nD:\\test\\test.txt 复制到D:\\det\\test.txt成功"); System.out.println("D:\\det\\test.txt中的内容:"); int q; //System.out.println(bis1.read()); while ((q = bis1.read()) != -1) { System.out.print((char)q); } // 关闭流 bis1.close(); } }
package test.pta.Student.wj; import java.io.*; public class Main3 { public static void main(String[] args) throws IOException { // 创建BufferedReader用于从键盘录入数据 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入您的未来职业意向"); String jobIntent = br.readLine(); // 将录入的数据写入到test.txt文件中 BufferedWriter bw1 = new BufferedWriter(new FileWriter("D:\\test\\test.txt")); bw1.write(jobIntent); bw1.close(); // 创建BufferedReader用于读取test.txt文件的内容 BufferedReader br1 = new BufferedReader(new FileReader("D:\\test\\test.txt")); // 创建BufferedWriter用于将读取的内容写入到新的文件中 BufferedWriter bw2 = new BufferedWriter(new FileWriter("D:\\det\\test.txt")); // 读取和写入操作 String line; while ((line = br1.readLine()) != null) { bw2.write(line); bw2.newLine(); // 写入换行符,实现与原文件相同的格式 } // 关闭资源 br1.close(); bw2.close(); System.out.println("文件复制成功!"); } }
最新评论