Java IO流-InputStream和OutputStream
Java IO流-InputStream和OutputStream
InputStream是输入流 OutputStream是输出流;
InputStream输入流可以把文件从硬盘读取到内存;
OutputStream输出流可以把文件从内存写入到硬盘;
我们实际使用的都是InputStream和OutputStream的子类;
比如文件操作方面用的是FileInputStream和FileOutputStream;
我们给下实例,****里会详细讲解:
准备工作,我们在C盘建一个txt文件 测试文件.txt
随便加点内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.java1234.chap10.sec03;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class Demo1 {
public static void main(String[] args) throws Exception {
File file= new File( "C://测试文件.txt" );
InputStream inputStream= new FileInputStream(file); // 实例化FileInputStream
byte b[]= new byte [ 1024 ];
int len=inputStream.read(b);
inputStream.close(); // 关闭输入流
System.out.println( "读取的内容是:" + new String(b, 0 ,len));
}
}
|
把文件从硬盘读取到内存,并且输出:
运行结果:
读取的内容是:我是人 www.java1234.com
上面那个是定义了固定字节数组 一批读取的,我们现在改进下,获取文件长度,然后定义指定字节数组的长度;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.java1234.chap10.sec03;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class Demo2 {
public static void main(String[] args) throws Exception {
File file= new File( "C://测试文件.txt" );
InputStream inputStream= new FileInputStream(file); // 实例化FileInputStream
int fileLength=( int )file.length();
byte b[]= new byte [fileLength];
inputStream.read(b);
inputStream.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
21
22
23
|
package com.java1234.chap10.sec03;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class Demo3 {
public static void main(String[] args) throws Exception {
File file= new File( "C://测试文件.txt" );
InputStream inputStream= new FileInputStream(file); // 实例化FileInputStream
int fileLength=( int )file.length();
byte b[]= new byte [fileLength];
int temp= 0 ;
int len= 0 ;
while ((temp=inputStream.read())!=- 1 ){
// 一个字节一个字节读取,放到b字节数组里
b[len++]=( byte )temp;
}
inputStream.close(); // 关闭输入流
System.out.println( "读取的内容是:" + new String(b));
}
}
|
运行结果还是一样的;
下面是讲下输出流;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.java1234.chap10.sec03;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Demo4 {
public static void main(String[] args) throws Exception {
File file= new File( "C://测试文件.txt" );
OutputStream out= new FileOutputStream(file);
String str= "你好,我好,大家好,Java好" ;
byte b[]=str.getBytes();
out.write(b); // 将b字节数组写入到输出流
out.close(); // 关闭输出流
}
}
|
我们把指定文件写入到文件;
上面那种是直接覆盖的,我们再来一个追加的;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.java1234.chap10.sec03;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Demo5 {
public static void main(String[] args) throws Exception {
File file= new File( "C://测试文件.txt" );
OutputStream out= new FileOutputStream(file, true );
String str= "你好,我好,大家好,Java好" ;
byte b[]=str.getBytes();
out.write(b); // 将b字节数组写入到输出流
out.close(); // 关闭输出流
}
}
|