字符流
【向文件中写入数据】
现在我们使用字符流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 字符流
* 写入数据
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "D:" +File.separator+ "hello.txt" ;
File f= new File(fileName);
Writer out = new FileWriter(f);
String str= "hello" ;
out.write(str);
out.close();
}
} |
当你打开hello。txt的时候,会看到hello
其实这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。
当你如果想问文件中追加内容的时候,可以使用将上面的声明out的哪一行换为:
Writer out =new FileWriter(f,true);
这样,当你运行程序的时候,会发现文件内容变为:
hellohello如果想在文件中换行的话,需要使用“\r\n”
比如将str变为String str="\r\nhello";
这样文件追加的str的内容就会换行了。
从文件中读内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/** * 字符流
* 从文件中读出内容
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "D:" +File.separator+ "hello.txt" ;
File f= new File(fileName);
char [] ch= new char [ 100 ];
Reader read= new FileReader(f);
int count=read.read(ch);
read.close();
System.out.println( "读入的长度为:" +count);
System.out.println( "内容为" + new String(ch, 0 ,count));
}
} |
【运行结果】:
读入的长度为:17
内容为hellohello
hello
当然最好采用循环读取的方式,因为我们有时候不知道文件到底有多大。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * 字符流
* 从文件中读出内容
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "D:" +File.separator+ "hello.txt" ;
File f= new File(fileName);
char [] ch= new char [ 100 ];
Reader read= new FileReader(f);
int temp= 0 ;
int count= 0 ;
while ((temp=read.read())!=(- 1 )){
ch[count++]=( char )temp;
}
read.close();
System.out.println( "内容为" + new String(ch, 0 ,count));
}
} |
运行结果:
内容为hellohello
hello
关于字节流和字符流的区别
实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候下后是会用到缓冲区的,是通过缓冲区来操作文件的。
读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。
使用字节流好还是字符流好呢?
答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。
文件的复制
其实DOS下就有一个文件复制功能,比如我们想把d盘下面的hello.txt文件复制到d盘下面的rollen.txt文件中,那么我们就可以使用下面的命令:
copy d:\hello.txt d:\rollen.txt
运行之后你会在d盘中看见hello.txt.,并且两个文件的内容是一样的,(这是屁话)
下面我们使用程序来复制文件吧。
基本思路还是从一个文件中读入内容,边读边写入另一个文件,就是这么简单。、
首先编写下面的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/** * 文件的复制
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
if (args.length!= 2 ){
System.out.println( "命令行参数输入有误,请检查" );
System.exit( 1 );
}
File file1= new File(args[ 0 ]);
File file2= new File(args[ 1 ]);
if (!file1.exists()){
System.out.println( "被复制的文件不存在" );
System.exit( 1 );
}
InputStream input= new FileInputStream(file1);
OutputStream output= new FileOutputStream(file2);
if ((input!= null )&&(output!= null )){
int temp= 0 ;
while ((temp=input.read())!=(- 1 )){
output.write(temp);
}
}
input.close();
output.close();
}
} |
然后在命令行下面
javac hello.java
java hello d:\hello.txt d:\rollen.txt
现在你就会在d盘看到rollen。txt了,
OutputStreramWriter 和InputStreamReader类
整个IO类中除了字节流和字符流还包括字节和字符转换流。
OutputStreramWriter将输出的字符流转化为字节流
InputStreamReader将输入的字节流转换为字符流
但是不管如何操作,最后都是以字节的形式保存在文件中的。
将字节输出流转化为字符输出流
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * 将字节输出流转化为字符输出流
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "d:" +File.separator+ "hello.txt" ;
File file= new File(fileName);
Writer out= new OutputStreamWriter( new FileOutputStream(file));
out.write( "hello" );
out.close();
}
} |
运行结果:文件中内容为:hello
将字节输入流变为字符输入流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 将字节输入流变为字符输入流
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName= "d:" +File.separator+ "hello.txt" ;
File file= new File(fileName);
Reader read= new InputStreamReader( new FileInputStream(file));
char [] b= new char [ 100 ];
int len=read.read(b);
System.out.println( new String(b, 0 ,len));
read.close();
}
} |
【运行结果】:hello
前面列举的输出输入都是以文件进行的,现在我们以内容为输出输入目的地,使用内存操作流
ByteArrayInputStream 主要将内容写入内容
ByteArrayOutputStream 主要将内容从内存输出
使用内存操作流将一个大写字母转化为小写字母
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * 使用内存操作流将一个大写字母转化为小写字母
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String str= "ROLLENHOLT" ;
ByteArrayInputStream input= new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream output= new ByteArrayOutputStream();
int temp= 0 ;
while ((temp=input.read())!=- 1 ){
char ch=( char )temp;
output.write(Character.toLowerCase(ch));
}
String outStr=output.toString();
input.close();
output.close();
System.out.println(outStr);
}
} |
【运行结果】:
rollenholt
内容操作流一般使用来生成一些临时信息采用的,这样可以避免删除的麻烦。
管道流
管道流主要可以进行两个线程之间的通信。
PipedOutputStream 管道输出流
PipedInputStream 管道输入流
验证管道流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/** * 验证管道流
* */
import java.io.*;
/** * 消息发送类
* */
class Send implements Runnable{
private PipedOutputStream out= null ;
public Send() {
out= new PipedOutputStream();
}
public PipedOutputStream getOut(){
return this .out;
}
public void run(){
String message= "hello , Rollen" ;
try {
out.write(message.getBytes());
} catch (Exception e) {
e.printStackTrace();
} try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} /** * 接受消息类
* */
class Recive implements Runnable{
private PipedInputStream input= null ;
public Recive(){
this .input= new PipedInputStream();
}
public PipedInputStream getInput(){
return this .input;
}
public void run(){
byte [] b= new byte [ 1000 ];
int len= 0 ;
try {
len= this .input.read(b);
} catch (Exception e) {
e.printStackTrace();
} try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( "接受的内容为 " +( new String(b, 0 ,len)));
}
} /** * 测试类
* */
class hello{
public static void main(String[] args) throws IOException {
Send send= new Send();
Recive recive= new Recive();
try {
//管道连接 send.getOut().connect(recive.getInput());
} catch (Exception e) {
e.printStackTrace();
}
new Thread(send).start();
new Thread(recive).start();
}
} |
【运行结果】:
接受的内容为 hello , Rollen
转自:http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html
相关推荐
java中的IO整理完整版
Java中IO系统总结[整理].pdf
此文档是对JAVA 中的 IO 流的整理,其中有大多实用 而平时可以接触到的 IO 基础,对开发工作者有很大的帮助
Java中的IO整理完整版
JAVA_IO流整理思维导图.
java IO全面整理,整理了一下关于java 的IO操作,我是直接在测试的时候将关键的测试代码放上去了,并配以简洁的注解,适合有一定基础的朋友!
javaIO流整理.txt
此文档属于本人当初学习java基础之IO流,所整理的文档。里面有字节流与字符流的比较,也有总结使用不同方式读取文档的demo。希望对你的学习有帮助,谢谢!
Java io系统的设计初衷,就是为了实现“文件、控制台、网络设备”这些io设置的通信。例如,对于一个文件,我们...而到了java 1.1,为了与国际化进行接轨,在java io中添加了许多以字符(Unicode)为单位进行操作的类。
自己整理了一下javaIO流的相关知识点 用xmind软件做了一下
Java IO流思维导图,主要摘录整理的是java.io.*包下的所有IO对象,其中对应备注里包含各个IO对象的构造方法
对于服务器的并发处理能力,我们需要的是:每一毫秒服务器都能及时处理这一毫秒内收到的数百个不同TCP连接上的报文,与此同时,可能服务器上还有数以十万计的最近几秒没有收发任何报文的相对不活跃连接。...
Java多线程.drawio
该文档主要整理了Java IO流的相关信息,主要包括IO流的按照不同维度的分类、节点流、处理流、输入输出流的处理过程、抽象基类的使用等细节内容
网上都是不带书签,并且有些地方翻译有歧义。 所以我整理了一个。 特点:带书签 Scalable io in java 中文版,并且对有歧义的语义进行了修改。
Java IO体系结构看似庞大复杂,其实有规律可循,要弄清楚其结构,需要明白两点: 1. 其对称性质:InputStream 与 OutputStream, Reader 与 Writer,他们分别是一套字节输入-输出,字符输入-输出体系 2. 原始处理器(适配器)...
Java线程和IO总结[整理].pdf
常用的JAVA,Android包库,闲的整理一下打包上传,math3主要用于线性计算,回归分析等,jama主要用于矩阵计算,Poi用于输出大量数据到Excel,再导入matlab。commonIO也是用于输入输出流
Java-IO流基础例题 & 例题源码 & PPT教学文档(黑马程序员详细版)。
Java高级技术整理,包含多线程、虚拟机、JAVA IO/NIO 、Java集合 等高级进阶知识点