作为java的初学者,在接触java I/O操作的时候没有一头雾水呢?在进行读文件写文件或者在网络上传输文件的时候有没有一脸疑惑呢?到底什么时候用InputStream什么时候用OutputStream,什么时候read()什么时候write()呢。
现在告诉你,java I/O操作中的in和out(read和write)都是针对内存来说的,就记住,内存内存内存……
这样,我们就可以很容易理解了,当从磁盘读文件到内存,或者从传输流读文件到内存的,就用InputStream\read();从内存写文件到磁盘或写入流中,就用OutputStream\write();
例如:将一个文件在控制台打印出来(这个过程是这样的:从磁盘把读到内存,再从内存中把文件写到控制台;磁盘——>内存——>控制台),那么,我们就可以知道,应该会用到InputStream或read(),然后在OutputStream或write()
- public static void readFileByBytes(String fileName) {
- //创建file对象
- FileInputStream fileInput = null;
- try {
- File file = new File(fileName);
- if (!file.exists()) {
- file.createNewFile();
- }
- byte[] buffer = new byte[1024];
- fileInput = new FileInputStream(file); //就文件放入输入流中
- int byteread = 0;
- // byteread表示一次读取到buffers中的数量。
- while ((byteread = fileInput.read(buffer)) != -1) { //从流中读到内存,边读边到控制台显示
- System.out.write(buffer, 0, byteread);
- }
- } catch (Exception e) {
- // TODO: handle exception
- } finally {
- try {
- if (fileInput != null) {
- fileInput.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void readFileByChars(String fileName) { //
- FileReader reader = null; //声明一个reader
- try {
- File file = new File(fileName);
- if (!file.exists()) {
- file.createNewFile();
- }
- reader = new FileReader(file);
- char[] buffer = new char[1024]; //
- int charread = 0;
- while ((charread = reader.read(buffer)) != -1) { //通过流读到内存,一边读入内存一边写到控制台显示
- System.out.print(buffer);
- }
- } catch (IOException e) {
- // TODO: handle exception
- } finally {
- try {
- if (reader != null) {
- reader.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public void writeByFileOutputStream() {
- FileOutputStream fop = null; // 将信息从内存写到磁盘用到
File file = null;
try {
file = new File("C:/Users/Administrator/Desktop/newfile.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
Scanner input = new Scanner( System.in );
String content = input.nextLine();
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop = new FileOutputStream(file);
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
再看看网络编程中的文件传输
如服务器端发文件给客户端,那大致过程是这样的:
server:磁盘中读文件到内存,再从内存中将文件写入传输流中
client:从传输流中读文件到内存,再从内存中将文件写到磁盘
这样,我们就可以知道,server应该先用到InputStream/read(),再用OutputStream/write();client也一样
代码:服务器
public class FileServer{
public static void main(String[] args)throws Exception{
//创建文件流用来读取文件中的数据
File file=new File("lishengjie.jpg");
FileInputStream fos=new FileInputStream(file);
//创建网络服务器接受客户请求
ServerSocket ss=new ServerSocket(3108);
Socket client=ss.accept();
//创建网络输出流并提供数据包装器
OutputStream netOut=client.getOutputStream();
OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
//创建文件读取缓冲区
byte[] buf=new byte[2048];
int num=fos.read(buf);
while(num!=(-1)){//是否读完文件
doc.write(buf,0,num);//把文件数据写出网络缓冲区
doc.flush();//刷新缓冲区把数据写往客户端
num=fos.read(buf);//继续从文件中读取数据
}
fos.close();
doc.close();
}
}
客户端:
public class FileClient{
public static void main(String[] args)throws Exception{
//使用本地文件系统接受网络数据并存为新文件
File file=new File("newFile.jpg");
file.createNewFile();
RandomAccessFile raf=new RandomAccessFile(file,"rw");
// 通过Socket连接文件服务器
Socket server=new Socket(InetAddress.getLocalHost(),3108);
//创建网络接受流接受服务器文件数据
InputStream netIn=server.getInputStream();
InputStream in=new DataInputStream(new BufferedInputStream(netIn));
//创建缓冲区缓冲网络数据
byte[] buf=new byte[2048];
int num=in.read(buf);
while(num!=(-1)){//是否读完所有数据
raf.write(buf,0,num);//将数据写往文件
raf.skipBytes(num);//顺序写文件字节
num=in.read(buf);//继续从网络中读取文件
}
in.close();
raf.close();
}
}