字节流
【向文件中写入字符串】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * 字节流
* 向文件中写入字符串
* */
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);
OutputStream out = new FileOutputStream(f);
String str= "你好" ;
byte [] b=str.getBytes();
out.write(b);
out.close();
}
} |
查看hello.txt会看到“你好”
当然也可以一个字节一个字节的写。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * 字节流
* 向文件中一个字节一个字节的写入字符串
* */
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);
OutputStream out = new FileOutputStream(f);
String str= "你好" ;
byte [] b=str.getBytes();
for ( int i = 0 ; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
} |
结果还是:“你好”
向文件中追加新内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** * 字节流
* 向文件中追加新内容:
* */
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);
OutputStream out = new FileOutputStream(f, true );
String str= "Rollen" ;
//String str="\r\nRollen"; 可以换行
byte [] b=str.getBytes();
for ( int i = 0 ; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
} |
【运行结果】:
你好Rollen
【读取文件内容】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * 字节流
* 读文件内容
* */
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);
InputStream in= new FileInputStream(f);
byte [] b= new byte [ 1024 ];
in.read(b);
in.close();
System.out.println( new String(b));
}
} |
【运行结果】
你好Rollen
Rollen_
但是这个例子读取出来会有大量的空格,我们可以利用in.read(b);的返回值来设计程序。如下:
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);
InputStream in= new FileInputStream(f);
byte [] b= new byte [ 1024 ];
int len=in.read(b);
in.close();
System.out.println( "读入长度为:" +len);
System.out.println( new String(b, 0 ,len));
}
} |
【运行结果】:
读入长度为:18
你好Rollen
Rollen
读者观察上面的例子可以看出,我们预先申请了一个指定大小的空间,但是有时候这个空间可能太小,有时候可能太大,我们需要准确的大小,这样节省空间,那么我们可以这样干:
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);
InputStream in= new FileInputStream(f);
byte [] b= new byte [( int )f.length()];
in.read(b);
System.out.println( "文件长度为:" +f.length());
in.close();
System.out.println( new String(b));
}
} |
文件长度为:18
你好Rollen
Rollen
将上面的例子改为一个一个读:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * 字节流
* 读文件内容,节省空间
* */
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);
InputStream in= new FileInputStream(f);
byte [] b= new byte [( int )f.length()];
for ( int i = 0 ; i < b.length; i++) {
b[i]=( byte )in.read();
}
in.close();
System.out.println( new String(b));
}
} |
输出的结果和上面的一样。
细心的读者可能会发现,上面的几个例子都是在知道文件的内容多大,然后才展开的,有时候我们不知道文件有多大,这种情况下,我们需要判断是否独到文件的末尾。
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);
InputStream in= new FileInputStream(f);
byte [] b= new byte [ 1024 ];
int count = 0 ;
int temp= 0 ;
while ((temp=in.read())!=(- 1 )){
b[count++]=( byte )temp;
}
in.close();
System.out.println( new String(b));
}
} |
【运行结果】
你好Rollen
Rollen_
提醒一下,当独到文件末尾的时候会返回-1.正常情况下是不会返回-1的
转自: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高级技术整理,包含多线程、虚拟机、JAVA IO/NIO 、Java集合 等高级进阶知识点
最新整理的Java中的IO与NIO相关面试题目总结,java求职面试题,共15题,含答案解析,希望能帮到有需求的同学
java详细的知识点整理,包括:jvm原理、IO、类加载过程、集合、线程、反射、泛型等java基础,spring原理、特点,微服务架构、数据库引擎、消息组件、算法、数据结构等。偏理论的知识较多,主要用于面试。