文件的读取和写入(指定路径)
2017-07-26 15:01
260 查看
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 }
相关文章推荐
- 文件的读取和写入(指定路径)
- java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
- Python读取指定路径所有.txt文件,将文件名写入.txt
- maven 打JAR包资源文件指定路径与文件读取
- MFC中怎样将位图写入指定的Picture控件中,两种方式:1 加载已有位图 2读取位图文件;另外CreateDIBitmap的用法和StretchDIBits用法举例
- C++指定路径写入文件
- vbs读取文件内的信息将非有效数据移动到指定路径
- VS打开.net工程提示"无法读取项目文件"web.csproj",系统找不到指定路径"
- java按指定编码写入和读取文件内容的类
- 读取指定文件夹中所有文件名以及文件路径,并读到matlab
- 什么是沙盒模型?怎么写入、读取沙盒路径文件数据,哪些操作是属于私有api范畴
- 将信息写入指定路径的Txt文件里
- C++读取写入.txt文件(ifstream/ofstream)—读取指定行,修改指定行,复制文件,清除文件,统计文件行数
- Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)
- HDFS 读取、写入、遍历目录获取文件全路径
- java基础知识回顾之javaIO类--File类应用:获取指定目录下面的指定扩展名的文件,将文件的绝对路径写入到目的文件当中
- 读取网页上的Html源码中指定的数据写入文件
- MyEclipse10在读取XML文件中的数据找不到指定路径
- 使用VC实现读取指定路径下的文件,并显示所有文件的文件名和文件图标
- iOS 通过路径对文件写入数据 读取数据