文件的读取和写入(指定路径)

  1 package cn.jason.ios.streams;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileNotFoundException;
  8 import java.io.FileOutputStream;
  9 import java.io.FileReader;
 10 import java.io.IOException;
 11 
 12 /**
 13  * 测试IO流 * 
 14  *         读取指定文件的信息
 15  * @author 小风微灵
 16  *
 17  */
 18 public class MyStream {
 19 
 20     /**
 21      * 字节流读取信息到控制台
 22      * @param filePath        文件地址
 23      * @throws IOException    抛出的异常
 24      */
 25     private static void getFileInfos_ZJ(String filePath) throws IOException{
 26         
 27         //创建对象
 28         File file=new File(filePath);
 29         
 30         //FileInputStream         输入流
 31         FileInputStream fis=new FileInputStream(file);
 32         
 33         //需要一个缓冲器
 34         byte[] bs=new byte[1024*1024];
 35         
 36         //记录读取长度
 37         int length=fis.read(bs);
 38         
 39         //[3]读取数据(开水管洗澡)    
 40         String content=new String(bs,0,length);
 41         
 42         System.out.println("【缓冲】读取的内容:"+content);
 43         
 44         //[4]关闭资源(关闭水龙头)
 45         fis.close();
 46     }
 47     
 48     /**
 49      * 字符流读取信息到控制台
 50      * @param filePath
 51      * @throws IOException 
 52      */
 53     private static void getFileInfos_ZF(String filePath) throws IOException{
 54                 //创建对象
 55                 File file=new File(filePath);
 56                 
 57                 //字符流
 58                 FileReader fr=new FileReader(file);
 59                 
 60                 //开始读取数据
 61                 int content=0;
 62                 while ((content=fr.read())!=-1) {            
 63                     System.err.print((char)content);
 64                 }
 65                 fr.close();
 66     }
 67         
 68     /**
 69      * 缓冲字符流读取信息到控制台
 70      * read
 71      *        public int read(char[] cbuf,
 72      *                        int offset,
 73      *                        int length)
 74      *                 throws IOException将字符读入数组中的某一部分。 
 75      *            
 76      *        指定者:
 77      *            类 Reader 中的 read
 78      *        参数:
 79      *            cbuf - 目标缓冲区
 80      *            offset - 从其处开始存储字符的偏移量
 81      *            length - 要读取的最大字符数 
 82      *        返回:
 83      *            读取的字符数,如果已到达流的末尾,则返回 -1 
 84      *        抛出: 
 85      *            IOException - 如果发生 I/O 错误
 86      *
 87      * @throws IOException
 88      */
 89     private static void getFileInfos_ZF_HC(String filePath)throws IOException{
 90         //读取
 91                 File file=new File(filePath);
 92                 FileInputStream fis=new FileInputStream(file);        
 93                 BufferedInputStream bis=new BufferedInputStream(fis);
 94             //工具    
 95                 int length=0;
 96                 byte[] b=new byte[1024];
 97                 StringBuilder infos=new StringBuilder();
 98         //输出        
 99                 while((length=bis.read(b))!=-1){
100                     String str=new String(b,0,length);                    
101                     infos.append(str);
102                 }
103                 System.out.println(infos.toString());
104                 
105         //关闭资源            
106                 bis.close();
107                 fis.close();
108     }
109     
110     /**
111      *拷贝一个文件的内容到另外一个文件中
112      * @param srcFilePath    源文件地址
113      * @param desFilePath    目标文件地址
114      * @throws IOException 
115      */
116     private static void sFile_To_OtherFile(String srcFilePath,String desFilePath) 
117                                             throws IOException{
118         //读取文件
119             File fin=new File(srcFilePath);
120             FileInputStream fis=new FileInputStream(fin);        
121             BufferedInputStream bis=new BufferedInputStream(fis);
122             
123         //写入文件
124             File fout=new File(desFilePath);
125             FileOutputStream fos=new FileOutputStream(fout,true);//追加信息,而不是覆盖        
126             BufferedOutputStream bos=new BufferedOutputStream(fos);
127             
128         //工具准备    
129             //缓冲写法1【测试通过】
130 //            byte[] b=new byte[1024];
131 //            int length=0;
132 //            while((length=bis.read(b))!=-1){
133 //                bos.write(b, 0, length);
134 //            }
135             //缓冲写法2【测试通过】
136             int length=0;
137             while((length=bis.read())!=-1){
138                 bos.write(length);
139             }
140         //关闭资源    
141             bos.close();
142             fos.close();
143             bis.close();
144             fis.close();    
145     }
146     
147     /**
148      * 程序入口
149      * @param params    启动参数
150      * @throws IOException     IO流异常抛出
151      */
152     public static void main(String[] params) throws IOException {
153         getFileInfos_ZF_HC("F:\FileDemos\2.txt");
154         sFile_To_OtherFile("F:\FileDemos\2.txt","F:\FileDemos\test.txt");
155     }
156 }
原文地址:https://www.cnblogs.com/newwind/p/5650147.html