IO流_part1
2016-07-26 23:42
501 查看
package 流.IO.读写TXT.part1; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; /** * 通过InputStream的read方法,获得字节数组,然后用字节数组初始化字符串,从而输出文件内容到控制台 * File * InputStream FileInputStream * read方法 * close方法 */ public class Read1 { public static void main(String[] args) throws IOException { File file = new File("D://测试文件.txt"); InputStream inputStream = new FileInputStream(file); byte[] b = new byte[1024]; int len = inputStream.read(b); //从流中读取字节到byte数组中,这个len是指读取到的数量 inputStream.close(); System.out.println(len); System.out.println(new String(b,0,len)); } }
package 流.IO.读写TXT.part1; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; /** * 通过file.length获得文件中的字节大小,从而创建一个刚刚大小的字节数组 */ public class Read2 { public static void main(String[] args) throws IOException { File file = new File("D://测试文件.txt"); InputStream inputStream = new FileInputStream(file); int fileLength = (int) file.length(); byte[] b = new byte[fileLength]; inputStream.read(b); inputStream.close(); System.out.println(new String(b)); } }
package 流.IO.读写TXT.part1;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* 一个字节一个字节的读取
*/
public class Read3 {
public static void main(String[] args) throws IOException {
File file = new File("D://测试文件.txt");
InputStream inputStream = new FileInputStream(file);
int fileLength = (int) file.length();
byte[] b = new byte[fileLength];
int temp=0;
int len=0;
while((temp=inputStream.read())!=-1){
b[len++]=(byte) temp;
}
inputStream.close();
System.out.println(new String(b));
}
}
package 流.IO.读写TXT.part1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* InputStream OutputStream
* BufferedInputStream BufferedOutputStream
*
* 比较了buffered类的效率
*
* 实现的是一个复制的功能
*
*/
public class ReadWrite {
public static void bufferedStream() throws Exception {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D://测试文件.txt"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("G://测试文件2.txt"));
int b = 0;
long startTime = System.currentTimeMillis();
while ((b = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(b);
}
bufferedInputStream.close();
bufferedOutputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
}
// 非缓冲
public static void stream() throws Exception {
InputStream inputStream = new FileInputStream("D://测试文件.txt");
OutputStream outputStream = new FileOutputStream("G://测试文件.txt");
int b = 0;
long startTime = System.currentTimeMillis();
while ((b = inputStream.read()) != -1) {
outputStream.write(b);
}
inputStream.close();
outputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
}
public static void main(String[] args) throws Exception {
stream();
bufferedStream();
}
}
package 流.IO.读写TXT.part1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * 以覆盖的方式写入文件 * File * OutputStream FileOutputStream * write方法 * close方法 */ public class Write1 { public static void main(String[] args) throws IOException { File file = new File("D://测试文件.txt"); OutputStream out=new FileOutputStream(file,true); String str="你好, Java"; byte[] b=str.getBytes(); out.write(b); out.close(); } }
相关文章推荐
- mybatis做like模糊查询
- 三.可选项和控制流
- 剑指offer——平衡二叉树
- java编程---3.2 (关于String的练习题) 统计字符串中每个字符的个数,例如:把aaaabbaaccbb变成a6b4c2
- Dalvik学习笔记--启动过程
- Android中实现根据资源名获取资源ID
- (转) 实时SLAM的未来及与深度学习的比较
- Java线程优先级
- 利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包含增删改查、JavaBean反射原理,附源码)
- PHP.INI配置:文件上传功能配置
- JAVA 过滤标签将html内容转换为文本
- lucene源码分析---7
- 暑假集训第1天链表-数据结构上机测试2-1:单链表操作A
- 在express4上使用socket.io 1.4.6版本的心得(node.js实战读书笔记2)
- C - Can you find it? HDU 2141
- 销售数据聚类、关联分析
- 线程的生命周期及状态转换
- fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- (转) ICCV 2015:21篇最火爆研究论文
- linux基础-crontab