Java IO总结之缓冲读入文件
package com.io; import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList; /**
* 缓冲读入文件
* @author Administrator
*
*/
public class BufferedInputFile { public static String read(String filename) throws IOException{
//reading input by lines:
BufferedReader in = new BufferedReader(new FileReader(filename));
String s;
StringBuilder sb= new StringBuilder(); while((s=in.readLine())!=null){
sb.append(s+"\n");
}
in.close();
return sb.toString();
} //test1,打开一个文本文件,每次读取一行内容,将每行作为一个string读入,并将该string对象存入linklist中,逆序打印linklist
public static void printLinkList(String fileName) throws IOException{
BufferedReader in = new BufferedReader(new FileReader("C:/Users/Administrator/Desktop/exec.txt"));
String string;
LinkedList<String> list = new LinkedList<String>();
while((string=in.readLine())!=null){
list.add(string);
}
/*for(int i=list.size();i>=0;i--){
System.out.println(list.get(i));
}*/
java.util.ListIterator<String> item = list.listIterator(list.size()); while(item.hasPrevious()){
System.out.println(item.previous());
} while(item.hasNext()){
System.out.println(item.next());
} /**
* 结果:
* 4-->4-->4:4:4:ghdgml11
3-->3-->3:3:3:flypiger
2-->2-->2:2:2:ghdgml11
1-->1-->1:1:1:flypiger
*/ } public static void main(String[] args) throws IOException {
//System.out.println(read("C:/Users/Administrator/Desktop/exec.txt"));
printLinkList("C:/Users/Administrator/Desktop/exec.txt");
}
}
不要问为什么,代码诠释一切
package com.io; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
/**
* 基本的io
* @author Administrator
*
*/
public class BasicFileOutput { public static String file = "C:/Users/Administrator/Desktop/exec.txt";
public static String file1 = "C:/Users/Administrator/Desktop/exe.txt";
public static void main(String[] args) throws IOException {
//使用BufferedReader缓冲读入流,使用StringReader包装一个BufferedInputFile.read方法读取的字符串文件流
System.out.println(BufferedInputFile.read(file));
BufferedReader in = new BufferedReader(new StringReader(BufferedInputFile.read(file)));
//FileWriter可以写数据的对象,用BufferedWriter缓冲流包装提升性能,再用PrintWriter包装提升格式
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file1)));
int lineCount = 1;
String string;
while((string=in.readLine())!=null){
out.println(lineCount++ +": "+string);
}
out.flush();
out.close();
in.close();
System.out.println(BufferedInputFile.read(file1));
}
}
package com.io; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; /**
* 读取二进制文件
* @author Administrator
*
*/
public class BinaryFile { public static byte[] read(File file) throws IOException{
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
byte[] data = new byte[inputStream.available()]; inputStream.read(data);
inputStream.close(); return data; }
public static byte[] read(String file) throws IOException{
return read(new File(file).getAbsoluteFile());
}
}
package com.io; import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
/**
* 格式化的内存输出
* @author Administrator
*
*/
public class FormattedMemoryInput { public static void main(String[] args) throws IOException {
DataInputStream in= new DataInputStream(new ByteArrayInputStream(BufferedInputFile.read("C:/Users/Administrator/Desktop/exec.txt").getBytes()));
//available用来查询还有多少可供存取的字符
while(in.available()!=0){
System.out.println(in.readByte());
}
}
}
package com.io; import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; /**
* 用GZIP进行压缩,有空总结常用流的包装
* @author Administrator
*
*/
public class GZIPcompress {
/**压缩/解压缩 常用流,可根据实例替换
* ZipOutputStream
* ZipInputStream
* GZipOutputStream
* GZipInputStream
* @param args
* @throws IOException
*/ public static void main(String[] args) throws IOException {
if(args.length==0){
System.out.println(
"Usage: \nGZIPcompress file\n"+
"\tUsers GZIP compression to compress "+
"the file to test.gz"
);
BufferedReader in = new BufferedReader(new FileReader("C:/Users/Administrator/Desktop/exec.txt"));
//后缀名可以是.gz .gzip.zip
BufferedOutputStream out= new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("C:/Users/Administrator/Desktop/test.gz")));
System.out.println("Writing file*********************************"); int c;
while((c=in.read())!=-1){
out.write(c);
}
out.close();
in.close();
System.out.println("reading file*********************************"); BufferedReader in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("C:/Users/Administrator/Desktop/test.gz"))));
String string;
while((string=in2.readLine())!=null){
System.out.println(string);
}
}
}
}
package com.io; import java.io.IOException;
import java.io.StringReader; /**
* 从内存中读取,没什么好说的
* @author Administrator
*
*/
public class MemoryInput { public static void main(String[] args) throws IOException {
StringReader in = new StringReader(BufferedInputFile.read("C:/Users/Administrator/Desktop/exec.txt"));
//SringReader读取的int类型的,所以要转换成char打印
int c;
while((c=in.read())!=-1){
System.out.println((char)c);
}
}
}
Java IO总结之缓冲读入文件的更多相关文章
- java - >IO流_缓冲流(高效流)
缓冲流(高效流) 在我们学习字节流与字符流的时候,大家都进行过读取文件中数据的操作,读取数据量大的文件时,读取的速度会很慢,很影响我们程序的效率,那么,我想提高速度,怎么办? Java中提高了一套缓冲 ...
- Java IO流之缓冲流
一.缓冲流简介 二.BufferedInputStream 三.其他三种缓冲流
- java IO流 对文件操作的代码集合
Io流 按照分类 有两种分类 流向方向: 有输入流和输出流 按照操作类型有:字节流和字符流 按照流向方向 字节流的一些操作 //读文件 FileInputStream fis = new FileIn ...
- Java IO之处理流
一.处理流: 增强功能,提供性能,在节点流之上. 二.节点流与处理流的关系 节点流(字节流.字符流)处于IO操作的第一线,所有操作必须通过它们进行: 处理流可以对其他流进行处理(提高效率或操作灵活性) ...
- JAVA Io 缓冲输入输出流
java中提供带缓冲的输入输出流.在打开文件进行写入或读取操作时,都会加上缓冲,提高了IO读写性能. 1. BufferedInputStream 缓冲输入流 2. BufferedOutputStr ...
- Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream
Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...
- Java IO流之【缓冲流和文件流复制文件对比】
与文件流相比,缓冲流复制文件更快 代码: package Homework; import java.io.BufferedOutputStream; import java.io.File; imp ...
- Java IO学习笔记(二)缓冲流
处理流:包在别的流上的流,可以对被包的流进行处理或者提供被包的流不具备的方法. 一.缓冲流:套接在相应的节点流之上,带有缓冲区,对读写的数据提供了缓冲的功能,提高读写效率,同时增加一些新的方法.可以减 ...
- java IO之 File类+字节流 (输入输出 缓冲流 异常处理)
1. File类
随机推荐
- 设计模式:单例模式(Singleton)
定义:确保一个类仅有一个实例,并提供一个访问它的全局访问点. 优点:在内存中只有一个对象,节省了内存空间 示例: Singleton.cs 写法一:非线程安全 public class Singlet ...
- Linux 下动态库 / 静态库(依赖)
一. 依赖动态库的动态库 libfun.so依赖动态库libtest.so(libfun.so动态库里的函数intnothing()调用了libtest.so里的intmytest()函数),而mai ...
- android Textview动态设置大小
import android.app.Activity; //import com.travelzen.tdx.BaseActivity; //import com.travelzen.tdx.uti ...
- Express创建并运行node项目(Jade和EJS模版引擎)
1.创建Node项目 [Jade模板] > express nodeJade express创建项目若不显示指定模板,默认使用Jade,以下写法都可以: express -jade nodeJa ...
- Round and Round We Go
http://acm.hdu.edu.cn/showproblem.php?pid=1313 考查大整数与小整数相乘 #include<iostream> #include<cstd ...
- Fiddler-001-抓包工具初识
Fiddler 是一个非常简单的网络调试器,也是目前最常用的http抓包工具之一 .通过 Fiddler,我们能够能够记录客户端和服务器之间的所有 HTTP请求,即记录并检查所有你的电脑和互联网之间的 ...
- php mysql连接例子
<?PHP @$conn = mysql_connect("127.0.0.1","root",""); //返回false或reso ...
- 【转】flash不建议设置wmode及wmode解释
flash不建议设置wmode及wmode解释 2011-11-21 16:57:57| 分类: flash cs3 AS3.0|举报|字号 订阅 转自:http://www.webryan ...
- OPENCV3.1+VS 坑我笔记!
1.调用findContours()函数程序崩溃. 原因: >>分析opencv源代码,跟踪测试,进入工程:opencv_imgproc 发现findContours函数 是调用 _fin ...
- LeetCode Maximal Square
原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...