JAVA之文件操作1,2,3,4

 1 package first_program;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 public class num_1v {
 7         public static void main(String[] args) {
 8             File file = new File("e:\\src.txt");//创建文件对象
 9             if(file.exists()){//如果文件存在
10                 file.delete();//将文件删除
11                 System.out.println("文件已删除");
12             }else{
13                 try{//try语句块捕捉可能出现的异常
14                     file.createNewFile();//创建该文件
15                 }catch(IOException e){
16                     e.printStackTrace();
17                 }
18                 System.out.println("文件已创建");
19             }
20         }
21 }
 1 package first_program;
 2 
 3 import java.io.*;
 4 
 5 public class num_2v {
 6     @SuppressWarnings("resource")
 7     public static void main(String[] args) {
 8         File file = new File("e:\\dest.txt");
 9         String str = "12345abcde@#$%&*软件技术专业435345";
10         try {
11             FileOutputStream fos = new FileOutputStream(file, true);
12             for(int i = 0;i < str.length();i++){
13                 fos.write((char)str.charAt(i));
14             }
15             /*byte[] buf = str.getBytes();
16              fos.write(buf);*/
17         } catch (Exception e) {
18             e.printStackTrace();
19         }
20     }
21 }
 1 package first_program;
 2 
 3 import java.io.*;
 4 /*文件字节输入流FileInputStream的应用
 5  修改num_2v中的程序,读文件"dest.txt",将读取到的数据输出在控制台。
 6  */
 7 public class num_3v {
 8     public static void main(String[] args) {
 9         //创建文件对象
10         File file = new File("e:\\dest.txt");
11         try {
12             //使用文件对象创建文件输入流对象,相当于打开文件
13             FileInputStream fis = new FileInputStream(file);
14             /*for(int i = 0;i<file.length();i++){
15                  char c = (char)(fis.read());//循环读取字符
16                  system.out.print(c);
17              }
18              */
19             //根据文件的字节长度创建字节数组,强制转为int型
20             byte[] buf = new byte[(int)file.length()];
21             //读取文件中的数据存放到字节数组中
22             fis.read(buf);
23             //利用字节数组创建字符串
24             String str = new String(buf);
25             //输出
26             System.out.println(str);
27             fis.close();
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31     }
32 }
 1 package first_program;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 public class num_4v {
 9     public static void main(String[] args) throws IOException{
10         File srcFile = new File("e:\\src.txt");//源文件对象
11         File destFile = new File("e:\\dest.txt");//目标文件对象
12         if(!(destFile.exists())){//判断目标文件是否存在
13             destFile.createNewFile();//如果不存在则创建新文件
14         }
15         //使用源文件对象创建文件输入流对象
16         FileInputStream fis = new FileInputStream(srcFile);
17         //使用目标文件对象创建文件输出流对象
18         FileOutputStream fos = new FileOutputStream(destFile);
19         byte[]buf = new byte[1024];//创建字节数组,作为临时缓冲
20         System.out.println("开始复制文件");
21         while (fis.read(buf) != -1) {//循环从文件输入流中读取数据
22             fos.write(buf);//写入到文件输出流中
23         }
24         System.out.println("文件复制成功");
25         fis.close();//关闭流
26         fos.close();
27         
28     }
29 }

猜你喜欢

转载自www.cnblogs.com/WuYangdan-5201314/p/10947920.html
今日推荐