Java IO流技术

第一种通过字节对文件读取

 1 package class04.filems;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.util.Scanner;
 8 
 9 /**
10 编写一个文件操作类(FileOperation),具有复制和剪切两个方法,要求:
11     源路径和目标路径由控制台输入
12     使用静态方法
13  * */
14 
15 public class FileOperation {
16 
17     public static String copyFile(String path) throws IOException {
18         StringBuffer sb = new StringBuffer();
19         File file = new File(path);
20         
21         if(!file.isFile() || !file.exists()) {
22             return "";
23         }
24         
25         FileInputStream inputStream = new FileInputStream(file);
26         for(int i = 0; i < file.length(); i++ ) {
27             //read()方法可填写byte数组
28             sb.append((char)inputStream.read());
29         }
30         inputStream.close();
31         return sb.toString();
32     }
33     
34     public static boolean pasteFile(String path, String txt) throws IOException {
35         File file = new File(path);
36         
37         FileOutputStream outputStream = new FileOutputStream(file);
38         for(int i = 0; i < txt.length(); i++ ) {
39             //write()方法可填写byte数组
40             outputStream.write(txt.charAt(i));
41         }
42         outputStream.close();
43         return true;
44     }
45     
46     
47     public static void main(String[] args) throws IOException {
48         String txt = "2018 FIFA World Cup will play in Russia.";
49         pasteFile("D:\\data.txt", txt);
50         System.out.println(copyFile("D:\\data.txt"));
51     }
52 
53 }
View Code

第二种

 1 package class04.filems;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileReader;
 7 import java.io.FileWriter;
 8 import java.io.IOException;
 9 
10 public class FileOperationT {
11 
12     public static String readFile(String path) throws IOException {
13         File file = new File(path);
14         FileReader fr = new FileReader(file);
15         BufferedReader bf = new BufferedReader(fr);
16         
17         StringBuffer sb = new StringBuffer();
18         String string = "";
19         
20         while((string = bf.readLine()) != null) {
21             sb.append(string);
22         }
23         bf.close();
24         fr.close();
25         return sb.toString();
26     }
27     
28     public static boolean writeFile(String path, String txt) throws IOException {
29         File file = new File(path);
30         FileWriter fw = new FileWriter(file);
31         BufferedWriter bw = new BufferedWriter(fw);
32         for(int i = 0; i < txt.length(); i++ ) {
33             bw.write(txt.charAt(i));
34         }
35         bw.newLine();
36         bw.close();
37         fw.close();
38         return true;
39     }
40     
41     public static void main(String[] args) throws IOException {
42         String txt = FileOperationT.readFile("D:\\data.txt");
43         System.out.println(txt);
44         FileOperationT.writeFile("D:\\data2.txt", txt);
45     }
46 
47 }
View Code

猜你喜欢

转载自www.cnblogs.com/Q1143316492/p/9124252.html