Java io流详解三
public class IOpractise {
public void iotest() {
int b= 0;
FileInputStream fis = null;
try {
fis = new FileInputStream("C:\\Users\\wb-cjz286752\\Desktop\\小程序.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("系统找不到指定文件!");
System.exit(-1);
}
long num =0;
try {
while((b=fis.read())!=-1){
//System.out.println((char)b);
System.out.println(b);
num++;
}
fis.close();
System.out.println();
System.out.println("总共读了"+num + "个字节的文件");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取错误!");
}
}
public void iotest1() throws IOException{
try {
FileInputStream fis = new FileInputStream("C:\\Users\\wb-cjz286752\\Desktop\\小程序.txt");
int read = 0;
while((read=fis.read())!=-1){
System.out.println(read);
}
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String iotest2() throws IOException{
StringBuffer result = new StringBuffer();
try {
BufferedReader bf = new BufferedReader(new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\webelement.txt"));
String str = null;
while ((str=bf.readLine())!=null){
result.append(str);
//System.out.println(result);
}
bf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return result.toString();
}
public void iotest3(String filename) throws IOException{
StringBuffer result = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String str = null;
while((str=br.readLine())!=null){
result.append(System.lineSeparator()+str);
//System.out.println();
}
b r.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result.toString());
}
public void iotest4() throws IOException{
try {
FileWriter fw = new FileWriter("C:\\Users\\wb-cjz286752\\Desktop\\1111111111111.txt");
fw.write("123llove");
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileReader fr = new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\1111111111111.txt");
int it =0;
while((it=fr.read())!=-1){
System.out.print((char)it);
}
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void iotest5(){
try {
FileReader fr = new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\webelement.txt");
int it =0;
char[] buf = new char[10];
StringBuilder sb = new StringBuilder();
try {
while((it=fr.read(buf))!=-1){
sb.append(new String(buf,0,it));
}
String str = sb.toString();
System.out.println(str);
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void iotest6() throws IOException{
FileWriter fw = new FileWriter("C:\\Users\\wb-cjz286752\\Desktop\\1111111111111.txt",true);
try {
fw.write("1234567890testtest!!!!!!!");
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void iotest7(){
try {
FileReader fr = new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\1111111111111.txt");
FileWriter fw = new FileWriter("C:\\Users\\wb-cjz286752\\Desktop\\2222222222222.txt");
char [] buf = new char[2];
int it =0;
while ((it=fr.read(buf))!=-1){
String str = new String(buf,0,it);
fw.write(str);
}
fw.flush();
fw.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void iotest8() throws IOException{
try {
FileReader fr = new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\2222222222222.txt");
BufferedReader br = new BufferedReader(fr);
String str = null;
StringBuilder sb = new StringBuilder();
while ((str=br.readLine())!=null){
sb.append(str);
}
br.close();
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void iotest9() throws IOException{
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\2222222222222.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\wb-cjz286752\\Desktop\\3333333333333.txt"));
String str = null;
while ((str=br.readLine())!=null){
bw.write(str);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void iotest10() throws IOException{
try {
FileInputStream fis = new FileInputStream("C:\\Users\\wb-cjz286752\\Desktop\\3333333333333.txt");
FileOutputStream fos = new FileOutputStream("C:\\Users\\wb-cjz286752\\Desktop\\4444444444444.txt");
int it = 0;
byte[] byt = new byte[1024];
while ((it=fis.read(byt))!=-1){
fos.write(byt,0 ,it);
}
fos.flush();
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
IOpractise iop = new IOpractise();
//iop.iotest3("C:\\Users\\wb-cjz286752\\Desktop\\webelement.txt");
iop.iotest10();
}
}
Java io流详解三的更多相关文章
- java IO流详解
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- 《Java基础知识》Java IO流详解
Java IO概念 1. 用于设备之间的数据传输. 2. Java 将操作数据流的功能封装到了IO包中. 3. 数据流流向分:输入流和输出流,操作对象为文件. 4. 流按照操作数据分:字节流(通用)和 ...
- Java IO流详解(六)——转换流
转换流也是一种处理流,它提供了字节流和字符流之间的转换.在Java IO流中提供了两个转换流:InputStreamReader 和 OutputStreamWriter,这两个类都属于字符流.其中I ...
- Java IO流详解(五)——缓冲流
缓冲流也叫高效流,是处理流的一种,即是作用在流上的流.其目的就是加快读取和写入数据的速度. 缓冲流本身并没有IO功能,只是在别的流上加上缓冲效果从而提高了效率.当对文件或其他目标频繁读写或操作效率低, ...
- Java IO流详解(二)——File类
在上一章博客中简单的介绍了Java IO流的一些特征.也就是对文件的输入输出,既然至始至终都离不开文件,所以Java IO流的使用得从File这个类讲起. File类的描述:File类是文件和目录路径 ...
- Java IO流详解(一)——简单介绍
文件在程序中是以流的形式来传输的.所以用Java来传输文件就得使用到Java IO流. 1.流的概念和作用 流:代表任何有能力产出数据的数据源对象或者是有能力接受数据的接收端对象<Thinkin ...
- Java IO流详解(三)——字节流InputStream和OutPutStream
我们都知道在计算机中,无论是文本.图片.音频还是视频,所有的文件都是以二进制(字节)形式存在的,IO流中针对字节的输入输出提供了一系列的流,统称为字节流.字节流是程序中最常用的流.在JDK中,提供了两 ...
- Java io流详解四
转载地址:http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html 写在前面:本文章基本覆盖了java IO的全部内容,jav ...
- Java io流详解二
原文地址https://www.cnblogs.com/xll1025/p/6418766.html 一.IO流概述 概述: IO流简单来说就是Input和Output流,IO流主要是用来处理设备之间 ...
随机推荐
- java部署ubuntu后中文显示问号问题
1.首先先回忆自身项目的编码格式,即在本地进行编码时使用的编码格式.UTF-82.检测tomcat的设置问题,在web.xml和server中的设置:server.xml中: <Connecto ...
- Tuning 14 Using Oracle Data Storage Structures Efficiently
90% 是Heap table Cluster 集群表, index-organized table: 就是把索引和表 和二为一了. partitioned table:表非常大, 逻辑上是一个大表, ...
- PAT004 Root of AVL Tree
题目: An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException
今天在练习spring aop时.调试程序出现下面错误 org.springframework.beans.factory.parsing.BeanDefinitionParsingException ...
- ImportError: cannot import name gof
今天打开spyder说调试一个theano程序,但是import theano提示 ImportError: cannot import name gof 最后解决方案 pip install --u ...
- Map Labeler (poj 2296 二分+2-SAT)
Language: Default Map Labeler Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1815 Ac ...
- 终于找到了最新的Chemdarw注册码
随着中国人对知识产权的保护意识提升,正版软件越来越流行,只有一小部分人还在寻找Chemdarw破解版.最新的ChemDraw 15正式版本已经强势来袭,在获取软件安装包之后需要有效的注册码才能激活软件 ...
- sql case when then else end sql_variant
/****************************************************************************** ** Name: usp_cfg_Get ...
- JMeter学习-021-JMeter 定时器的应用
定时器类型 下面我们看下jmeter提供了哪些定时器组件: 固定定时器 高斯随机定时器 Uniform Random Timer Synchronizing Timer Poisson Random ...
- Duilib教程-自动布局2
在上一节中,我简单介绍了控件随父LAYOUT自由移动的设置.在这一节,我将介绍一种常见的情况:嵌入窗口. 在项目中,我们很少会100%的编写一个软件,特别是界面相关的,我们会使用以前已经编写好的窗口, ...